#include <iostream>

using namespace std;

long long int ans;

void function(int y,int index,int *c,int *x,int n,int m,int now){

	//cout<<y<<" "<<index<<" "<<n<<" "<<m<<" "<<now<<endl;

	if(now>=ans){
		return;
	}
	if(index>=n){
		if(ans>=now){
			ans = now;
		}
		return;
	}
	for(int i=0;i<m;i++){
		int nextY = y+c[i];
		if(nextY>=255)nextY=255;
		else if(nextY<=0)nextY=0;
		function(nextY,index+1,c,x,n,m,now+(nextY-x[index])*(nextY-x[index]));
	}
}

int min(int a,int b){
	if(a>=b)return b;
	else return a;
}

int main(){

	int n,m;
	long long int dp[20][256];
	int next;

	while(cin>>n>>m){
		ans = 1000000000000000;
		if(n==0 && m==0){
			break;
		}
		int *c;
		int *x;
		c = new int[m];
		x = new int[n];

		for(int i=0;i<m;i++){
			cin>>c[i];
		}
		for(int i=0;i<n;i++){
			cin>>x[i];
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<256;j++){
				dp[i][j]=10000000000;
			}
		}

		for(int i=0;i<m;i++){
			next = 128+c[i];
			if(next>=255)next=255;
			if(next<=0)next = 0;
			dp[0][next] = (x[0]-next)*(x[0]-next);
		}
		/*
		for(int i=0;i<256;i++){
			cout<<dp[0][i]<<" ";
		}
		cout<<endl;
		*/
		for(int i=1;i<n;i++){
			for(int j=0;j<256;j++){
				for(int k=0;k<m;k++){
					next = j+c[k];
					if(next>=255)next=255;
					if(next<=0)next = 0;
					//dp[next] = min(dp[next],dp[j]+(x[i]-next)*(x[i]-next));
					dp[i][next] = min(dp[i][next],dp[i-1][j]+(x[i]-next)*(x[i]-next));
				}
			}
		}
		for(int i=0;i<256;i++){
			ans = min(ans,dp[n-1][i]);
		}


		cout<<ans<<endl;

	}

	return 0;

}