#include "bits/stdc++.h"

using namespace std;

const long INF = 1e15;

void solve(void)
{
	int n, m;
	const int s = 256;
	while(cin >> n >> m)
	{
		if (!n && !m) break;
		vector<int> cs(m);
		for (int& c : cs) cin >> c;
		vector<long> dp(s, INF);
		dp[128] = 0;
		for (int i = 0; i < n; i++)
		{
			int x;
			cin >> x;
			vector<long> nx(s, INF);
			for (int j = 0; j < s; j++)
			{
				if (dp[j] == INF) continue;
				for (int c : cs)
				{
					int k = j + c;
					if (k < 0) k = 0;
					if (k >= s) k = s-1;
					nx[k] = min(nx[k], dp[j] + (x-k)*(x-k));
				}
			}
			swap(dp, nx);
		}
		cout << *min_element(dp.begin(), dp.end()) << '\n';
	}
}

int main()
{
	solve();
	//cout << "yui(*-v・)yui" << endl;
	return 0;
}


