#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<ctime>
#define LL long long
#define inf 0x7ffffff
#define pa pair<int,int>
#define pi 3.1415926535897932384626433832795028841971
using namespace std;
inline LL read()
{
LL x=0,f=1;char ch=getchar();
while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
return x*f;
}
struct edge{int to,next,v;}e[2000010];
struct bian{int x,y,z;}b[1000010];
int n,m,cnt;
int head[1000010];
bool mrk[1000010];
int fa[1000010];
int son[1000010];
LL ans;
inline void ins(int u,int v,int w)
{
e[++cnt].to=v;
e[cnt].v=w;
e[cnt].next=head[u];
head[u]=cnt;
}
inline void insert(int u,int v,int w)
{
ins(u,v,w);
ins(v,u,w);
}
inline void dfs(int x)
{
if (mrk[x])return;mrk[x]=1;
son[x]=1;
for (int i=head[x];i;i=e[i].next)
if (!mrk[e[i].to])
{
fa[e[i].to]=x;
dfs(e[i].to);
son[x]+=son[e[i].to];
}
}
int main()
{
n=read();
for (int i=1;i<n;i++)
{
b[i].x=read();b[i].y=read();b[i].z=read();
insert(b[i].x,b[i].y,b[i].z);
}
dfs(1);
for (int i=1;i<n;i++)
{
int x=b[i].x,y=b[i].y;
if (fa[y]==x)swap(x,y);
ans+=(LL)abs(son[x]-(n-son[x]))*b[i].z;
}
printf("%lld\n",ans);
}