Under construction

Still being built. Things may change

Election

Time limit1sMemory limit128 MB

Summary
Simulate instant-runoff voting over at most 5 candidates and 20 preference groups, eliminating the lowest scorer each round.
Level

Easy3 of 10

Topics
Simulation, Implementation, Array
Solved
No attempts yet

Problem

Given the voting preferences of a population of MM people, determine the winner of an election among NN candidates numbered 1,…,N1, \dots, N. The MM people are partitioned into GG "groups," where all members of a group share the same voting preferences. Each group's preferences are given by listing the candidates from most preferred to least preferred. The result is determined by an instant-runoff voting procedure.

In this method, the current first choices of the MM people are counted, and the candidate with the fewest votes is eliminated. If several candidates tie for the fewest votes, the highest-numbered among them is eliminated. The eliminated candidate is then removed from every person's preference list, and again the candidate with the fewest votes is eliminated. This repeats until only one candidate remains, who is the winner.

Input

The input contains multiple test cases. Each test case begins with a line containing two integers GG and NN (2≤N≤52 \le N \le 5, 1≤G≤201 \le G \le 20). The next GG lines each have the form Mi ai1 ai2 … aiNM_i\ a_{i1}\ a_{i2}\ \dots\ a_{iN}, where 1≤Mi≤201 \le M_i \le 20 and ai1,…,aiNa_{i1}, \dots, a_{iN} is a permutation of 1,…,N1, \dots, N. Here MiM_i is the number of people in the ii-th group, and ai1,…,aiNa_{i1}, \dots, a_{iN} orders the NN candidates from most to least preferred for that group. The end of input is marked by a test case with G=N=0G = N = 0, which should not be processed.

Output

For each test case, print the winner of the election on its own line.

Examples1

  1. Example 1

    Input
    3 4
    10 1 4 2 3
    15 3 2 1 4
    12 4 3 2 1
    3 2
    10 1 2
    10 1 2
    20 2 1
    0 0
    
    Expected output
    4
    1