#include <bits/stdc++.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
using namespace std;
using ll = long long;
using pii = pair<int, int>;

template<class T> inline bool chmin(T &a, T b) {
	if (a > b) { a = b; return true; }
	else return false;
}

const ll INF = 1e18;

int main() {
	int n, m;
	while (cin >> n >> m) {
		if (n == 0 && m == 0) break;
		vector<int> c(m);
		rep(i, m) cin >> c[i];
		vector<int> x(n);
		rep(i, n) cin >> x[i];
		
		vector<vector<ll>> dp(n + 1, vector<ll>(256, INF));
		dp[0][128] = 0;
		
		rep(i, n) {
			rep(j, 256) {
				if (dp[i][j] == INF) continue;
				rep(k, m) {
					int y = max(0, min(255, j + c[k]));
					ll p = pow(y - x[i], 2);
					chmin(dp[i + 1][y], dp[i][j] + p);
				}
			}
		}
		
		ll ans = INF;
		rep(i, 256) chmin(ans, dp[n][i]);
		cout << ans << endl;
	}
	return 0;
}
