Problem:
When Can We Meet? The ICPC committee would like to have its meeting as soon as
possible to address every little issue of the next contest.  
However, members of the committee are so busy maniacally developing
(possibly useless) programs that it is very difficult to arrange
their schedules for the meeting.
So, in order to settle the meeting date, the chairperson requested every
member to send back a list of convenient dates by E-mail.
Your mission is to help the chairperson, who is now dedicated to other
issues of the contest, by writing a program that 
chooses the best date from the submitted lists.
Your program should find the date convenient for the most members.
If there is more than one such day, the earliest is the best. Input The input has multiple data sets, each starting with a line
containing the number of committee members and the quorum of the meeting. N Q  Here, N , meaning the size of the committee, and Q meaning the quorum, are positive integers. N is less than 50,
and, of course, Q is less than or equal to N.    N lines follow, each describing convenient dates for a
committee
member in the following format.  M Date 1  Date 2 ... Date M   Here, M means the number of convenient dates for
the member, which is an integer greater than or equal to zero.
The remaining items in the line are his/her dates of convenience,
which are positive integers less than 100, that is, 1 means tomorrow, 
2 means the day after tomorrow, and so on.
They are in ascending order without any repetition 
and separated by a space character.
Lines have neither leading nor trailing spaces.  A line containing two zeros indicates the end of the input.  Output  For each data set, print a single line containing the date number
convenient for the largest number of committee members.
  If there is more than one
such date, print the earliest.  However, if no dates are convenient
for more than or equal to the quorum number of members, print 0 instead.  Sample Input  3 2
2 1 4
0
3 3 4 8
3 2
4 1 5 8 9
3 2 5 9
5 2 4 5 7 9
3 3
2 1 4
3 2 5 9
2 2 4
3 3
2 1 2
3 1 2 9
2 2 4
0 0  Output for the Sample Input  4
5
0
2

Solution (Python):



while True:
	n, q = map(int, input().split())
	if n == 0: break
	f, ma = [0]*120, 0
	for i in range(n):
		ds = list(map(int, input().split()))
		for d in ds[1:]:
			f[d] += 1
			ma = max(ma, d)
	ans = 0
	for i in range(1, ma+1):
		if f[i] >= q and f[i] > f[ans]: ans = i
	print(ans)

Solution (Python):
import collections

while True:
    N, Q = map(int, input().split())
    if N == 0 and Q == 0:
        break
    D = []
    for _ in range(N):
        i = list(map(int, input().split()))
        del i[0]
        [D.append(ii) for ii in i]
    if not len(D) == 0:
        c = collections.Counter(D)
        cc = c.most_common()
        
        if cc[0][1] >= Q:
            ans = 1000000
            tmp = cc[0][1]
            for i in range(len(cc)):
                if cc[i][1] == tmp:
                    ans = min(cc[i][0], ans)
            print(ans)
        else:
            print(0)
    else:
        print(0)

Solution (Python):
while True:
    (N, Q) = [int(x) for x in input().split()]
    if N==0 and Q==0:
        exit()
    Date = [0]*100
    meeting_day = 0
    member_num = 0
    for i in range(N):
        M = [int(x) for x in input().split()]
        for j in range(1, M[0]+1):
            Date[M[j]-1] += 1
    for i in range(100):
        if Date[i] >= Q and Date[i]>member_num:
            meeting_day = i+1
            member_num = Date[i]
    print(meeting_day)

Solution (Python):



import os
import sys
import itertools
import math
from collections import Counter, defaultdict

class Main(object):
    
    def __init__(self):
        pass

    def solve(self):
        '''
        insert your code
        '''
        while True:
            n, q = map(int, raw_input().split())
            if n == 0 and q == 0:
                break
            c = Counter()
            for i in range(n):
                line = map(int, raw_input().split())
                m = line[0]
                for j in range(1, m+1):
                    c[line[j]] += 1
            
            max_key = None
            max_value = -1
            for k, v in c.items():
                if v > max_value or (v == max_value and k < max_key):
                    max_key = k
                    max_value = v
            if max_value >= q:
                print max_key
            else:
                print 0
        return None

if __name__ == '__main__':
    m = Main()
    m.solve()

Solution (Python):



while True:
    N,Q = map(int,input().split(" "))
    if N == 0 and Q == 0:
        break
    dateList = [0 for i in range(101)]
    for i in range(N):
        date = list(map(int,input().split()))
        for d in date[1:]:
            dateList[d] += 1
    max_participant = max(dateList)
    if max_participant < Q:
        print(0)
    else:
        for i in range(101):
            if max_participant == dateList[i]:
                print(i)
                break

Solution (Python):
while 1:
	n, q = map(int, raw_input().split(" "))
	if (n, q) == (0,0):
		break
	date_total = [0 for i in range(100)]
	for i in range(n):
		date = map(int, raw_input().split(" "))
		for j in range(1,date[0]+1):
			date_total[date[j]] += 1
	best = max(date_total)
	if best < q:
		print 0
	else :
		print date_total.index(best)

Solution (Python):



(N, Q) = map(int, raw_input().split())
while N!=0 and Q!=0:
    H=[0]*101
    for i in range(N):
        q = map(int, raw_input().split())
        for j in range(1, len(q)):
            H[q[j]] += 1
    if Q <= max(H):
        print H.index(max(H))
    else:
        print 0

    (N, Q) = map(int, raw_input().split())

Solution (Python):
while True:
    n, q = map(int, input().split())
    if n == 0: break
    ls = []
    for i in range(n):
        ls.append(list(map(int, input().split())))

    max_q = 0
    date = 0
    for i in range(1, 101):
        count = 0
        for j in range(len(ls)):
            if i in ls[j][1:]:
                count += 1
                if count > max_q:
                    max_q = count
                    date = i
    if max_q < q:
        print(0)
    else:
        print(date)

Solution (Python):
while True:
    N, Q = map(int, raw_input().split())
    if (N, Q) == (0, 0):
        break
    count = [Q-1] + [0] * 100
    for _ in xrange(N):
        for d in map(int, raw_input().split())[1:]:
            count[d] += 1
    print count.index(max(count))

Solution (Python):





while True:
    amount, require = map(int, input().split())
    if amount == require == 0: break

    dates = []

    for _ in range(amount):
        date = list(map(int, input().split()))
        if not date.pop(0) == 0:
            
            dates.append(date)
        else:
            dates.append([0])

    
    max_d = 0
    for date in dates:
        if max(date) >= max_d:
            max_d = max(date)

    meet_count = 0
    can_meet = False
    x_day = 0
    for day in range(1, max_d+1):
        temp = 0
        for date in dates:
            
            if day in date:
                
                temp += 1

            
        if temp >= require:
            
            if temp > meet_count:
                
                x_day = day
                meet_count = temp
                can_meet = True

    if can_meet:
        print(x_day)
    else:
        print(0)

Solution (Python):
from collections import Counter

while True:
	n, q = map(int, input().split())
	if (n, q) == (0, 0):
		break
	r = []
	for i in range(n):
		d = list(map(int, input().split()))
		if len(d) > 1:
			r += d[1:]
	r = Counter(r).most_common()
	if len(r) > 0 and r[0][1] >= q:
		print(r[0][0])
	else:
		print(0)

Solution (Python):
while True:
    n, q = map(int, raw_input().split())

    if n == 0:
        break

    m = {}

    for _ in range(n):
        d = map(int, raw_input().split())
        for i in range(1, len(d)):
            if d[i] in m:
                m[d[i]] += 1
            else:
                m[d[i]] = 1

    maxk = 0
    maxv = 0
    for k, v in m.items():
        if v >= q and maxv < v:
            maxk = k
            maxv = v

    print maxk

Solution (Python):
while True:
    n, q = map(int, input().split())
    if n == 0: break
    ls = []
    for i in range(n):
        ls.append(list(map(int, input().split())))

    max_q = 0
    date = 0
    for i in range(1, 101):
        count = 0
        for j in range(len(ls)):
            if i in ls[j][1:]:
                count += 1
                if count > max_q:
                    max_q = count
                    date = i
    if max_q < q:
        print(0)
    else:
        print(date)

Solution (Python):
cond = [int(n) for n in input().split(" ")]
while True:
    answer = [101,0]
    case = []
    sets = set()
    for c in range(cond[0]):
        case.append([int(n) for n in input().split(" ")[1:]])
        sets = sets | {n for n in case[-1]} 
        
    else:
        for num in sets:
            disp = [num,0]
            for c in case:
                if num in c:
                    disp[1] += 1
            else:
                
                if (disp[1] >= cond[1] and disp[1] > answer[1] ) or (disp[1] == answer[1] and disp[0] < answer[0]):
                    answer = disp
        else:
            if answer[0] == 101:
                print(0)
            else:
                print(answer[0])
            cond = [int(n) for n in input().split(" ")]
            if cond == [0,0]:
                break

Solution (Python):
while True:
  N,Q = map(int, raw_input().split())
  if N==Q==0: break
  schedule = [0] * 101
  for _ in xrange(N):
    ipt = map(int, raw_input().split())
    for i in xrange(1, ipt[0]+1):
      schedule[ipt[i]] += 1
  bestday = 0
  bestnum = 0
  for i in xrange(1, 101):
    if bestnum < schedule[i]:
      bestday = i
      bestnum = schedule[i]
  if bestnum < Q:
    bestday = 0
  print bestday

Solution (Python):
while 1:
    n, q = map(int, raw_input().split())
    if n==0:
        break
    d = [0]*101
    for i in xrange(n):
        D = map(int, raw_input().split())
        for j in xrange(D[0]):
            d[D[j+1]] += 1

    v = max(d)
    if v < q:
        print 0
    else:
        print d.index(v)

Solution (Python):
import collections
from operator import itemgetter

while True:
    max_value = 1
    num_people,num_border = map(int,raw_input().split())
    if (num_people == 0) and (num_border == 0):
        break
    list2 = []
    for i in range(num_people):
        line = raw_input()
        list1 = line.split()
        list1 = [int(s) for s in list1]
        if list1[0] != "0":
            del list1[0]
            list2.extend(list1)
    counter = collections.Counter(list2)
    if counter == {}:
        print "0"
    s = sorted(counter.items(), key=itemgetter(0))
    for key,value in sorted(s,key= itemgetter(1),reverse = True):
        if value >= num_border:
            print key
            break
        else:
            print "0"
            break

Solution (Python):

while True:
    tmp = input().split(' ')
    n = int(tmp[0])
    q = int(tmp[1])
    if n == 0 and q == 0:
        break
    a = []
    d = [0]*100
    for i in range(n):
        a = input().split(' ')
        for j in range(int(a[0])):
            qq = int(a[ j+1 ])
            d[qq] = d[qq] + 1
    mx = max(d)
    if mx < q:
        print(0)
    else:
        for i in range(len(d)):
            if mx == d[i]:
                print( i )
                break

Solution (Python):
from collections import Counter
while True:
    N,Q = map(int,input().split())
    if N == 0: break
    ctr = Counter()
    for i in range(N):
        src = list(map(int,input().split()))
        ctr.update(src[1:])
    ans_n = 0
    ans_d = -1
    for d,n in sorted(ctr.items()):
        if n > ans_n:
            ans_n = n
            ans_d = d
    if ans_n >= Q:
        print(ans_d)
    else:
        print(0)

Solution (Python):
while True:
    N, Q = map(int, raw_input().split())

    if N == 0 and Q == 0:
        break

    t = [map(int, raw_input().split())[1:] for _ in range(N)]
    table = [[i, 0] for i in range(101)]

    for i in t:
        for j in i:
            table[j][1] += 1

    table.sort(reverse=True, key=lambda x: x[1])

    print table[0][0] if table[0][1] >= Q else 0

Solution (Python):
while True:
    n, q = map(int, input().split())
    if n == 0:
        break

    data = [list(map(int, input().split())) for i in range(n)]
    for li in data:
        if li[0] > 0:
            del(li[0])

    q -= 1
    day = 0
    for d in range(1,100):
        cnt = 0
        for li in data:
            if d in li:
                cnt += 1
        if cnt > q:
            q = cnt
            day = d

    print(day)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**3
eps = 1.0 / 10**10
mod = 10**9+7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []

    while True:
        n,q = LI()
        if n == 0:
            break
        a = [LI() for _ in range(n)]
        d = collections.defaultdict(int)
        for c in a:
            for cc in c[1:]:
                d[cc] += 1
        t = sorted([[-v,k] for k,v in d.items() if v >= q])
        if len(t) == 0:
            rr.append(0)
        else:
            rr.append(t[0][1])

    return '\n'.join(map(str, rr))


print(main())

Solution (Python):
while 1:
    n,q = map(int, raw_input().split())
    if n == 0 and q == 0: break
    dates = [0 for i in xrange(100)]
    for i in xrange(n):
        cDate = map(int, raw_input().split())
        if cDate[0] != 0:
            for j in xrange(cDate[0]):
                dates[cDate[j+1]] += 1
    nom = max(dates)
    if nom >= q:
        for i in xrange(len(dates)):
            if dates[i] == nom:
                print i
                break
    else:
        print 0

Solution (Python):
import sys
import collections

while True:
    N, Q = map(int, sys.stdin.readline().split())
    if N == 0:
        break
    counter = collections.Counter()
    for i in xrange(N):
        date = map(int, sys.stdin.readline().split())
        if date[0] != 0:
            counter.update(date[1:])
    
    if len(counter) == 0:
        print 0
        continue
    
    result = counter.most_common()
    largest = result[0][1]
    
    ret = -1
    if largest < Q:
        print 0
    else:
        smallest = 100
        for n, c in result:
            if c < largest:
                break
            if n < smallest:
                smallest = n
        print smallest

Solution (Python):
while True:
	n, q = map(int, raw_input().split())
	if n == q == 0: break
	counts = [0] * 101
	maxd = 0
	for i in xrange(n):
		data = map(int, raw_input().split()[1:])
		for d in data:
			counts[d] += 1
			maxd = max(maxd, counts[d])
	if q <= maxd:
		print counts.index(maxd)
	else:
		print 0

Solution (Python):
while True:
    N,Q=map(int,raw_input().split())
    if N==Q==0:break
    T=[0]*101
    for i in range(N):
        L=map(int,raw_input().split())
        for j in L[1:]:
            T[j]+=1
    m=max(T)
    print 0 if m<Q else T.index(m)

Solution (Python):

while 1:
  day = [0] * 100
  n, q = map(int, raw_input().split())
  if not n:
    break
  for i in range(n):
    data = map(int, raw_input().split())
    for j in range(1, len(data)):
      day[data[j] - 1] += 1
  m = max(day) 
  if m >= q:
    for i in range(0, len(day)):
      if day[i] == m:
        print i + 1
        break
  else:
    print 0

Solution (Python):
while 1:
	N,Q = map(int,raw_input().split())
	if N == 0: break
	C = [0]*100
	for _ in range(N):
		D = map(int,raw_input().split())
		for i in D[1:]:
			C[i] += 1
	print C.index(max(C)) if max(C) >= Q else 0

Solution (Python):
while True:
    N, Q = map(int, input().split())
    if N == 0 and Q == 0:
        break
    counts = [0 for i in range(100)]
    for i in range(N):
        tmp = list(map(int, input().split()))
        M = tmp[0]
        for i in range(M):
            counts[tmp[i + 1] - 1] += 1
            
    max_count = max(counts)
    if max_count < Q:
        print(0)
    else:
        for i in range(100):
            if counts[i] == max_count:
                print(i + 1)
                break

Solution (Python):
def get_input():
    data_list = input().split()
    for i in range(len(data_list)):
        data_list[i] = int(data_list[i])
    return data_list

def make_dict(day_dict, data_list):
    candidate_num =data_list[0] + 1
    for i in range(1, candidate_num):
        data = data_list[i]
        if data in day_dict:
            day_dict[data] += 1
        else:
            day_dict[data] = 1
    return day_dict

def decide_open_day(day_dict, quorum):
    max_num = max(day_dict.values())
    if max_num < quorum:
        return 0
    max_k_list = [kv[0] for kv in day_dict.items() if kv[1] == max_num]
    open_day = min(max_k_list)
    return open_day


if __name__ == "__main__":
    while True:
        member_num, quorum = get_input()
        if member_num == 0:
            break
        day_dict = dict()
        for i in range(member_num):
            data_list = get_input()
            day_dict = make_dict(day_dict, data_list)
        if day_dict == {}: 
            print(0)
        else:
            open_day = decide_open_day(day_dict, quorum)
            print(open_day)

Solution (Python):
while 1:
    n, q = map(int, input().split())
    if n==0: break
    a = []
    for _ in range(n):
        a = a + list(map(int, input().split()))[1:]
    max = 0
    d = 0
    for i in range(1, 101):
        if a.count(i) >= q and max < a.count(i):
            max = a.count(i)
            d = i
    print(d)

Solution (Python):
while(True):
	N, Q = map(int, input().split())
	if (N+Q) == 0:break
	D = list(list(int(i) for i in input().split()) for i in range(N))
	D_i  = [1]*N
	answer = 0
	now_max = Q -1
	for i in range(1,100):
		member = 0
		for j in range(N):
			if D[j][0] < D_i[j]:
				continue
			if D[j][D_i[j]] == i:
				member += 1
				D_i[j] += 1
		if member > now_max :
			answer = i
			now_max = member
		if member == N:
			answer = i
			break
	print(answer)

Solution (Python):

while(1):
    N,Q=tuple(map(int,input().split()))
    if not(N or Q):
        break
    data=[]
    temp=[]
    for i in range(N):
        temp=list(map(int,input().split()))
        temp.pop(0)
        data.append(temp) 

    sum=0
    max=0
    index=0
    for i in range(100):
        sum=0
        for j in range(N):
            sum=sum+int((i in data[j]))
            if sum >= Q and sum > max:
                max = sum
                index=i

    print(index)

Solution (Python):
from collections import defaultdict
while True:
  n, q = list(map(int, input().split()))
  if n==0:
    break

  t = defaultdict(int)
  for i in range(n):
    d = list(map(int, input().split()))
    for j in range(1, len(d)):
      t[d[j]]+=1
  tt = sorted(t.items())
  tt.sort(key=lambda x: -x[1])
  if len(tt)==0 or tt[0][1]<q:
    print(0)
  else:
    print(tt[0][0])

Solution (Python):
from collections import defaultdict
def main(n, m):
    d = defaultdict(int)
    for _ in range(n):
        M = list(map(int, input().split()))
        for num, i in enumerate(M):
            if num == 0: continue
            d[i] += 1
    ans = 10**20
    for key, value in d.items():
        if value > m:
            ans = key
            m = value
        elif value == m:
            ans = min(ans,key)
    if ans == 10 ** 20:
        ans = 0
    print(ans)

while 1:
    n, m = map(int, input().split())
    if n == m == 0:
        break
    main(n,m)

Solution (Python):
while True:
    n, q = map(int, input().split())
    if n == 0:
        break
    count = [0] * 101
    for i in range(n):
        dates = list(map(int, input().split()))
        for d in range(1, len(dates)):
            count[dates[d]] += 1
    ind = 0
    max_ = 0
    for i in range(101):
        if max_ < count[i]:
            ind = i
            max_ = count[i]
    if max_ < q:
        print(0)
    else:
        print(ind)

Solution (Python):
while True:
  N,Q = map(int,input().split())
  if N+Q==0:
    break
  date = [0 for i in range(1000)]
  for i in range(N):
    M = list(map(int,input().split()))
    for i in M[1:]:
      date[i]+=1
  a = max(date)
  if a>=N:
    for i in range(1000):
      if N==date[i]:
        print(i)
        break
  elif N>a>=Q:
    for i in range(1000):
      if a==date[i]:
        print(i)
        break
  else:
    print(0)

Solution (Python):
def main():
    while True:
        n,q = map(int,input().split())
        if not n and not q:
            return
        res = [0 for _ in range(1000)]
        for _ in range(n):
            for m in list(map(int,input().split()))[1::]:
                res[m] += 1
        ans = max(res)
        if ans < q:
            print(0)
        else:
            print(res.index(ans))

if __name__ == '__main__':
    main()

Solution (Python):
while True:
    n, q = map(int, input().split())
    if n == 0: break

    c_dates = []

    for i in range(n):
        data = input().split()
        del data[0]
        for j in range(len(data)):
            data[j] = int(data[j])
        c_dates = c_dates + data
        
    c_dates.sort()
    
    

    c_day = 0
    c_num = 1
    max_num = 0
    

    if len(c_dates) == 1 and q == 1:
        print(c_dates[0])
        continue
    
    for i in range(len(c_dates) - 1):
        if c_dates[i] == c_dates[i + 1] and i != len(c_dates) - 1:
            c_num += 1
            if i != len(c_dates) - 2: continue
        if c_num >= q and c_num > max_num:
            c_day = c_dates[i]
            
            
            max_num = c_num
            c_num = 1
        else:
            c_num = 1

    print(c_day)

Solution (Python):

while 1:
    date=[[0,i] for i in range(101)]
    n,q=map(int,input().split())
    if n==0:
        break
    for i in range(n):
        t = list(map(int,input().split()))
        for num in t[1:]:
            date[num][0]+=1
    ans = sorted(date,key=lambda x:(-x[0],x[1]))[0]
    print(ans[1] if ans[0]>=q else 0)