let rec for_fold a b v f = if a >= b then v else for_fold (a+1) b (f v a) f
let () =
  Scanf.scanf "%d %d" @@ fun n m ->
  let a = Array.init n (fun _ -> Array.init m (fun _ -> Scanf.scanf " %d" pred)) in
  let held = Array.make m true in
  for_fold 0 m n (fun c _ ->
    let counts = Array.make m 0 in
    for i = 0 to n-1 do
      let k = for_fold 0 m (-1) (fun k j ->
        if k >= 0 then k
        else if held.(a.(i).(j)) then a.(i).(j) else -1) in
      counts.(k) <- counts.(k) + 1;
    done;
    let k = for_fold 0 m 0 (fun k i ->
      if not held.(k) then i
      else if not held.(i) then k
      else if counts.(k) > counts.(i) then k else i)
    in
    held.(k) <- false;
    min c counts.(k)
  ) |> Printf.printf "%d\n"