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 C++14 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <algorithm>
#include <cassert>
#include <deque>
#include <functional>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

bool test(int N, int M, const vector<vector<int>>& A, int k)
{
    vector<bool> used(M, false);

    vector<vector<int>::const_iterator> ps(N);
    vector<vector<int>::const_iterator> ends(N);
    for (int i = 0; i < N; ++i) {
        ps[i] = A[i].begin();
        ends[i] = A[i].end();
    }

    while (true) {
        vector<int> cnt(M);
        for (int i = 0; i < N; ++i) {
            if (ps[i] != ends[i])
                ++cnt[*ps[i]];
        }

        vector<pair<int, int>> rank;
        for (int i = 0; i < M; ++i) {
            rank.push_back(make_pair(cnt[i], i));
        }
        sort(rank.begin(), rank.end(), greater<pair<int, int>>());

        if (rank[0].first > k) {
            for (auto pr : rank) {
                int c = pr.first;
                int i = pr.second;
                used[i] = true;

                if (c <= k)
                    break;
            }

            for (int i = 0; i < N; ++i) {
                if (ps[i] == ends[i])
                    continue;

                while (ps[i] != ends[i] && used[*ps[i]])
                    ++ps[i];
                if (ps[i] == ends[i])
                    return false;
            }
        } else {
            break;
        }
    }

    return true;
}

int main()
{
    int N, M;
    cin >> N >> M;

    vector<vector<int>> A(N, vector<int>(M));
    for (int i = 0; i < N; ++i) {
        for (int j = 0; j < M; ++j) {
            cin >> A[i][j];
            --A[i][j];
        }
    }

    int mn = 0;
    int mx = 300;
    while (mx - mn > 1) {
        int mid = (mn + mx) / 2;
        if (test(N, M, A, mid))
            mx = mid;
        else
            mn = mid;
    }

    cout << mx << '\n';

    return 0;
}