Klingon Course Levels

No attempts yetTime limit1sMemory limit128 MB

Problem

At a Latin American high school, the Klingon language has become so popular that many students started learning this constructed language on their own. Seeing this, the administration decided to offer formal Klingon courses. Because students start from very different levels, two course levels are offered: basic and advanced.

The school is organized into several divisions, and every student belongs to exactly one division. Because of scheduling conflicts, students from different divisions cannot share the same Klingon course. To be fair, both the basic and the advanced course must be offered to every division at the same level of difficulty.

Each division is therefore split into two groups: one group takes the basic course and the other takes the advanced course. A division may also have no students in one of the two levels.

Every student has already taken a Klingon placement test and received an integer grade between $0$ and $1000$ inclusive. The administration picks a single threshold $T$: every student whose score is greater than or equal to $T$ is assigned to the advanced level, and every student whose score is less than $T$ is assigned to the basic level.

They want a threshold that splits the divisions as evenly as possible. For a fixed $T$, each division contributes the absolute difference between the number of its basic-level students and the number of its advanced-level students. The accumulated difference is the sum of these per-division differences.

For example, suppose the school has two divisions. In the first there are $10$ basic and $20$ advanced students; in the second there are $17$ basic and $15$ advanced students. Then the accumulated difference is $|10 - 20| + |17 - 15| = 12$.

Choose $T$ so that the accumulated difference is minimized.

Input

The input contains several test cases.

Each test case begins with a line containing one integer $N$ ($1 \le N \le 10^4$), the number of divisions in the school. Then $2N$ lines follow, describing the divisions two lines at a time. For division $i$, the first of its two lines contains one integer $K_i$ ($1 \le K_i \le 10^4$), the number of students in that division; the second line contains $K_i$ integers between $0$ and $1000$ inclusive, separated by single spaces, the scores of those students. Within a single test case the total number of students (the sum of all $K_i$) does not exceed $10^5$.

The last test case is followed by a line containing a single $0$.

Output

For each test case, print a single line with one integer: the minimum possible accumulated difference when $T$ is chosen optimally.