#include <bits/stdc++.h>
using namespace std;

int main(void){
	int n, m;
	while(1){
		cin >> n >> m;
		if(!n&&!m) break;
		vector<int> c(m), x(n);
		for(int i = 0; i < m; ++i){
			cin >> c[i];
		}
		for(int i = 0; i < n; ++i){
			cin >> x[i];
		}
		vector<vector <int>> dp(n+2);
		for(int i = 0; i < n+2; ++i){
			dp[i].resize(256);
			for(int j = 0; j < 256; ++j){
				dp[i][j] = INT_MAX;
			}
		}
		dp[0][128]=0;
		for(int i = 1; i <= n; ++i){
			for(int j = 0; j < m; ++j){
				for(int k = 0; k < 256; ++k){
					if(dp[i-1][k]==INT_MAX) continue;
					int next = k+c[j];
					if(next > 255) next = 255;
					if(next < 0) next = 0;
					dp[i][next] = min(dp[i][next], dp[i-1][k] + (int)pow(x[i-1]-next,2));
				}
			}
		}
		int ans = INT_MAX;
		for(int i = 0; i < 256; ++i){
			ans = min(ans, dp[n][i]);
			//cout << dp[n][i] << " ";
		}
		cout << ans << endl;
	}
	return 0;
}
