#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class MedianFaking { public:
	vector <int> minimize(int F, int M, vector <int> data, int goal)
	{
		vector<pair<int, int>> VP;
		for (int p = 0; p < F; ++p)
			for (int m = 0; m < M; ++m)
				VP.emplace_back(data[p*M + m], p);
		sort(VP.begin(), VP.end());

		const int N = VP.size();
		const int mid = (N - 1) / 2;

		for (int s = 0; s <= N; ++s) {
			if (s == N || VP[s].first >= goal) {
				for (int m = s; m <= N; ++m) {
					if (m == N || VP[m].first > goal) {
						// small goal big
						// [0,s)[s,m)[m,N)
						if (s <= mid && mid < m)
							return{ 0,0 };
						if (s > mid) {
							return{ choose(s - mid, vector<pair<int, int>>(VP.begin(), VP.begin() + s)), s - mid };
						}
						if (m <= mid) {
							return{ choose(mid - m + 1, vector<pair<int, int>>(VP.begin() + m, VP.end())), mid - m + 1 };
						}
						return{ -1,-1 };
					}
				}
				break;
			}
		}
		return{ -1,-1 };
	}

	int choose(int k, const vector<pair<int, int>>& VP)
	{
		map<int, int> pcmap;
		for (auto vp : VP) pcmap[vp.second]++;
		vector<int> cs;
		for (auto pc : pcmap) cs.emplace_back(pc.second);
		sort(cs.rbegin(), cs.rend());

		int p = 0;
		for (auto c: cs) {
			++p;
			k -= c;
			if (k <= 0) break;
		}
		return p;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const vector <int>& Expected, const vector <int>& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: " << Expected << endl << "\tx: " << Received << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, MedianFaking().minimize(F, M, data, goal));}
int main(){

CASE(0)
	int F = 5; 
	int M = 5; 
	int data_[] = {1,2,3,4,5, 10,9,8,7,6, 25,24,23,22,21, 18,16,17,19,20, 11,13,12,14,15};
	  vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 
	int goal = 8; 
	int __[] = {1, 5 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(1)
	int F = 5; 
	int M = 5; 
	int data_[] = {1,2,25,24,23, 3,4,22,21,20, 5,6,19,18,17, 7,16,15,14,13, 8,12,11,10,9};
	  vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 
	int goal = 8; 
	int __[] = {2, 5 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(2)
	int F = 1; 
	int M = 4; 
	int data_[] = {10, 40, 30, 20};
	  vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 
	int goal = 20; 
	int __[] = {0, 0 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(3)
	int F = 4; 
	int M = 3; 
	int data_[] = {3,8,12, 3,8,12, 3,8,12, 8,12,17};
	  vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 
	int goal = 12; 
	int __[] = {1, 2 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(4)
	int F = 5; 
	int M = 1; 
	int data_[] = {10, 20, 30, 40, 50};
	  vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 
	int goal = 23; 
	int __[] = {1, 1 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
CASE(5)
	int F = 1; 
	int data_[] = { 2 };
	vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_));
	int M = data.size()/F;
	int goal = 3;
	int __[] = { 1,1 };
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
/*
CASE(6)
	int F = ; 
	int M = ; 
	int data_[] = ;
	  vector <int> data(data_, data_+sizeof(data_)/sizeof(*data_)); 
	int goal = ; 
	int __[] = ;
	  vector <int> _(__, __+sizeof(__)/sizeof(*__)); 
END
*/
}
// END CUT HERE
