#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

int main()
{
	int N, M, D;

	while (true)
	{
		scanf("%d", &N);
		scanf("%d", &M);

		if (N == 0 && M == 0) { break; }

		vector<int> C; C.push_back(0);

		for (int i = 0; i < N; i++)
		{
			scanf("%d", &D);

			if (D <= M)
			{
				C.push_back(D);
			}
		}

		vector<int> r1, r;

		for (int i = 0; i < C.size(); i++)
		{
			for (int j = i; j < C.size(); j++)
			{
				if (C[i] + C[j] <= M)
				{
					r1.push_back(C[i] + C[j]);
				}
			}
		}

		sort(r1.begin(), r1.end());

		r.push_back(r1[0]);

		for (int i = 0; i < r1.size(); i++)
		{
			if (r1[i - 1] != r1[i])
			{
				r.push_back(r1[i]);
			}
		}

		int ret = 0;

		for (int i = 0; i < r.size(); i++)
		{
			int ptr = upper_bound(r.begin(), r.end(), M - r[i]) - r.begin() - 1;
			
			if (ptr >= 0)
			{
				ret = max(ret, r[i] + r[ptr]);
			}
		}

		printf("%d\n", ret);
	}

	return 0;
}
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <cstring>
#include <iomanip>
#include <cassert>

using namespace std;
typedef long long ll;

int main(){
   int N, M;
   cin >> N >> M;
   vector<int> pos(N,0);
   int favs[N][M];
   for(int i = 0 ; i <N; i++){
      for(int j = 0; j <M; j++){
         cin >> favs[i][j];
         favs[i][j]--;
      }
   }
   vector<bool> used(M,false);
   int ans = N;
   for(int j = 0 ; j < M; j++){
      vector<int> cnt(M,0);
      int target = 0;
      int m=0;
      for(int i = 0 ; i <N; i++){
         int f = favs[i][pos[i]];
         cnt[f]++;
         if(cnt[f]>m){
            m=cnt[f];
            target = f;
         }
      }
      // delete target
      //cout << m << " " << target << endl;
      used[target]=true;
      ans = min(m,ans);
      if(j!=M-1){
         for(int i = 0 ; i <N; i++){
            while(used[favs[i][pos[i]]]==true) pos[i]++; 
         }
      }
   }
   cout << ans << endl;
   return 0;
}
