#include<bits/stdc++.h>
using namespace std;
static const int MAX_N = 20000;
static const int MAX_M = 16;
static const int MAX_Y = 256;
static const int INF = 1 << 30;

int N, M;
int C[MAX_M], x[MAX_N];

int memo[MAX_Y][MAX_N + 1];

int main(){
	for(;;){
		scanf("%d %d", &N, &M);
		if(N == 0 && M == 0) break;
		for(int i = 0; i < M; i++) scanf("%d", &C[i]);
		for(int i = 0; i < N; i++) scanf("%d", &x[i]);
		fill(memo[0], memo[MAX_Y], INF);
		memo[128][0] = 0;
		for(int i = 0; i < N; i++){
			for(int j = 0; j < MAX_Y; j++){
				if(memo[j][i] == INF) continue;
				for(int k = 0; k < M; k++){
					int num = min(255, max(0, j + C[k]));
					memo[num][i + 1] = min(memo[num][i + 1], memo[j][i] + (num - x[i]) * (num - x[i]));
				}
			}
		}
		int res = INF;
		for(int i = 0; i < MAX_Y; i++) res = min(res, memo[i][N]);
		printf("%d\n", res);
	}
	return 0;
}

