#include<iostream>
#include<algorithm>

using namespace std;
#define INF (1<<29)
int n, m, x;
int c[20];
int dp[21600][256];
bool solve(){
	cin >> n >> m;
	if(n == 0 && m == 0)return false;
	for(int i = 0;i < m;i++){
		cin >> c[i];
	}
	for(int i = 0;i < 21600;i++){
		for(int j = 0;j < 256;j++){
			dp[i][j] = INF;
		}
	}
	dp[0][128] = 0;
	for(int i = 0;i < n;i++){
		cin >> x;
		for(int j = 0;j < 256;j++){
			for(int k = 0;k < m;k++){
				int nxt = max(0, min(255, j + c[k]));
				dp[i+1][nxt] = min(dp[i+1][nxt], dp[i][j] + (x - nxt)*(x - nxt));
			}
		}
	}
	int res = INF;
	for(int i = 0;i < 256;i++)res = min(res, dp[n][i]);
	cout << res << endl;
	return true;
}

int main(){
	while(solve());
	return 0;
}