#include<iostream>
#include<algorithm>

using namespace std;

#define rep(i,n) for(int i=0;i<n;i++)
#define SQ(n) ((n)*(n))
const int inf=1<<28;

int n,m,code[16],in[20000];
int dp[2][256];

int func(int n)
{
	if(n<0)return 0;
	return min(255,n);
}

int main(){
	while(cin>>n>>m,n)
	{
		rep(i,m)cin>>code[i];
		rep(i,n)cin>>in[i];
		
		fill_n(dp[0],256,inf);
		dp[0][128]=0;
		
		int cur=0,next=1;
		for(int i=1;i<=n;i++)
		{
			fill_n(dp[next],256,inf);
			rep(j,256)rep(k,m)dp[next][func(j+code[k])]=min(
				dp[next][func(j+code[k])],dp[cur][j]+
				SQ(func(j+code[k])-in[i-1])
			);
			swap(cur,next);
		}
		
		cout<<*min_element(dp[cur],dp[cur]+256)<<endl;
	}
	return 0;
}