#include <iostream>
#include <algorithm>
#define int long long
using namespace std;

int n, m;
int c[16];
int x[20000];
int dp[20001][256];

signed main() {
	int i, j, k;
	
	while (cin >> n >> m) {
		if (!n) break;
		for (i = 0; i < m; i++) cin >> c[i];
		for (i = 0; i < n; i++) cin >> x[i];
		for (i = 0; i <= n; i++) for (j = 0; j < 256; j++) dp[i][j] = 1145141919810;
		
		dp[0][128] = 0;
		for (i = 0; i < n; i++) {
			for (j = 0; j < 256; j++) {
				for (k = 0; k < m; k++) {
					int ny = max(0LL, min(255LL, j + c[k]));
					dp[i + 1][ny] = min(dp[i + 1][ny], dp[i][j] + (x[i] - ny) * (x[i] - ny));
				}
			}
		}
		
		int ans = 1145141919810;
		for (i = 0; i < 256; i++) ans = min(ans, dp[n][i]);
		cout << ans << endl;
	}
	return 0;
}