#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
const int maxn=1000010;
typedef long long ll;
int n,cnt;
int q[maxn],h,t;
int to[maxn<<1],next[maxn<<1],val[maxn<<1],head[maxn],fa[maxn],size[maxn];
ll ans,v[maxn<<1];
int readin()
{
int ret=0; char gc;
while(gc<‘0‘||gc>‘9‘) gc=getchar();
while(gc>=‘0‘&&gc<=‘9‘) ret=ret*10+gc-‘0‘,gc=getchar();
return ret;
}
ll z(ll x)
{
return x>0?x:-x;
}
void add(int a,int b,int c)
{
to[cnt]=b;
val[cnt]=c;
next[cnt]=head[a];
head[a]=cnt++;
}
int main()
{
n=readin();
memset(head,-1,sizeof(head));
int i,j,a,b,c,u;
for(i=1;i<n;i++)
{
a=readin(),b=readin(),c=readin();
add(a,b,c);
add(b,a,c);
}
h=1;
q[++t]=1;
while(h<=t)
{
u=q[h++];
for(i=head[u];i!=-1;i=next[i])
{
if(to[i]!=fa[u])
{
fa[to[i]]=u;
q[++t]=to[i];
size[to[i]]=1;
v[to[i]]=val[i];
}
}
}
for(i=n;i>=2;i--)
{
size[fa[q[i]]]+=size[q[i]];
ans+=v[q[i]]*(z((long long)size[q[i]]*2-n));
}
printf("%lld",ans);
return 0;
}