#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 28;
int rnd(int v){
	if (v < 0) return 0;
	if (v > 255) return 255;
	return v;
}
int diff(int a, int b){
	return (a - b) * (a - b);
}

int n, m;
int c[16];
int x[20000];
int dp[20001][256];

int main()
{
	while (scanf("%d %d", &n, &m), n){
		fill((int*)dp, (int*)dp + sizeof(dp) / sizeof(int), INF);
		
		for (int i = 0; i < m; i++){
			scanf("%d", &c[i]);
		}
		for (int i = 0; i < n; i++){
			scanf("%d", &x[i]);
		}
		
		dp[0][128] = 0;
		for (int i = 0; i < n; i++){
			for (int j = 0; j < 256; j++){
				for (int k = 0; k < m; k++){
					int to = rnd(j + c[k]);
					dp[i + 1][to] = min(dp[i + 1][to], dp[i][j] + diff(to, x[i]));
				}
			}
		}
		
		int mini = INF;
		for (int i = 0; i < 256; i++){
			mini = min(mini, dp[n][i]);
		}
		
		printf("%d\n", mini);
	}
}