#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

template<class T> inline void chmin(T& a, const T& b) { if(b < a) a = b; }

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

	const int INF = 0xfffffff;

	for(int n, m; cin >> n >> m, n;) {
		vector<int> c(m);
		for(int i = 0; i < m; ++i)
			cin >> c[i];

		vector<int> dp(256, INF);
		dp[128] = 0;
		for(int i = 0; i < n; ++i) {
			int x;
			cin >> x;

			vector<int> tmp(256, INF);
			for(int j = 0; j < 256; ++j) {
				for(int k = 0; k < m; ++k) {
					const int next = max(0, min(255, j + c[k]));
					const int diff = x - next;
					chmin(tmp[next], dp[j] + diff * diff);
				}
			}

			dp.swap(tmp);
		}

		cout << *min_element(dp.begin(), dp.end()) << endl;
	}

	return EXIT_SUCCESS;
}