#include <bits/stdc++.h>
using namespace std;

int	main()
{
	int	N, M;
	while (cin >> N >> M) {
		if (N == 0 && M == 0) break;
		vector<vector<int>>	cost(N + 1, vector<int>(256, -1));
		cost[0][128] = 0;
		vector<int>	C_vec(M), x_vec(N);
		for (int i = 0; i < M; ++i)
			cin >> C_vec.at(i);
		for (int i = 0; i < N; ++i)
			cin >> x_vec.at(i);
		int	min_cost = -1;
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < 256; ++j) {
				if (cost[i][j] == -1) continue;
				for (int k = 0; k < M; ++k) {
					int	l = j + C_vec.at(k);
					if (l < 0) l = 0;
					if (l > 255) l = 255;
					int	cost_l = (l - x_vec.at(i)) * (l - x_vec.at(i));
					cost_l += cost[i][j];
					if (cost[i + 1][l] == -1 || cost[i + 1][l] > cost_l) {
						cost[i + 1][l] = cost_l;
						if (i + 1 == N) {
							if (min_cost == -1 || min_cost > cost_l)
								min_cost = cost_l;
						}
					}
				}
			}
		}
		cout << min_cost << endl;
	}
}

