#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <iomanip>
using namespace std;

typedef long long ll;
const ll INF = 1LL << 50;
ll dp[20010][260];
ll c[16];
ll x[20000];

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);

	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];
		}

		fill((ll*)begin(dp), (ll*)end(dp), INF);
		dp[0][128] = 0;

		for(int i = 0; i < n; i++) {
			for(int j = 0; j <= 255; j++) {
				for(int k = 0; k < m; k++) {
					int nj = max(min(j + c[k], 255LL), 0LL);
					dp[i + 1][nj] = min(dp[i + 1][nj], dp[i][j] + (x[i] - nj) * (x[i] - nj));
				}
			}
		}

		ll ans = INF;
		for(int j = 0; j <= 255; j++) {
			ans = min(ans, dp[n][j]);
		}
		cout << ans << endl;
	}

}