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 in Rust (1.15.1) (judge verdict: Accepted)

use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;
use std::iter::FromIterator;
use std::cmp::min;

fn main() {
    let cin = stdin();
    let mut scan = Scanner::new(cin.lock());
    let n = scan.read::<usize>();
    let m = scan.read::<usize>();
    let a = arr(n, |_| arr(m, |_| scan.read::<usize>()-1));
    let mut b = vec![vec![0;m];n];
    for i in 0..n {
        for j in 0..m {
            b[i][a[i][j]] = j as i64;
        }
    }
    let mut segtrees = vec![];
    for y in 0..n {
        segtrees.push(SegIndexTree::new(b[y].clone(), 1i64 << 60, std::cmp::min));
    }
    let mut ret = 1i64 << 60;
    for _ in 0..m {
        let mut counter = vec![0i64;m];
        for y in 0..n {
            let (_, i) = segtrees[y].get(0..m);
            counter[i] += 1;
        }
        let (maxi, &k) = max_with_index(&counter);
        ret = min(ret, k);
        for y in 0..n {
            segtrees[y].set(maxi, 1i64 << 60);
        }
    }
    println!("{}", ret);
}

#[allow(dead_code)]
fn max_with_index<T: Ord>(slice: &[T]) -> (usize, &T) {
    let mut value: Option<&T> = None;
    let mut index = 0;
    for i in 0..slice.len() {
        if let Some(v) = value {
            if v < &slice[i] {
                index = i;
                value = Some(&slice[i]);
            }
        } else {
            index = i;
            value = Some(&slice[i]);
        }
    }
    (index, value.expect("not found max value"))
}

use std::ops::Range;

#[allow(dead_code)]
fn contains(p: &Range<usize>, q: &Range<usize>) -> bool {
    p.start <= q.start && q.end <= p.end
}

#[allow(dead_code)]
fn separates(p: &Range<usize>, q: &Range<usize>) -> bool {
    p.end <= q.start || q.end <= p.start
}

#[allow(dead_code)]
fn size(p: &Range<usize>) -> usize {
    p.end - p.start
}

#[allow(dead_code)]
struct SegIndexTree<'a> {
    node: Vec<(i64, usize)>,
    zero: i64,
    op: Box<Fn((i64, usize), (i64, usize)) -> (i64, usize) + 'a>,
    n: usize,
}

#[allow(dead_code)]
impl <'a> SegIndexTree<'a> {
    fn new<B>(v: Vec<i64>, zero: i64, op: B) -> SegIndexTree<'a> where B: Fn((i64, usize), (i64, usize)) -> (i64, usize) + 'a {
        let size = v.len();
        let mut n = 1usize;
        while n < size {n *= 2}
        let mut node = vec![(zero, 0); 2*n-1];
        for i in 0..size {node[i+n-1] = (v[i], i)}
        for i in (0..n - 1).rev() {node[i] = op(node[2*i+1], node[2*i+2])}
        SegIndexTree {node: node, zero: zero, op: Box::new(op), n: n}
    }

    fn set(&mut self, mut x: usize, value: i64) {
        let y = x;
        x += self.n - 1;
        self.node[x] = (value, y);
        while x > 0 {
            x = (x - 1) / 2;
            self.node[x] = (self.op)(self.node[2*x+1], self.node[2*x+2]);
        }
    }

    fn get1(&self, i: usize) -> (i64, usize) {self.getrec(i..i+1, 0, 0..self.n)}
    fn get(&self, query: Range<usize>) -> (i64, usize) {self.getrec(query, 0, 0..self.n)}

    fn getrec(&self, query: Range<usize>, k: usize, range: Range<usize>) -> (i64, usize) {
        if separates(&query, &range) {return (self.zero, 0)}
        if contains(&query, &range) {return self.node[k]}
        let vl = self.getrec(query.clone(), 2*k+1, range.start..(range.start + range.end)/2);
        let vr = self.getrec(query.clone(), 2*k+2, (range.start + range.end)/2..range.end);
        (self.op)(vl, vr)
    }
}

// region template
#[allow(dead_code)]
fn iu(i: isize) -> usize { i as usize }

#[allow(dead_code)]
fn ui(i: usize) -> isize { i as isize }

#[allow(dead_code, deprecated)]
fn join<T: std::fmt::Display>(slice: &[T], sep: &str) -> String {
    let strings = slice.iter().map(|t| format!("{}", t)).collect::<Vec<_>>();
    strings.connect(sep)
}

#[allow(dead_code)]
fn arr<'a, S, T>(n: usize, mut f: S) -> Vec<T> where S: FnMut(usize) -> T + 'a {
    (0..n).map(|i| f(i)).collect::<Vec<T>>()
}

#[allow(dead_code)]
fn alt<S, T>(v: Vec<S>) -> T where T: FromIterator<S> {
    v.into_iter().collect::<T>()
}

#[allow(dead_code)]
fn dup<S, T>(v: &[S]) -> T where T: FromIterator<S>, S: Clone {
    v.iter().cloned().collect::<T>()
}

struct Scanner<'a> {
    cin: StdinLock<'a>,
}

#[allow(dead_code)]
impl<'a> Scanner<'a> {
    fn new(cin: StdinLock<'a>) -> Scanner<'a> {
        Scanner { cin: cin }
    }

    fn read1<T: FromStr>(&mut self) -> Option<T> {
        let token = self.cin.by_ref().bytes().map(|c| c.unwrap() as char)
            .skip_while(|c| c.is_whitespace())
            .take_while(|c| !c.is_whitespace())
            .collect::<String>();
        token.parse::<T>().ok()
    }

    fn read<T: FromStr>(&mut self) -> T {
        self.read1().unwrap()
    }

    fn readn<T: FromStr>(&mut self, n: usize) -> Vec<T> {
        (0..n).map(|_| self.read::<T>()).collect()
    }

    fn chars(&mut self) -> Vec<char> {
        self.read::<String>().chars().collect()
    }
}
// endregion