#include <iostream>
#include <algorithm>

#define INF (1 << 29)

#define MAX_N 20000
#define MAX_M 16

using namespace std;

int N, M, C[MAX_M], X, dp[MAX_N + 1][256];

int main()
{
	while (true)
	{
		for (int i = 0; i < MAX_N + 1; i++)
		{
			for (int j = 0; j < 256; j++)
			{
				dp[i][j] = INF;
			}
		}

		dp[0][128] = 0;

		cin >> N >> M;

		if (N == 0 && M == 0) { break; }

		for (int i = 0; i < M; i++) { cin >> C[i]; }

		for (int i = 0; i < N; i++)
		{
			cin >> X;

			for (int j = 0; j < 256; j++)
			{
				for (int k = 0; k < M; k++)
				{
					int ptr = max(j + C[k], 0);

					ptr = min(ptr, 255);

					int sabun = (X - ptr) * (X - ptr);

					dp[i + 1][ptr] = min(dp[i + 1][ptr], dp[i][j] + sabun);
				}
			}
		}

		cout << *min_element(dp[N], dp[N] + 256) << endl;
	}

	return 0;
}