#include <iostream>

#include <algorithm>



constexpr int MAX_N=1001;



int n,m;

int P[MAX_N];



int Data[MAX_N*MAX_N];



int main() {
    while(1){
    std::cin>>n>>m;
    if(n+m==0)
    	break;

    for(int i=1;i<=n;++i)

        std::cin>>P[i];

    for(int i=0;i<=n;++i){

		for(int j=0;j<=n;++j)

	    	Data[i*(n+1)+j]=-(P[i]+P[j]);

    }

    std::sort(Data,Data+(n+1)*(n+1));

    int ans=0;

    for(int i=0;i<=n;++i){

		for(int j=0;j<=n;++j){

	   		int x=P[i]+P[j]-m;

	    	int* v=std::lower_bound(Data,Data+(n+1)*(n+1),x);

	    	if(v!=Data+(n+1)*(n+1))

				ans=std::max(ans,P[i]+P[j]-(*v));

		}

    }

    std::cout<<ans<<std::endl;
    }

    return 0;

}