#include <iostream>
#define INF 1<<30
using namespace std;

int N,M;
int c[17];
int dp[20002][256];
int main(){
	int num,cost;
	while(cin >> N >> M){
		if(N == 0)break;
		for(int i = 0;i < M;i++){
			cin >> c[i];
		}
		for(int i = 0;i <= N;i++){
			for(int j = 0;j < 256;j++){
				dp[i][j] = INF;
			}
		}
			dp[0][128] = 0;
		for(int i = 0;i < N;i++){
			cin >> num;
			for(int j = 0;j <= 255;j++){
				if(dp[i][j] == INF)continue;
				for(int m = 0;m < M;m++){
					int x = j + c[m];
					if(x > 255)x = 255;
					else if(x < 0)x = 0;
					cost = (num-x)*(num-x);
					dp[i+1][x] = min(dp[i+1][x],dp[i][j] + cost);
				}
			}
		}
		cost = INF;
		for(int i = 0;i <= 255;i++){
			cost = min(cost,dp[N][i]);
		}
		cout << cost <<endl;
	}
	return 0;
}