#include <iostream>
#include <algorithm>
#define rep(i, n) for(i = 0; i < n; i++)
#define int long long
using namespace std;

void chmin(int &a, int b) { a = min(a, b); }

int INF = 1e+15;
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;
		rep(i, m) cin >> c[i];
		rep(i, n) cin >> x[i];
		
		rep(i, n + 1) rep(j, 256) dp[i][j] = INF;
		dp[0][128] = 0;
		
		rep(i, n) {
			rep(j, 256) {
				rep(k, m) {
					int y = j + c[k];
					if (y < 0) y = 0;
					if (y > 255) y = 255;
					chmin(dp[i + 1][y], dp[i][j] + (x[i] - y) * (x[i] - y));
				}
			}
		}
		
		int ans = INF;
		rep(i, 256) chmin(ans, dp[n][i]);
		cout << ans << endl;
	}
	return 0;
}
