#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = s; i < (int)(n); i++)
#define rep3(i, s, n) for (int i = s; i > (int)(n); i--)
#define all(obj) obj.begin(), obj.end()
#define from1(obj) obj.begin() + 1, obj.end()
#define db(x) cerr << #x << ":" << x << " "
#define dbl(x) cerr << #x << ":" << x << "\n"
#define dbp(p) cerr << #p << ":(" << p.first << ", " << p.second << ") "
#define dbpl(p) cerr << #p << ":(" << p.first << ", " << p.second << ")\n"
#define dbt3(t3) cerr << #t3 << ":(" << get<0>(t3) << ", " << get<1>(t3) << ", " << get<2>(t3) << ") "
#define dbt3l(t3) cerr << #t3 << ":(" << get<0>(t3) << ", " << get<1>(t3) << ", " << get<2>(t3) << ")\n"
#define dbt4(t4) cerr << #t4 << ":(" << get<0>(t4) << ", " << get<1>(t4) << ", " << get<2>(t4) << ", " << get<3>(t4) << ") "
#define dbt4l(t4) cerr << #t4 << ":(" << get<0>(t4) << ", " << get<1>(t4) << ", " << get<2>(t4) << ", " << get<3>(t4) << ")\n"
#define dbv(vec) cerr << #vec << ":{ "; for (auto e : vec) cerr << e << ", "; cerr << "}\n"
#define dbvv(vv) cerr << #vv << ":{\n"; for (auto vec : vv) { cerr << "{ "; for (auto e : vec) cerr << e << ", "; cerr << "}\n"; } cerr << "}\n"
#define dbm(map) cerr << #map << ":{ "; for (auto p : map) cerr << "(" << p.first << ", " << p.second << "), "; cerr << "}\n"
#define YN(f) cout << (f ? "YES" : "NO") << "\n"
#define Yn(f) cout << (f ? "Yes" : "No") << "\n"
#define yn(f) cout << (f ? "yes" : "no") << "\n"
using ll = long long;
using vi = vector<int>; using vl = vector<ll>; using vs = vector<string>; using vb = vector<bool>;
using vvi = vector<vi>; using vvl = vector<vl>; using vvb = vector<vb>;
using pii = pair<int, int>; using pil = pair<int, ll> ; using pll = pair<ll, ll>; using pli = pair<ll, int>;
using vpii = vector<pii>; using vpll = vector<pll>;
using ti3 = tuple<int, int, int>; using tl3 = tuple<ll, ll, ll>;
using ti4 = tuple<int, int, int, int>; using tl4 = tuple<ll, ll, ll, ll>;
using mii = map<int, int>; using mil = map<int, ll>; using mll = map<ll, ll>; using mli = map<ll, int>;

int main() {
	const int MAX = 255, INF = 1e9;
	while(true) {
		int N, M; cin >> N >> M;
		if (N == 0 and M == 0) break;
		// コードブック中の値の番号 k は0-ind( k = 0 ~ M-1 )
		vector<int> C(M); rep(k, M) cin >> C[k];
		// 信号及び復号化後の値の番号 i は1-ind( i = 1 ~ N )
		vector<int> x(N + 1); rep2(i, 1, N + 1) cin >> x[i]; // 1-ind
		// dbv(C); dbv(x);
		vvi dp(N + 1, vi(MAX + 1, INF)); // dp[i][j]:y[0]=128からy[i]までの列で，y[i]=jであるようなものの差y[i]-x[i]の二乗和の最小値
		dp[0][128] = 0;
		// 遷移:現在の値 j = y[i] に値 C[k] を加え，丸めた値を nj = min(max(j + C[k], 0), MAX) とする。
		// dp[i+1][nj] = min(dp[i+1][nj], dp[i][j] + (nj - x[i+1])^2) である。
		// rep(j, MAX + 1) if (dp[0][j] != INF) { dbp(pii(0, j)); dbl(dp[0][j]); }
		rep(i, N) { // i = 0 ~ N
			rep(j, MAX + 1) { // j = 0 ~ MAX(255)
				if (dp[i][j] == MAX) continue;
				rep(k, M) { // k = 0 ~ M - 1
					int nj = min(max(j + C[k], 0), MAX);
					dp[i + 1][nj] = min(dp[i + 1][nj], dp[i][j] + (nj - x[i + 1]) * (nj - x[i + 1]));
				}				
			}
			// rep(j, MAX + 1) if (dp[i + 1][j] != INF) { dbp(pii(i + 1, j)); dbl(dp[i + 1][j]); }
		}		
		cout << *min_element(all(dp[N])) << endl;
	}
}


Measured memory_bytes: 24288