#include <iostream>
#include <algorithm>

using namespace std;

const int MAX_N = 20000;
const int MAX_M = 16;
const int INF = 1 << 29;

int C[MAX_M];
int X[MAX_N];
int dp[2][256];

int sub_pow ( int a, int b )
{
	int c = a - b;
	return c * c;
}

int main ( void )
{
	int n, m;
	while (cin >> n >> m, n|m) {
		for (int i = 0; i < 2; ++i) {
			fill(dp[i], dp[i]+256, INF);
		}
		for (int i = 0; i < m; ++i) {
			cin >> C[i];
		}
		for (int i = 0; i < n; ++i) {
			cin >> X[i];
		}
		dp[0][128] = 0;
		for (int i = 0; i < n; ++i) {
			int a = i & 1, b = (i + 1) & 1;
			fill(dp[b], dp[b]+256, INF);
			for (int j = 0; j < 256; ++j) {
				if (dp[a][j] == INF) { continue; }
				for (int k = 0; k < m; ++k) {
					int x = min(max(0, j + C[k]), 255);
					dp[b][x] = min(dp[b][x], dp[a][j] + sub_pow(x, X[i]));
				}
			}
		}
		int ans = INF;
		for (int i = 0; i < 256; ++i) {
			ans = min(ans, dp[n & 1][i]);
		}
		cout << ans << endl;
	}
	return 0;
}