#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++){
		function(y+c[i],index+1,c,x,n,m,now+(y+c[i]-x[index])*(y+c[i]-x[index]));
	}
}

int main(){

	int n,m;
	ans = 1000000000000000;

	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];
		}
		function(128,0,c,x,n,m,0);
		cout<<ans<<endl;

	}

	return 0;

}