Problem:
 Score : 700 points    Problem Statement Takahashi is hosting an sports meet.
There are N people who will participate. These people are conveniently numbered 1 through N .
Also, there are M options of sports for this event. These sports are numbered 1 through M .
Among these options, Takahashi will select one or more sports (possibly all) to be played in the event.  Takahashi knows that Person i 's j -th favorite sport is Sport A_{ij} .
Each person will only participate in his/her most favorite sport among the ones that are actually played in the event, and will not participate in the other sports.  Takahashi is worried that one of the sports will attract too many people.
Therefore, he would like to carefully select sports to be played so that the number of the participants in the sport with the largest number of participants is minimized.
Find the minimum possible number of the participants in the sport with the largest number of participants.      Constraints  1 \leq N \leq 300  1 \leq M \leq 300  A_{i1} , A_{i2} , ... , A_{iM} is a permutation of the integers from 1 to M .         Input Input is given from Standard Input in the following format:  N  M  A_{11}  A_{12}  ...  A_{1M}  A_{21}  A_{22}  ...  A_{2M}  :  A_{N1}  A_{N2}  ...  A_{NM}       Output Print the minimum possible number of the participants in the sport with the largest number of participants.        Sample Input 1 4 5
5 1 3 4 2
2 5 3 1 4
2 3 1 4 5
2 5 4 3 1      Sample Output 1 2  Assume that Sports 1 , 3 and 4 are selected to be played. In this case, Person 1 will participate in Sport 1 , Person 2 in Sport 3 , Person 3 in Sport 3 and Person 4 in Sport 4 .
Here, the sport with the largest number of participants is Sport 3 , with two participants.
There is no way to reduce the number of participants in the sport with the largest number of participants to 1 . Therefore, the answer is 2 .       Sample Input 2 3 3
2 1 3
2 1 3
2 1 3      Sample Output 2 3  Since all the people have the same taste in sports, there will be a sport with three participants, no matter what sports are selected.
Therefore, the answer is 3 .

Solution (Python):
from collections import defaultdict
N,M=map(int,input().split())
A=[list(map(int,input().split())) for _ in range(N)]
ans = N
ss=set()
front=[0]*N
while len(ss)<M-1:
    cnts = defaultdict(lambda: 0)
    for i,f in enumerate(front):
        cnts[A[i][f]]+=1
    
    maxi, maxc = max(cnts.items(), key=lambda p: p[1])
    ans = min(ans, maxc)
    ss.add(maxi)
    
    for i in range(N):
        while A[i][front[i]] in ss:
            front[i]+=1
print(ans)

Solution (Python):
import collections
n,m=map(int,input().split())
A=[]
for i in range(n):
  A.append([int(_) for _ in input().split()])
D=dict()
for i in range(m):
  D[i+1]=1
ans=n
for _ in range(m):
  p=[]
  for i in range(n):
    for j in range(m):
      if D[A[i][j]]==1:
        p.append(A[i][j])
        break
  pp=collections.Counter(p).most_common()
  a,b=pp[0]
  ans=min(ans,b)
  D[a]=0
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline

import numpy as np

N,M = map(int,input().split())
A = np.array([input().split() for _ in range(N)],dtype=np.int32)

"""
最人気を消去しない → それが採用
消去 → 消去
"""

answer = 10**10
for _ in range(M):
    counter = np.bincount(A[:,0])
    i = counter.argmax()
    max_cnt = counter[i]
    if answer > max_cnt:
        answer = max_cnt
    A = A[A != i].reshape(N,-1)

print(answer)

Solution (Python):
from collections import defaultdict
N, M = map(int, input().split())
A = [[] for i in range(N)]
for i in range(N):
    A[i] = list(map(lambda x: int(x) - 1, input().split()))


def calc(k):
    D = [0] * N
    S = [True] * M
    K = M
    while K > 0:
        d = defaultdict(int)
        for i in range(N):
            while D[i] < M and not S[A[i][D[i]]]:
                D[i] += 1
            d[A[i][D[i]]] += 1
        for p, v in d.items():
            if v > k:
                S[p] = False
                K -= 1
                break
        else:
            return True
    return False


l, r = 0, N
while r - l > 1:
    mid = (l + r) // 2
    if calc(mid):
        r = mid
    else:
        l = mid

print(r)

Solution (Python):
from collections import Counter

N, M = map(int, input().split())
Ass = [tuple(map(lambda x: int(x)-1, input().split())) for _ in range(N)]

def isOK(k):
    iAs = [0] * N
    isAvailables = [True] * M
    numSport = M
    while numSport > 0:
        sports = []
        for i in range(N):
            while iAs[i] < M and not isAvailables[Ass[i][iAs[i]]]:
                iAs[i] += 1
            sports.append(Ass[i][iAs[i]])
        cnt = Counter(sports)
        isOK = True
        for sp, num in cnt.items():
            if num > k:
                isAvailables[sp] = False
                numSport -= 1
                isOK = False
        if isOK:
            return True
    return False

ng, ok = 0, N
while abs(ok-ng) > 1:
    mid = (ng+ok) // 2
    if isOK(mid):
        ok = mid
    else:
        ng = mid

print(ok)

Solution (Python):
from collections import Counter, deque

n, m = map(int, input().split())
likes = []
for i in range(n):
    likes.append(deque(map(int, input().split())))

removed = set()
ans = n

for i in range(m):
    count = []
    for qu in likes:
        while qu and qu[0] in removed:
            qu.popleft()
        count.append(qu[0])
    v, c = Counter(count).most_common(1)[0]
    ans = min(ans, c)
    if c == 1:
        break
    removed.add(v)

print(ans)

Solution (Python):
def count(A, idx, N):
    cnt = {}
    for i in range(N):
        if A[i][idx[i]] not in cnt:
            cnt[A[i][idx[i]]] = 0
        cnt[A[i][idx[i]]] += 1
    return cnt


def main():
    N, M = map(int, input().split())
    A = [list(input().split()) for _ in range(N)]
    ans = float("inf")
    idx = [0] * N
    used = set()
    for _ in range(M):
        cnt = count(A, idx, N)
        mx = (0, -1)
        for x in cnt:
            mx = max(mx, (cnt[x], x))
        ans = min(ans, mx[0])
        used.add(mx[1])
        for i in range(N):
            while idx[i] < M and A[i][idx[i]] in used:
                idx[i] += 1
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import sys
F = sys.stdin
def single_input(): return F.readline().strip("\n")
def line_input(): return F.readline().strip("\n").split()

def solve():
    N, M = map(int, input().split())
    A = [None] * N
    for i in range(N):
        A[i] = list(map(int, line_input()))
    pointer = [0] * N
    used = set()
    minmax = 500
    for m in range(M):
        num_people = [0] * M
        maxgather = (0, 0)
        for n in range(N):
            num_people[A[n][pointer[n]] - 1] += 1
            if num_people[A[n][pointer[n]] - 1] > maxgather[0]:
                maxgather = (num_people[A[n][pointer[n]] - 1], A[n][pointer[n]])
        used |= {maxgather[1]}
        minmax = min(minmax, maxgather[0])
        for n in range(N):
            while pointer[n] < M:
                if A[n][pointer[n]] in used:
                    pointer[n] += 1
                else: break
    print(minmax)
    return 0
  
if __name__ == "__main__":
    solve()

Solution (Python):
from collections import Counter
from copy import deepcopy
N, M = map(int, input().split())
A = [list(map(int, input().split()))[::-1] for _ in range(N)]
ok = N+1
ng = 0
def check(med, B):
    NG = set()
    for _ in range(N):
        CS = Counter([b[-1] for b in B])
        flag = True
        for k, v in CS.items():
            if v > med:
                NG.add(k)
                flag = False
        if flag:
            return True
        if len(NG) == M:
            return False
        for b in B:
            for _ in range(M):
                if b[-1] not in NG:
                    break
                b.pop()
while abs(ok - ng) > 1:
    B = deepcopy(A)
    med = (ok + ng)//2
    if check(med, B):
        ok = med
    else:
        ng = med

print(ok)

Solution (Python):
from collections import defaultdict
from operator import itemgetter
N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]
ans = float("inf")
for _ in range(M):
    L = []
    cnt = defaultdict(int)
    for a in A:
        if len(a)!=0:
            a0 = a[0]
            L.append(a0)
            cnt[a0] += 1
    ama, ma = max(cnt.items(), key=itemgetter(1))
    ans = min(ans, ma)
    for a in A:
        a.remove(ama)
print(ans)

Solution (Python):
import sys, collections
def input():
	return sys.stdin.readline()[:-1]

n, m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]

def calc(s):
	c = collections.Counter()
	for i in range(n):
		for j in range(m):
			if a[i][j] not in s:
				c[a[i][j]] += 1
				break
	tmp = [-1, -1]
	for k, v in c.items():
		if v > tmp[1]:
			tmp = [k, v]
	del c
	return tmp

ans, boycott = n, set()

for _ in range(m-1):
	res = calc(boycott)
	ans = min(ans, res[1])
	boycott.add(res[0])

print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
N,M=map(int,input().split())
A=[[int(i)-1 for i in input().split()] for i in range(N)]
L=set()

def f(L):
    K=[0]*M
    for i in range(N):
        for j in range(M):
            if A[i][j] not in L:
                K[A[i][j]]+=1
                break
    a=0
    b=0
    for i in range(M):
        if a<K[i]:
            a=K[i]
            b=i
    return a,b
ans=10**9
for i in range(M):
    a,b =f(L)
    ans=min(ans,a)
    L.add(b)
    
print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
from collections import Counter
n,m=map(int,input().split())
A,B=[],[]
for _ in range(n):
    A.append(tuple(map(lambda x:int(x)-1,input().split())))
    B.append(A[-1][0])
delete=[False]*m
ans=n
idx=[0]*n
for _ in range(m-1):
    c=Counter(B)
    sport,num=c.most_common()[0]
    ans=min(ans,num)
    delete[sport]=True
    for i in range(n):
        while delete[A[i][idx[i]]]:
            idx[i]+=1
        B[i]=A[i][idx[i]]
print(ans)

Solution (Python):
import sys
from collections import Counter
input = sys.stdin.readline

n,m = map(int,input().split())
a = []
b = []
for i in range(n):
    a.append(tuple(map(lambda x:int(x)-1,input().split())))
    b.append(a[i][0])

used = [False]*m
idx = [0]*n

res = n
for i in range(m-1):
    ctr = Counter(b)
    d = ctr.most_common()
    p,num = d[0]
    res = min(res,num)
    used[p] = True

    for j in range(n):
        while used[a[j][idx[j]]]:
            idx[j] = idx[j] + 1
        b[j] = a[j][idx[j]]

print(res)

Solution (Python):
n, m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]

s = set([])
ans = n
for _ in range(m):
    memo = [0] * (m + 1)
    flag = [0] * (n + 1)
    for j in range(m):
        for i in range(n):
            if flag[i] == 0 and a[i][j] not in s:
                memo[a[i][j]] += 1
                flag[i] = 1
    idx = 0
    mmax = 0
    for i, me in enumerate(memo):
        if me > mmax:
            idx = i
            mmax = me

    ans = min(ans, mmax)
    s.add(idx)

print(ans)

Solution (Python):
N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]

ans = N
for i in range(M - 1):
    p = [0] * M
    for j in range(N):
        p[A[j][0] - 1] += 1
    mp = max(p)
    MM = p.index(mp) + 1
    ans = min(ans, mp)
    for j in range(N):
        A[j].remove(MM)

print(ans)

Solution (Python):
n,m=map(int,input().split())
k=[[0]*m for i in range(n)]
for i in range(n):
    k[i]=list(map(int,input().split()))
ans=1000
for i in range(m-1):
    ma=[0]*m
    mi=-1
    for j in range(n):
        ma[k[j][0]-1]+=1
    if ans>max(ma):
        ans=max(ma)
    mi=ma.index(max(ma))+1
    for j in range(n):
        k[j].remove(mi)
if m==1:
    print(n)
else:
    print(ans)

Solution (Python):
from collections import deque
N,M=map(int,input().split())
c=[deque([int(i) for i in input().split()]) for i in range(N)]

G=[0]*M
B=[-1]*N
l=-1
ans=float("inf")
for i in range(M):
  for j in range(N):
    S=len(c[j])
    if B[j]==l:
      for k in range(S):
        s=c[j].popleft()
        if G[s-1]>=0:
          G[s-1]+=1
          B[j]=s-1
          break
  a,l=0,0
  for j in range(M):
    if G[j]>a:
      a,l=G[j],j
  ans=min(ans,a)
  G[l]=-1
print(ans)

Solution (Python):





"""
AGC018 B
"""

from collections import Counter

n, m = map(int, input().split())
alili = [list(map(int, input().split())) for i in range(n)]

cnt = Counter()

for i in range(n):
    cnt[alili[i][0]] += 1

ans = cnt.most_common(1)[0][1]
delset = set()

for j in range(m-1):
    x, tmp = cnt.most_common(1)[0]
    if tmp < ans:
        ans = tmp
    del cnt[x]
    delset.add(x)
    for i in range(n):
        if alili[i][0] == x:
            while alili[i][0] in delset:
                del alili[i][0]
            cnt[alili[i][0]] += 1

print(ans)

Solution (Python):
n, m=map(int, raw_input().split())
l=[map(int, raw_input().split()) for i in range(n)]
last, first=0, n
while last+1!=first:
	mid=last+(first-last)/2
	erased=[False]*(m+1)
	bucket=[[] for i in range(m+1)]
	for i in range(n):
		bucket[l[i][0]].append((i, 0))
	ok=True
	while True:
		any_change=False
		for i in range(m+1):
			if len(bucket[i])>mid:
				erased[i]=True
				for which, index in bucket[i]:
					while index<m and erased[l[which][index]]:
						index+=1
					if index==m:
						ok=False
						break
					bucket[l[which][index]].append((which, index))
				bucket[i]=[]
				any_change=True
		if not any_change or not ok:
			break
	if ok:
		first=mid
	else:
		last=mid
print first

Solution (Python):
n,m = map(int,input().split())
ans = 10 ** 9
lis = [list(map(int,input().split())) for i in range(n)]
for i in range(m):
  num = [0 for i in range(m)]
  for j in range(n):num[lis[j][0]-1] += 1
  ans = min(max(num),ans)
  for k in range(n):lis[k].remove(num.index(max(num))+1)
print(ans)

Solution (Python):
N,M = map(int,input().split(" "))
A = []
for i in range(N):
    A.append(list(map(int,input().split(" "))))

ans = float('inf')
sport_num = M
adopt_sports = [1] * M
for it in [0]*M:
    participate_sports = [0] * M
    for n in range(N):
        for m in range(M):
            if adopt_sports[A[n][m]-1] == 1:
                participate_sports[A[n][m]-1] += 1
                break

    max_v = 0
    max_idx = -1
    for m in range(M):
        if participate_sports[m] > max_v:
            max_idx = m
            max_v = participate_sports[m]

    ans = min(max_v,ans)
    adopt_sports[max_idx] = 0

print(ans)

Solution (Python):
from collections import Counter
N, M = map( int, input().split())
A = [ [] for _ in range(N)]
for i in range(N):
    A[i] = list( map( int, input().split())) 
V = [1]*(M+1)
ans = N
for _ in range(M-1):
    W = []
    for i in range(N):
        while True:
            if V[A[i][0]] == 1:
                W.append(A[i][0])
                break
            else:
                A[i].pop(0)
    a, c = Counter(W).most_common()[0]
    ans = min( ans, c)
    V[a] = 0
print(ans)

Solution (Python):
from collections import Counter
from operator import itemgetter

def sub(prefers, canceled):
    res = Counter()
    for pref in prefers:
        p = pref[-1]
        while p in canceled:
            pref.pop()
            p = pref[-1]
        res[p] += 1
    return max(res.items(), key=itemgetter(1))

def solve(m, prefers):
    canceled = set()
    ans = float('inf')
    for _ in [0] * m:
        s, c = sub(prefers, canceled)
        canceled.add(s)
        ans = min(ans, c)
    return ans

if __name__ == '__main__':
    N, M = map(int, input().split())
    A = [list(map(int, input().split()))[::-1] for _ in [0] * N]
    print(solve(M, A))

Solution (Python):
from collections import Counter
from operator import itemgetter

def sub(prefers, canceled):
    res = Counter()
    for pref in prefers:
        pt = -1
        while pref[pt] in canceled:
            pt -= 1
        res[pref[pt]] += 1
    return max(res.items(), key=itemgetter(1))

def solve(m, prefers):
    canceled = set()
    ans = float('inf')
    for _ in [0] * m:
        s, c = sub(prefers, canceled)
        canceled.add(s)
        ans = min(ans, c)
    return ans

if __name__ == '__main__':
    N, M = map(int, input().split())
    A = [list(map(int, input().split()))[::-1] for _ in [0] * N]
    print(solve(M, A))

Solution (Python):
n,m = (int(i) for i in input().split())
a = [[int(i) for i in input().split()] for i in range(n)]
b,x,num = [0]*n,[0]*(m+1),m
for i in range(n): x[a[i][0]]+=1
z = max(x)
ans,x[x.index(z)] = z,-1
while ans>(n-1)//num+1:
	for i in range(n):
		if x[a[i][b[i]]]==-1:
			while x[a[i][b[i]]]==-1: b[i]+=1
			x[a[i][b[i]]]+=1
	z = max(x)
	ans,x[x.index(z)],num = min(ans,z),-1,num-1
print(ans)

Solution (Python):

def check(mask):
    for e in mask:
        if e==1: return True
    return False

def main():
    n,m=map(int,input().split(" "))
    ia=[list(map(int,input().split(" "))) for _ in range(n)]
    a=[[0 for _ in range(m)] for _ in range(n)]
    for i in range(n):
        for j in range(m):
            a[i][ia[i][j]-1]=j
    mask=[1 for _ in range(m)]
    res=1000
    while check(mask):
        cnt=[0 for _ in range(m)]
        for alst in a:
            mi=3000
            pos=-1
            for i in range(m):
                if alst[i]<mi and mask[i]==1:
                    pos=i
                    mi=alst[i]
            cnt[pos]+=1
        ma=max(cnt)
        res=min(res,ma)
        for i in range(m):
            if cnt[i]==ma: mask[i]=0

    print(res)            


main()

Solution (Python):
import collections
from collections import deque
n,m = map(int,input().split())
ko = deque([])
for i in range(n):
  a= deque(map(int,input().split()))
  ko.append(a)
dai = float("inf")
for j in range(m):
  san = []
  for i in range(n):
    san.append(ko[i][0])
  c = collections.Counter(san)
  to = c.most_common()[0]
  dai = min(to[1],dai)
  ira = to[0]
  for i in range(n):
    ko[i].remove(ira)
print(dai)

Solution (Python):


import sys, math, copy




HUGE = 2147483647
HUGEL = 9223372036854775807
ABC = "abcdefghijklmnopqrstuvwxyz"

def each_attend(anm, held):
    n = len(anm)
    m = len(anm[0])
    atte = []
    for i in range(n):
        aim = anm[i]
        for k in aim:
            if k in held:
                break
        atte.append(k)
    return atte

def main():
    n, m = map(int, input().split())
    anm = []
    for i in range(n):
        aim = list(map(int, input().split()))
        anm.append(aim)

    held = set(range(1, m + 1))
    res = HUGE
    while len(held) > 0:
        atte = each_attend(anm, held)
        num_atte = {k: 0 for k in range(1, m + 1)}
        for k in atte:
            num_atte[k] += 1
        ma = max(num_atte.values())
        res = min(res, ma)
        popest_sports = set(filter(lambda k: num_atte[k] == ma, held))
        held -= popest_sports
    print(res)

main()

Solution (Python):
from collections import Counter

N,M = map(int,input().split())
src = [list(map(lambda x:int(x)-1,input().split())) for i in range(N)]

removed = [0] * M
idxes = [0] * N
ans = N
for _ in range(M-1):
    ctr = Counter([src[i][idxes[i]] for i in range(N)])
    k,most = ctr.most_common(1)[0]
    ans = min(ans, most)
    removed[k] = 1
    for i in range(N):
        while removed[src[i][idxes[i]]]:
            idxes[i] += 1
print(ans)

Solution (Python):
N,M=map(int,input().split())
A=[]
for i in range(N):
  a=[int(x) for x in input().split()]
  A.append(a)
  
def solve():
  res=[ 0 for x in range(M+1)]
  for i in range(N):
    res[A[i][0]]+=1
  ans=max(res)
  jud=res.index(ans)
  for j in range(N):
    A[j].remove(jud)
  return ans

minans=N
for i in range(M):
  minans=min(solve(),minans)
print(minans)

Solution (Python):
N,M=map(int,input().split())
A=[[int(i) for i in input().split()] for _ in range(N)]
R=[0 for i in range(M)]
for i in range(N):
    R[A[i][0]-1]+=1
P=0
Q=0
S=set()
for i in range(N):
    if R[A[i][0]-1]>=P:
        P=R[A[i][0]-1]
        Q=A[i][0]
S.add(Q)
while(len(S)<M):
    Pd=0
    Qd=0
    R=[0 for i in range(M)]
    for i in range(N):
        for j in range(M):
            if not(set([A[i][j]]) <= S):
                R[A[i][j]-1]+=1
                break
    for j in range(M):
        if R[j]>=Pd:
            Pd=R[j]
            Qd=j+1
    if Pd<P:
        P=Pd
    S.add(Qd)
print(P)

Solution (Python):
import sys
n,m=map(int,input().split())
a=[list(map(int,input().split())) for i in range(n)]
for i in range(n):
    for j in range(m):
        a[i][j]-=1
itr=[0 for i in range(n)]
opend=[True for i in range(m)]
back=[[] for i in range(m)]
for i in range(n):
    back[a[i][0]].append(i)
ret=10**9
while True:
    tmp=0
    for i in range(m):
        tmp=max(tmp,len(back[i]))
    ret=min(ret,tmp)
    for i in range(m):
        if len(back[i])>=ret:
            opend[i]=False
            for j in range(len(back[i])):
                source=back[i][j]
                while not opend[a[source][itr[source]]]:
                    itr[source]+=1
                    if itr[source]==m:
                        print(ret)
                        sys.exit()
                back[a[source][itr[source]]].append(source)
            back[i]=[]

Solution (Python):
N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]

S_list = {}
for i in range(M):
    S_list[i+1] = 0

participants = 10000
while len(S_list) > 0:
    for i in range(N):
        for j in range(M):
            if A[i][j] in S_list:
                S_list[A[i][j]] += 1
                break
    participants = min(participants,max(S_list.values()))
    if participants == 1:
        break
    for i in S_list.keys():
        if S_list[i] == max(S_list.values()):
            del S_list[i]
            break
    for i in S_list.keys():
        S_list[i] = 0

print(participants)

Solution (Python):
from collections import defaultdict
from operator import itemgetter


def sub(prefers, canceled):
    res = defaultdict(int)
    for pref in prefers:
        p = pref[-1]
        while p in canceled:
            pref.pop()
            p = pref[-1]
        res[p] += 1

    return max(res.items(), key=itemgetter(1))


def solve(m, prefers):
    canceled = set()
    ans = float('inf')
    for _ in range(m):
        s, c = sub(prefers, canceled)
        canceled.add(s)
        ans = min(ans, c)
    return ans


n, m = map(int, input().split())
prefers = [list(reversed(list(map(int, input().split())))) for _ in range(n)]

print(solve(m, prefers))

Solution (Python):
N, M = map(int, input().split())
A = [list(map(int, input().split())) for i in range(N)]


for i in range(N):
    a = A[i]
    b = [0]*M
    for j in range(M):
        b[a[j]-1] = M-j
    A[i] = b

select = [set() for i in range(M)]
cur = [[1]*(M+1) for i in range(N)]
mp = [[M+1]*(M+1) for i in range(N)]
I = [M]*N
for i in range(N):
    mpi = mp[i]
    Ai = A[i]
    for j in range(M):
        mpi[Ai[j]] = j
    select[mpi[M]].add(i)

ans = N
cnt = 0
for t in range(M+1):
    ma = max(map(len, select))
    ans = min(ans, ma)
    rm = []
    for i in range(M):
        if len(select[i]) == ma:
            rm.append(i)
    target = set()
    for i in rm:
        for j in range(N):
            cur[j][A[j][i]] = 0
        target |= select[i]
        select[i] = []
        cnt += 1
    if cnt == M:
        break
    for i in target:
        p = I[i]
        c = cur[i]
        while c[p] == 0:
            p -= 1
        I[i] = p
        select[mp[i][p]].add(i)
print(ans)

Solution (Python):
people, sports = (int(n) for n in input().split(" "))
preference = []
for _ in range(people):
    pre = [int(n) - 1 for n in input().split(" ")]
    preference.append(pre)
answer = float("inf")
use_sports = {n for n in range(sports)}
for _ in range(sports):
    rank = [0 for n in range(sports)]
    for p in range(people):
        for s in (preference[p]):
            if s in use_sports:
                rank[s] += 1
                break
    answer = min(answer, max(rank))
    use_sports -= {rank.index(max(rank))}
print(answer)

Solution (Python):
def dfs(people, sports, use_sports, answer):
    rank = [0 for n in range(sports)]
    for p in range(people):
        for s in (preference[p]):
            if s in use_sports:
                rank[s] += 1
                break
            will_delete = []
    max_p = max(rank)
    exclude = rank.index(max_p)
    if len(use_sports) > 1:
        answer = min(answer, dfs(people, sports, use_sports - {exclude}, answer))
    answer = min(answer, max_p)
    return answer

people, sports = (int(n) for n in input().split(" "))
preference = []
for _ in range(people):
    pre = [int(n) - 1 for n in input().split(" ")]
    preference.append(pre)
answer = float("inf")
use_sports = {n for n in range(sports)}
print(dfs(people, sports, use_sports, answer))

Solution (Python):
N,M = map(int,input().split())
A = [list(map(lambda x:int(x)-1,input().split()))[::-1] for i in range(N)]

L = [0]*M
for p in A:
  L[p[-1]] += 1
best,i = max((l,i) for i,l in enumerate(L))

for r in range(M-1):
  L[i] = -1
  for p in A:
    L[p[-1]] -= 1
    while L[p[-1]] < 0:
      p.pop()
    L[p[-1]] += 1

  b,i = max((l,i) for i,l in enumerate(L))
  best = min(best,b)

print(best)

Solution (Python):
N,M=list(map(int,input().strip().split(' ')))
A=[]
for _ in range(N):
    A+=[list(map(int,input().strip().split(' ')))[::-1]]
    
current=[0]*(M+1)
deleted=[0]*(M+1)

for j in range(N):
    current[A[j][-1]]+=1
    

MAX=max(current)
ANS=MAX
index=-1
for j in range(1,len(current)):
    if current[j]==MAX:
        index=j
good=0       
if MAX==1:
    good=1
    print(1)
if good==0:
    count=0
    while(count<M-1):
        current=[0]*(M+1)
        deleted[index]=1
        for x in range(N):
            while(deleted[A[x][-1]]==1):
                A[x].pop()
                if A[x]==[]:
                    break
        for x in range(N):
            if A[x]!=[]:
                current[A[x][-1]]+=1
        MAX=max(current)
        if MAX<ANS:
            ANS=MAX
        for x in range(M+1):
            if current[x]==MAX:
                index=x
                
        count+=1  
    print(ANS)

Solution (Python):
N, M = map(int, input().split())
A=[]
for n in range(N):
    A.append(list(map(int,input( ).split( ))))

candi=[ ]
for m in range(M):
    tmp =[0 for _ in range(M)]
    for n in range(N):
        tmp[A[n][0]-1] +=1
    max_player=max(tmp)
    candi.append(max_player)
    max_sports=tmp.index(max_player)
    for n in range(N):
         A[n].remove(max_sports+1)
 
print(min(candi))

Solution (Python):
from collections import Counter

def read():
    return [int(x) for x in input().split()]

N, M = read()

A = []
for _ in range(N):
    A.append(read())

res = N
for _ in range(M - 1):
    count_dict = Counter([a[0] for a in A])
    sport, count = count_dict.most_common(1)[0]
    res = min(res, count)
    for a in A:
        a.remove(sport)

print(res)

Solution (Python):
if __name__ == '__main__':
    N, M = list(map(int, input().split()))
    A = []
    for i in range(N):
        A.append(list(map(int, input().split())))

    ans = float("inf")
    while len(A[0]) != 0:
        count_list = [0 for i in range(M + 1)]
        for i in range(0, N):
            count_list[A[i][0]] += 1

        max_value = max(count_list)
        for i in range(0, N):
            A[i].remove(count_list.index(max_value))

        if ans > max_value:
            ans = max_value

    print(ans)

Solution (Python):
from collections import Counter

N, M=map(int, input().split())
m=[]
ans = 300

for i in range(N):
    m.append(list(map(int,input().split())))

def search(ln):
    
    cnt = Counter()
    for r in m:
        cnt[r[0]] += 1
    s,c = cnt.most_common(1)[0]
    global ans
    ans = min(ans, c)
    ln.remove(s)
    for r in m:
        r.remove(s)
    if len(ln) == 0:
        return 
    search(ln)
search(list(range(1, M+1)))
print (ans)

Solution (Python):
N, M = map(int, raw_input().split(' '))
A = [map(int, raw_input().split(' ')) for _ in xrange(N)]

answer = N
for _ in xrange(M):
    participants = [0] * M
    for n in xrange(N):
        participants[A[n][0]-1] += 1
    sport_with_max_participants = max(xrange(M), key=lambda x: participants[x])
    answer = min(answer, participants[sport_with_max_participants])
    for n in xrange(N):
        A[n].remove(sport_with_max_participants+1)

print answer

Solution (Python):


def main():
	N, M = list(map(int, input().split()))
	prefs = [list(map(int, input().split())) for _ in range(N)]

	game_list = [i for i in range(1, M + 1)]

	tmp = 10 ** 10
	for i in range(M):
		max_game, idx = maxGame(prefs, game_list, M)
		tmp = min(tmp, max_game)
		prefs = removeGame(prefs, idx + 1)
	print(tmp)

def removeGame(prefs, idx):
	for i in range(len(prefs)):
		prefs[i].remove(idx)
	return(prefs)

def maxGame(prefs, game_list, M):
	counter = [0 for _ in range(M)]
	for i in range(len(prefs)):
		counter[prefs[i][0] - 1] += 1
	return([max(counter), counter.index(max(counter))])

if __name__ == '__main__':
	main()

Solution (Python):
import sys
from collections import Counter
import random

random.seed(1)




def read_int_list():
    return list(map(int, input().split()))


def read_int():
    return int(input())


def read_str_list():
    return input().split()


def read_str():
    return input()


def gen():
    n = 301
    m = 300
    a = [[-1] * m for i in range(n)]
    for i in range(n):
        for j in range(m):
            a[i][j] = 1 + (i + j) % m
    return n, m, a


def gen2(n, m):
    a = [list(range(1, m + 1)) for i in range(n)]
    for i in range(n):
        random.shuffle(a[i])
    return a


def is_possible(l, n, m, a):
    s = set(range(1, m + 1))
    choice = [0] * n
    c = Counter()
    for i in range(n):
        c[a[i][0]] += 1
    while len(s) > 0:
        update = False
        for i in range(n):
            j = choice[i]
            x = a[i][j]
            if x not in s:
                c[x] -= 1
                while a[i][j] not in s:
                    j += 1
                choice[i] = j
                x = a[i][j]
                c[x] += 1
            if c[x] > l:
                s.remove(x)
                update = True
                break
        if not update:
            return True
    return False


def solve(n, m, a):
    left, right = 1, n
    if is_possible(left, n, m, a):
        return left
    while right - left > 1:
        mid = (left + right) // 2
        if is_possible(mid, n, m, a):
            right = mid
        else:
            left = mid
    return right


def solve2(n, m, a):
    pass

    pinf = float("inf")
    best = pinf
    roop = 0
    while True:
        if roop == m:
            break

        firstlist = []
        for r in a:
            tmp = r[0]
            firstlist.append(tmp)
        mode = -1
        count = -1
        for i in range(1, m + 1):
            tmp_c = firstlist.count(i)
            if tmp_c > count:
                mode = i
                count = tmp_c
        if mode == -1:
            break
        elif count <= best:
            best = count
        for r in a:
            r.remove(mode)
        roop += 1
    return best


def check():
    n_steps = 10000
    M = 10
    n_max = 10
    m_max = 10
    for i in range(n_steps):
        n = random.randint(1, n_max)
        m = random.randint(1, m_max)

        a = gen2(n, m)
        aa = [row[:] for row in a]
        r = solve(n, m, a)
        r2 = solve2(n, m, aa)
        if r != r2:
            print('expected:', r2, 'got:', r, )
            print(n, m)
            for row in a:
                print(*row)
            print()
            
            n_max -= 1
            m_max -= 1


def main():
    
    solve_it()


def solve_it():
    debug = False
    if debug:
        n, m, a = gen()
    else:
        n, m = read_int_list()
        a = [read_int_list() for _ in range(n)]
    res = solve(n, m, a)
    print(res)


if __name__ == '__main__':
    main()