Scanf.scanf "%d %d" (fun n m ->
    let a = Array.init n (fun _ ->
        Array.init m (fun _ -> Scanf.scanf " %d" (fun a -> a - 1)))
    in
    let module S = Set.Make (struct type t = int let compare = compare end) in
    let check q =
        let idx = Array.make n 0 in

        let check2 set =
            let hist = Array.make m 0 in
            let rec loop i mx mv =
                if i = n then mv else
                    let c = a.(i).(idx.(i)) in
                    let () = hist.(c) <- hist.(c) + 1 in
                    if hist.(c) > mx then loop (i + 1) hist.(c) c else
                                          loop (i + 1) mx mv
            in
            let mv = loop 0 0 0 in
            let mx = hist.(mv) in
            let set = S.add mv set in
            if mx <= q then true, set else (
                for i = 0 to n - 1 do
                    while idx.(i) < m && S.mem a.(i).(idx.(i)) set do
                        idx.(i) <- idx.(i) + 1
                    done
                done;
                false, set
            )
        in

        let rec loop rest set =
            if rest = 0 then false else
                let flag, set = check2 set in
                if flag then true else loop (rest - 1) set
        in
        loop m S.empty
    in
    let rec bin left right =
        if left = right then left else
            let m = (left + right) / 2 in
            if check m then bin left m
                       else bin (m + 1) right
    in
    bin 1 (n + 1) |> Printf.printf "%d\n"
)