#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e9+7;
ll dp[260][20005];

signed
main(){
	int N,M;
	while(cin >> N >> M)
	{
		if(N==0) break;
		for(int i=0;i<260;i++)
			for(int j=0;j<20005;j++)
				dp[i][j] = INF;
		
		vector<int> c(M);
		for(int i=0;i<M;i++) cin >> c[i];
		
		dp[128][0] = 0;
		for(int i=0;i<N;i++)
		{
			int x;
			cin >> x;
			for(int k=0;k<256;k++)
			{
				if(dp[k][i]==INF) continue;
				for(int j=0;j<M;j++)
				{
					int tmp = k+c[j];
					if(tmp >= 255) tmp = 255;
					else if(tmp < 0) tmp = 0;
					dp[tmp][i+1] = min( dp[tmp][i+1], dp[k][i]+(tmp-x)*(tmp-x) );
				}

			}
		}
		int ans = INF;
		for(int k=0;k<256;k++)
			if(ans > dp[k][N])
				ans = dp[k][N];

		cout << ans << endl;
	}
}
