#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 2000000000;

int main()
{
	ll N, M;
	while (cin >> N >> M, N | M) {
		vector<ll> C(M), x(N);
		for (int i = 0; i < M; i++) {
			cin >> C[i];
		}
		sort(C.begin(), C.end());
		for (int i = 0; i < N; i++) {
			cin >> x[i];
		}
		vector<vector<ll>> dp(N + 1, vector<ll>(256, INF));
		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++) {
					ll id = min(255ll, max(0ll, j + C[k])), d = id - x[i]; d *= d;
					dp[i + 1][id] = min(dp[i + 1][id], dp[i][j] + d);
				}
			}
		}
		ll res = INF;
		for (int i = 0; i < 256; i++) {
			res = min(res, dp[N][i]);
		}
		cout << res << endl;
	}
	return 0;
}