#include <iostream>
#include <vector>

using namespace std;

const int INF = 1000000000;

int clamp(int x){
	if(x < 0){ return 0; }
	if(x >= 256){ return 255; }
	return x;
}

int pow2(int x){ return x * x; }

int main(){
	while(true){
		int n, m;
		cin >> n >> m;
		if(n == 0 && m == 0){ 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 + 1, vector<int>(256, INF));
		dp[0][128] = 0;
		for(int i = 0; i < n; ++i){
			for(int j = 0; j < 256; ++j){
				if(dp[i][j] >= INF){ continue; }
				for(int k = 0; k < m; ++k){
					int y = clamp(j + c[k]);
					dp[i + 1][y] = min(
						dp[i + 1][y], dp[i][j] + pow2(y - x[i]));
				}
			}
		}
		int answer = INF;
		for(int i = 0; i < 256; ++i){
			answer = min(answer, dp[n][i]);
		}
		cout << answer << endl;
	}
	return 0;
}