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

using namespace std;

int main(void)
{
	int n, m;

	while (cin >> n >> m, n || m){
		vector <int> p(n + 1);
		for (int i = 0; i < n; i++){
			scanf("%d", &p[i]);
		}
		p[n] = 0;
		n++;

		vector <int> sum(n * n);
		int cnt = 0;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < n; j++){
				sum[cnt] = p[i] + p[j];
				// cout << sum[cnt] << " " << p[i] << " " << p[j] << endl;
				cnt++;
			}
		}

		sort(sum.begin(), sum.end());
		int ret = 0;
		for (int i = 0; i < n * n; i++){
			int diff = m - sum[i];
			if (diff < 0) continue;
			int up = *(upper_bound(sum.begin(), sum.end(), diff)-1);
			ret = max(ret, sum[i] + up);
		}

		cout << ret << endl;
	}

	return 0;
}
#include <bits/stdc++.h>
typedef long long LL;
using namespace std;
inline int rd() {
    char ch=getchar(); int i=0,f=1;
    while(!isdigit(ch)) {if(ch=='-')f=-1; ch=getchar();}
    while(isdigit(ch)) {i=(i<<1)+(i<<3)+ch-'0'; ch=getchar();}
    return i*f;
}
const int N=2e5+50;
const LL INF=0x3f3f3f3f3f3f3f3f;
int n,q;
LL bit[2][N],tag,mn=0;
inline void inc(LL *a,int i,LL v) {for(;i<=n;i+=(i&(-i))) a[i]=min(a[i],v);}
inline LL ask(LL *a,int i,LL v=INF) {for(;i;i-=(i&(-i))) v=min(a[i],v); return v;}
int main() {
    n=rd(), q=rd(); 
    int a=rd(), b=rd();
    memset(bit[0],0x3f,sizeof(bit[0]));
    memset(bit[1],0x3f,sizeof(bit[1]));
    inc(bit[0],a,-a); inc(bit[1],n-a+1,a);
    for(int i=1;i<=q;i++) {
        int x=rd();
        LL v=min(ask(bit[0],x)+x,ask(bit[1],n-x+1)-x);
        tag+=abs(x-b); v-=abs(x-b); mn=min(mn,v);
        inc(bit[0],b,v-b); inc(bit[1],n-b+1,v+b);
        b=x;
    }
    printf("%lld\n",mn+tag);
}