Problem:
You are a programmer who loves pretty girl games (a sub-genre of dating simulation games). A game, which is titled "Floatable Heart" and was released last Friday, has arrived at your home just now. This game has multiple stories. When you complete all of those stories, you can get a special figure of the main heroine, Megumi. So, you want to hurry to play the game! But, let's calm down a bit and think how to complete all of the stories in the shortest time first.

In fact, you have the special skill that allows you to know the structure of branching points of games. By using the skill, you have found out that there are n branching points in this game and i-th branching point has k_{i} choices. This game is so complicated that multiple routes get to i-th branching point probably. You also noticed that it takes c_{ij} minutes to proceed from i-th branching point to another branching point (or an ending), if you select j-th choice of i-th branching point. Of course you need to select all the choices on all the branching points and read stories between a branching point and another branching point (or an ending) to complete all of those stories. In addition, you can assume it only takes negligible time to return to the beginning of the game ("reset") and to play from the beginning to the first branching point.

The game's manual says that this game has an additional feature called "Quick Save", and the feature allows you to record the point where you are currently playing and return there at any time later. However, this feature is not working for some bug. Thus you have to restart from the first branching point every time, if you reach an ending or quit the game on the way. Any patch to fix this bug has not been yet published. This is an imposed tribulation for the fastest players.

Well, let's estimate how long it will take for completing all of the stories in the shortest time.

Input

A data set is given in the following format.

n
k_{1} t_{11} c_{12} ... t_{1k_{1}} c_{1k_{1}}
:
:
k_{n} t_{n1} c_{n2} ... t_{nk_{n}} c_{nk_{n}}

The first line of the data set contains one integer n (2 \leq n \leq 1{,}000), which denotes the number of the branching points in this game. The following n lines describe the branching points. The i-th line describes the branching point of ID number i. The first integer k_{i} (0 \leq k_{i} \leq 50) is the number of choices at the i-th branching point. k_{i} ≥ 0 means that the i-th branching point is an ending. Next 2k_{i} integers t_{ij} (1 \leq t_{ij} \leq n) and c_{ij} (0 \leq c_{ij} \leq 300) are the information of choices. t_{ij} denotes the ID numbers of the next branching points when you select the j-th choice. c_{ij} denotes the time to read the story between the i-th branching point and the t_{ij}-th branching point. The branching point with ID 1 is the first branching point. You may assume all the branching point and endings are reachable from the first branching point. You may also assume that there is no loop in the game, that is, no branching point can be reached more than once without a reset.

Output

Print the shortest time in a line.

Sample Input 1

2
1 2 2
0

Output for the Sample Input 1

2

Sample Input 2

6
2 2 1 3 2
2 4 3 5 4
2 5 5 6 6
0
0
0

Output for the Sample Input 2

24

Sample Input 3

6
3 2 100 3 10 3 10
1 4 100
1 4 10
3 5 1 5 1 6 1
0
0

Output for the Sample Input 3

243

Solution in JAVA (judge verdict: Wrong Answer)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[][] c = new int[n][n];
		int[] excess = new int[n];
		@SuppressWarnings("unchecked")
		ArrayList<Integer>[] g = new ArrayList[n];
		for (int i = 0; i < n; ++i) {
			g[i] = new ArrayList<>();
		}

		int[][] u = new int[n][n];
		for (int src = 0; src < n; ++src) {
			int k = sc.nextInt();
			if (k == 0) {
				g[src].add(0);// end?????????????§????0????????????0???????????????
				u[src][0] = 999999;
				--excess[src];
				++excess[0];
				continue;
			}
			for (int i = 0; i < k; ++i) {
				int dst = sc.nextInt();
				--dst;
				int cost = sc.nextInt();
				c[src][dst] = cost;
				c[dst][src] = -cost;
				g[src].add(dst);
				u[src][dst] = 999999;
				--excess[src];
				++excess[dst];
			}
		}

		solve(n, excess, g, c, u);
	}

	void solve(int n, int[] excess, ArrayList<Integer>[] g, int[][] c, int[][] u) {
		int[][] f = new int[n][n];
		int s = 0;
		for (int src = 0; src < n; ++src) {
			for (int dst : g[src]) {
				if (c[src][dst] > 0)
					s += c[src][dst];
			}
		}
		for (int src = 0; src < n; ++src) {
			for (int dst : g[src]) {
				if (!g[dst].contains(src)) {
					g[dst].add(src);
				}
			}
		}
		System.out.println(minCostFlow(n, u, f, c, g, excess) + s);
	}

	int minCostFlow(int n, int[][] u, int[][] f, int[][] c, ArrayList<Integer>[] g, int[] excess) {
		long[] p = new long[n];
		out: for (int i = 0; i < n; ++i) {
			if (excess[i] <= 0) {
				if (i == n - 1)
					break out;
				else
					continue out;
			}
			long[] d = new long[n];
			int[] pre = new int[n];
			Arrays.fill(pre, -1);
			Arrays.fill(d, Integer.MAX_VALUE / 16);
			d[i] = 0;
			class P implements Comparable<P> {
				int id;
				long d;

				public P(int id, long d) {
					this.id = id;
					this.d = d;
				}

				public int compareTo(P o) {
					return Long.compare(d, o.d);
				};
			}
			PriorityQueue<P> pq = new PriorityQueue<>();
			pq.add(new P(i, 0));
			while (!pq.isEmpty()) {
				P v = pq.poll();
				if (d[v.id] < v.d)
					continue;
				for (int dst : g[v.id]) {
					if (u[v.id][dst] - f[v.id][dst] <= 0)
						continue;
					if (c[v.id][dst] - (p[v.id] - p[dst]) + d[v.id] < d[dst]) {
						d[dst] = c[v.id][dst] - (p[v.id] - p[dst]) + d[v.id];
						pq.add(new P(dst, d[dst]));
						pre[dst] = v.id;
					}
				}
			}
			for (int j = 0; j < n; ++j) {
				if (excess[j] >= 0)
					continue;
				int ep = Math.min(excess[i], -excess[j]);
				for (int k = j; pre[k] != -1; k = pre[k]) {
					ep = Math.min(ep, u[pre[k]][k] - f[pre[k]][k]);
				}
				for (int k = j; pre[k] != -1; k = pre[k]) {
					f[pre[k]][k] += ep;
					f[k][pre[k]] -= ep;
				}
				excess[i] -= ep;
				excess[j] += ep;
				break;
			}

			for (int o = 0; o < n; ++o) {
				for (int l = 0; l < n; ++l) {
					if (o == l)
						continue;
					if (!(u[o][l] - f[o][l] > 0))
						continue;
					if (d[l] - d[o] - (c[o][l] - (p[o] - p[l])) > 0)
						tr(d[l] - d[o] - (c[o][l] - (p[o] - p[l])));// ?????????
				}
			}
			for (int j = 0; j < n; ++j) {
				p[j] -= d[j];
			}
			i = -1;
		}
		int ans = 0;
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				if (u[i][j] > 0)
					ans += c[i][j] * f[i][j];
			}
		}
		return ans;
	}

	// ?°??´??????¨????????????????????????????????????
	void check(int[][] c, long[] p, int[][] u, int[][] f, int n) {
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < n; ++j) {
				if (!(u[i][j] - f[i][j] > 0))
					continue;
				if (c[i][j] - (p[i] - p[j]) < 0) {
					tr("p", p);
					throw new AssertionError();
				}
			}
		}
		System.out.println("OK");
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}