#include<iostream>
using namespace std;
const int N=1e6+10;
int n,ans,ending=N+10;
struct node{
int lch;
int rch;
int w;
}tree[N];
void dfs(int root,int dep){
if(root==0) return ;
ans+=dep*tree[root].w;
dfs(tree[root].lch,dep+1);
dfs(tree[root].rch,dep+1);
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&tree[i].w);
scanf("%d",&tree[i].lch);
scanf("%d",&tree[i].rch);
}
for(int i=1;i<=n;i++){
ans=0;
dfs(i,1);
if(ending>ans) swap(ending,ans);
}
printf("%d",ending);
return 0;
}