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 <stdio.h>
#include <iostream>
#include <cstdio>
#include <algorithm> 
#include <stdio.h>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <list>
#include <utility>
#include <set>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>
#include <bitset>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <random>

using namespace std;

static const double EPS = 1e-9;
template<class T> bool INRANGE(T x, T a, T b) { return a <= x&&x <= b; }
template<class T> void amin(T &a, T v) { if (a > v) a = v; }
template<class T> void amax(T &a, T v) { if (a < v) a = v; }
int ROUND(double x) { return (int)(x + 0.5); }
bool ISINT(double x) { return fabs(ROUND(x) - x) <= EPS; }
bool ISEQUAL(double x, double y) { return fabs(x - y) <= EPS*max(1.0, max(fabs(x), fabs(y))); }
double SQSUM(double x, double y) { return x*x + y*y; }
#define PI  (acos(-1))
#define ARRAY_NUM(a) (sizeof(a)/sizeof(a[0])) 
#define NG (-1)
#define BIG ((int)1e9+10)
#define BIGLL ((ll)4e18)
#define SZ(a) ((int)(a).size())
#define SQ(a) ((a)*(a))
typedef unsigned long long ull;
typedef long long ll;


#if 1
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 k = 0; k < m; ++k)
		{
			cin >> a[i][k];
			a[i][k]--;
		}
	}

	for (int ans=1; ans<=n; ans++)
	{
		vector <bool> kaisai(m, true);
		vector <int> rank(n);	// NG 決定

		while(1)
		{
			int x = 0;
			vector <int> freq(m);
			for (int i = 0; i < n; ++i)
			{
				if (rank[i] != NG && rank[i]<m)
				{
					int k = a[i][rank[i]];
					if(kaisai[k])
					{
						freq[k]++;
						x++;
					}
				}
			}

			if(x==0)
			{
				break;
			}

			// すべてのfreq[]

			// もしansを超える人気競技があれば、その競技は選べない。
			bool ok = true;
			for (int k = 0; k < m; ++k)
			{
				if (freq[k] > ans)
				{
					ok = false;
					kaisai[k] = false;
				}
			}

			if(ok)
			{
				cout << ans << endl;
				return 0;
			}

			// 全員
			// 人気すぎる競技を選んだ人→希望rankさげ
			for (int i = 0; i < n; ++i)
			{
				if (rank[i] != NG && rank[i] < m)
				{
					if(kaisai[a[i][rank[i]]])
					{
					}
					else
					{
						rank[i]++;
					}
				}
			}
		}
	}

	return 0;
}



#else

ll gcd(ll x, ll y)
{
	if (x == 0) return y;
	if (y == 0) return x;

	ll t;

	while (y != 0)
	{
		t = x%y;
		x = y;
		y = t;
	}
	return x;
}

int main()
{
	int n,k;
	cin >> n >> k;

	vector <int> a(n);

	for (int i = 0; i < n; ++i)
	{
		cin >> a[i];
	}

	int mx = *max_element(a.begin(),a.end());

	ll g = a[0];
	for (int i = 1; i < n; ++i)
	{
		g = gcd(g, a[i]);
	}

	if (k <= mx && k%g == 0)
	{
		cout << "POSSIBLE" << endl;
	}
	else
	{
		cout << "IMPOSSIBLE" << endl;
	}

	return 0;
}

#endif