#include <cstdio>
#include <iostream>
using namespace std;

struct Candidate{
	int num;
};

int main(void) {
	for (;;) {
		int n, p;
		cin >> n >> p;
		if (n == 0 && p == 0)
			break;
		Candidate candidate[50];
		for (int i = 0; i < 50; i++) {
			candidate[i].num = 0;
		}
		
		int winner;
		int t = p;
		for (int i = 0; i <= 1000000; i=(i+1)%n) {
			if (p != 0) {
				candidate[i].num++;
				p--;
			}
			else if (p == 0) {
				p+=candidate[i].num;
				candidate[i].num = 0;
			}
			if (candidate[i].num == t) {
				winner = i;
				break;
			}
		}
		printf("%d\n", winner);
	}
	return 0;
}
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
vector <int> a;
vector <int> all;
int n,m;


int main() {

  while(1) {
    scanf("%d%d",&n,&m);
    if(n == 0 && m ==0) break;
    a.resize(n);
    for(int i=0;i<n;i++) scanf("%d",&a[i]);

    a.push_back(0);
    all.clear();

    for(int i=0;i<a.size();i++) 
      for(int j=i;j<a.size();j++)
	all.push_back(a[i]+a[j]);

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

    int ans = 0;
    int allsize = all.size();
    int b = 0;
    for(int i=allsize-1;i>= 0;i--) {
      for(int j=b;j<allsize;j++) {
	int sum = all[i] + all[j];
	if(sum > m) break;
	b++;
	if(ans < sum) ans = sum;
      }
      if(ans == m) break;
    }

    cout << ans <<endl;
  }
  return 0;
  
}