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

int main() {
	while (1) {
		int n, m; cin >> n >> m;
		if (n==0 && m==0) return 0;
		vector<ll> c(m); for (int i=0; i<m; i++) cin >> c[i];
		vector<ll> x(n); for (int i=0; i<n; i++) cin >> x[i];
		vector<vector<ll> > dp(n+1, vector<ll>(256, 1000000000000));
		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)
				{
					if (j+c[k] < 0) dp[i+1][0] = min(dp[i+1][0], dp[i][j]+x[i]*x[i]);
					else if (j+c[k] > 255) dp[i+1][255] = min(dp[i+1][255], dp[i][j]+(255-x[i])*(255-x[i]));
					else dp[i+1][j+c[k]] = min(dp[i+1][j+c[k]], dp[i][j]+(x[i]-(j+c[k]))*(x[i]-(j+c[k])));
				}
			}
		}
		ll ans = 1000000000000;
		for (int j = 0; j < 256; ++j)
		{
			ans = min(ans, dp[n][j]);
		}
		cout << ans << endl;
	}
}
