import java.util.*;
public class Main {
static class Tree {
private class Node {
int data;
ArrayList children;
public Node(int data) {
this.data = data;
children = new ArrayList<>();
}
}
private Node root;
private int size;
public Tree() {
Scanner sc = new Scanner(System.in);
this.root = build(sc, null, 0);
}
private Node build(Scanner sc, Node parent, int j) {
int nodedata = sc.nextInt();
Node node = new Node(nodedata);
this.size++;
int children = sc.nextInt();
for (int i = 0; i < children; i++) {
Node child = this.build(sc, node, i);
node.children.add(child);
}
return node;
}
public void levelSum(int k) {
this.levelSum(this.root, k);
}
private void levelSum(Node node, int k) {
int s = 0;
int l = 0;
int f = 0;
LinkedList<Node> queue = new LinkedList<>();
queue.add(this.root);
while (!queue.isEmpty()) {
int sz = queue.size();
while (sz-- > 0) {
Node p = queue.peek();
queue.remove();
if (l == k) {
f = 1;
s += p.data;
} else {
for (int i = 0; i < p.children.size(); i++) {
queue.addLast(p.children.get(i));
}
}
}
l++;
if (f == 1) {
break;
}
}
System.out.println(s);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Tree tree = new Tree();
int k = sc.nextInt();
tree.levelSum(k);
}
}
