#include <iostream>
#include <vector>
#include <climits>
int square(int x){
	return x * x;
}
int main()
{
	using namespace std;
	int N, M, i, j, c, k, v, y, sub_sq, min;
	vector <int> C, x;
	while (true)
	{
		cin >> N >> M;
		if (N == 0 and M == 0) break;
		vector < vector<int> > dp(N+1);
		C.resize(M); x.resize(N);
		for (i=0;i<=N;i++){dp[i].resize(256); for (j=0;j<256;j++) dp[i][j] = INT_MAX;}
		for (i=0;i<M;i++) cin >> C[i];
		for (i=0;i<N;i++) cin >> x[i];
		dp[0][128] = 0;
		for(i = 0; i < N; i++)
		{
			for (k = 0; k < 256; k++)
			{
				v = dp[i][k];
				if (v == INT_MAX) continue; 
				for(j = 0; j < M; j++)
				{
					c = C[j];
					if (k + c > 255) y = 255; else if (k + c < 0) y = 0; else y = k + c;
					sub_sq = square(x[i]-y);
					if(v + sub_sq < dp[i+1][y]) dp[i+1][y] = v + sub_sq;
				}
			}
		}
		min = INT_MAX;
		for(k = 0; k < 256; k++) min = min <= dp[N][k] ? min : dp[N][k];  
		cout << min << endl;
	}
	return 0;
}