import sys

input = sys.stdin.readline

N, M = list(map(int, input().split()))

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

inva = [[0] * M for _ in range(N)]

for i in range(N):

  for j in range(M):

    inva[i][a[i][j] - 1] = j

dp = [N * M] * M

t = [0] * N

vis = set()

#print(inva)

for i in range(M):

  x = 0

  y = 0

  for j in range(M):

    z = 0

    for k in range(N):

      z += inva[k][j] == t[k]

    if z > y:

      y = z

      x = j

  dp[i] = y

  vis.add(x)

  for k in range(N):

    w = M

    for j in range(M):

      if j in vis: continue

      w = min(w, inva[k][j])

    t[k] = w

print((min(dp)))
import sys

input = sys.stdin.readline

N, M = list(map(int, input().split()))

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

t = [0] * (M + 1)

p = [0] * N

s = set()

res = float("inf")

for _ in range(M):

  t = [0] * (M + 1)

  for i in range(N): t[a[i][p[i]]] += 1

  x = 0

  y = 0

  for j in range(1, M + 1):

    if y < t[j]:

      x = j

      y = t[j]

  s.add(x)

  for i in range(N):

    while a[i][p[i]] in s:

      p[i] += 1

      if p[i] >= M:

        break

  if max(p) >= M: break

  res = min(res, max(t))

  #print(t, p)

print((min(res, N)))
import sys

input = sys.stdin.readline
N, M = list(map(int, input().split()))
a = [list(map(int, input().split())) for _ in range(N)]
inva = [[0] * M for _ in range(N)]
for i in range(N):
    for j in range(M):
        inva[i][a[i][j] - 1] = j
dp = [N * M] * M
t = [0] * N
vis = set()
# print(inva)
for i in range(M):
    x = 0
    y = 0
    for j in range(M):
        z = 0
        for k in range(N):
            z += inva[k][j] == t[k]
        if z > y:
            y = z
            x = j
    dp[i] = y
    vis.add(x)
    for k in range(N):
        w = M
        for j in range(M):
            if j in vis:
                continue
            w = min(w, inva[k][j])
        t[k] = w
print((min(dp)))

import sys

input = sys.stdin.readline
N, M = list(map(int, input().split()))
a = [list(map(int, input().split())) for _ in range(N)]
t = [0] * (M + 1)
p = [0] * N
s = set()
res = float("inf")
for _ in range(M):
    t = [0] * (M + 1)
    for i in range(N):
        t[a[i][p[i]]] += 1
    x = 0
    y = 0
    for j in range(1, M + 1):
        if y < t[j]:
            x = j
            y = t[j]
    s.add(x)
    for i in range(N):
        while a[i][p[i]] in s:
            p[i] += 1
            if p[i] >= M:
                break
    if max(p) >= M:
        break
    res = min(res, max(t))
    # print(t, p)
print((min(res, N)))

-inva = [[0] * M for _ in range(N)] -for i in range(N): -    for j in range(M): -        inva[i][a[i][j] - 1] = j -dp = [N * M] * M -t = [0] * N -vis = set() -# print(inva) -for i in range(M): +t = [0] * (M + 1) +p = [0] * N +s = set() +res = float("inf") +for _ in range(M): +    t = [0] * (M + 1) +    for i in range(N): +        t[a[i][p[i]]] += 1 -    for j in range(M): -        z = 0 -        for k in range(N): -            z += inva[k][j] == t[k] -        if z > y: -            y = z +    for j in range(1, M + 1): +        if y < t[j]: -    dp[i] = y -    vis.add(x) -    for k in range(N): -        w = M -        for j in range(M): -            if j in vis: -                continue -            w = min(w, inva[k][j]) -        t[k] = w -print((min(dp))) +            y = t[j] +    s.add(x) +    for i in range(N): +        while a[i][p[i]] in s: +            p[i] += 1 +            if p[i] >= M: +                break +    if max(p) >= M: +        break +    res = min(res, max(t)) +    # print(t, p) +print((min(res, N)))