#include "bits/stdc++.h"
using namespace std;
int C[16];
int x[20001];
int dp[20001][256];
const int INF = 1 << 30;
int main() {
	int N, M;
	while (cin >> N >> M, N) {
		for (int i = 0; i < M; i++) {
			cin >> C[i];
		}
		for (int i = 0; i < N; i++) {
			cin >> x[i];
		}
		for (int i = 0; i <= N; i++) {
			for (int j = 0; j < 256; j++) dp[i][j] = INF;
		}
		dp[0][128] = 0;
		for (int i = 0; i < N; i++) {
			for (int j = 0; j < 256; j++) {
				if (dp[i][j] == INF) continue;
				for (int k = 0; k < M; k++) {
					int nj = j + C[k];
					if (0 > nj) nj = 0;
					if (nj > 255) nj = 255;
					int tmp = (nj - x[i])*(nj - x[i]);
					if (dp[i][j] + tmp < dp[i+1][nj]) {
						dp[i+1][nj] = dp[i][j] + tmp;
					}
				}
			}
		}
		int ans = INF;
		for (int i = 0; i < 256; i++) {
			ans = min(ans, dp[N][i]);
		}
		cout << ans << endl;
	}
	
}