#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long LL;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<int> VI;
typedef vector<VI> VVII;
const LL INF = (LL)1e9 *(LL)1e9;
using namespace std;

int main(){
	while (1){
		int N, M;
		cin >> N >> M;
		if (N + M == 0)return 0;
		VI x(N+1),C(M+1);
		for (int i = 0; i < M; i++){
			cin >> C[i];
		}
		for (int i = 1; i < N+1; i++){
			cin >> x[i];
		}
		VVLL dp(N + 1, VLL(256, INF));
		dp[0][128] = 0;
		for (int n = 0; n < N; n++){
			for (int i = 0; i < 256; i++){
				for (int j = 0; j < M; j++){
					int r = i + C[j];
					if (r>255)r = 255;
					else if (r < 0)r = 0;
					dp[n + 1][r] = min(dp[n+1][r],dp[n][i] + (x[n + 1] - r)*(x[n + 1] - r));
				}
			}
		}
		LL res = INF;
		for (int i = 0; i < 256; i++){
			res = min(res, dp[N][i]);
		}
		cout << res << endl;

	}

	return 0;
}