package tiantiDaily;
import java.util.Scanner;
class TreeNode {
int val;
int left;
int right;
int father;
TreeNode(int x) {
val = x;
}
}
public class _H {
static int n, sum, ans = Integer.MAX_VALUE;
static TreeNode[] t = new TreeNode[106];
static int[] vis = new int[105];
public static void dfs(int step, int pos) {
sum += step * t[pos].val;
int fa = t[pos].father, l = t[pos].left, r = t[pos].right;
if (fa != 0 && vis[fa] == 0) {
vis[fa] = 1;
dfs(step + 1, fa);
}
if (l != 0 && vis[l] == 0) {
vis[l] = 1;
dfs(step + 1, l);
}
if (r != 0 && vis[r] == 0) {
vis[r] = 1;
dfs(step + 1, r);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
for (int i = 1; i <= n; i++) {
t[i] = new TreeNode(sc.nextInt());
t[i].left = sc.nextInt();
t[i].right = sc.nextInt();
if (t[i].left != 0)
t[t[i].left] = new TreeNode(i);
if (t[i].right != 0)
t[t[i].right] = new TreeNode(i);
if (t[i].left != 0)
t[t[i].left].father = i;
if (t[i].right != 0)
t[t[i].right].father = i;
}
for (int i = 1; i <= n; i++) {
sum = 0;
for (int j = 0; j < vis.length; j++) {
vis[j] = 0;
}
vis[i] = 1;
dfs(0, i);
ans = Math.min(ans, sum);
}
System.out.println(ans);
}
}