#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int square(int x) {
	return x * x;
}

int main() {
	int n, m; 
	while (cin >> n >> m, n) {
		vector<int> c(m), x(n);
		for (int i = 0; i < m; ++i) cin >> c[i];
		for (int i = 0; i < n; ++i) cin >> x[i];
		vector<vector<int> > dp(n + 1, vector<int>(256, 1e9));
		dp[0][128] = 0;
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < m; ++j) {
				for (int k = 0; k <= 255; ++k) {
					int next = min(255, max(0, k + c[j]));
					dp[i + 1][next] = min(dp[i + 1][next], dp[i][k] + square(next - x[i]));
				}
			}
		}
		int ans = 1e9;
		for (int i = 0; i <= 255; ++i) ans = min(ans, dp[n][i]);
		cout << ans << endl;
	}

	return 0;
}
