#include <stdio.h>
#include <string.h>

#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define sq(x) ((x) * (x))

#define INF 1000000000

int dp[20001][256];

int main(void) {
	int i, j, k;
	int n, m;

	while (scanf("%d %d", &n, &m), n) {
		int c[16];
		int min;

		for (i = 0; i < n + 1; i++)
			for (j = 0; j < 256; j++)
				dp[i][j] = INF;

		for (i = 0; i < m; i++)
			scanf("%d", c + i);

		dp[0][128] = 0;
		for (i = 0; i < n; i++) {
			int x;
			scanf("%d", &x);
			for (j = 0; j < 256; j++)
				for (k = 0; k < m; k++) {
					int idx = max(0, min(255, j + c[k]));
					dp[i + 1][idx] = min(dp[i + 1][idx], dp[i][j] + sq(idx - x));
				}
		}

		min = INF;
		for (i = 0; i < 256; i++)
			min = min(min, dp[n][i]);

		printf("%d\n", min);
	}

	return 0;
}