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

Int solve(Int n, Int m){
	vector<Int> a(m), b(n);
	for(auto &i:a) cin >> i;
	for(auto &i:b) cin >> i;
	vector<vector<Int>> dp(n+1, vector<Int>(256, 1e10));
	dp[0][128] = 0;
	for(Int i=0; i<n; ++i){
		for(Int j=0; j<256; ++j){
			for(Int k=0; k<m; ++k){
				Int l = j + a[k];
				if(l < 0) l = 0;
				if(l > 255) l = 255;
				dp[i+1][l] = min(dp[i+1][l], dp[i][j] + (l-b[i])*(l-b[i]));
			}
		}
	}
	Int ans = 1e10;
	for(Int i=0; i<256; ++i) ans = min(ans, dp[n][i]);
	return ans;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	while(1){
		Int n, m; cin >> n >> m;
		if(n == 0) break;
		cout << solve(n, m) << "\n";
	}
}
