#include<bits/stdc++.h>
using namespace std;
#define ll long long
typedef pair<ll,ll> P;
typedef pair<ll,P> PP;
const ll MOD = 1e9+7;
const ll INF = 1e18;
ll dp[20010][300];

int main(){
  ll n,m;
  while(cin>>n>>m,n){
	ll c[22],x[20010];
	for(int i=0;i<m;i++)cin>>c[i];
	for(int i=0;i<n;i++)cin>>x[i];
  
	fill_n(*dp,20010*300,INF);

	dp[0][128]=0;
	for(int i=0;i<n;i++){
	  for(int j=0;j<256;j++){
		if(dp[i][j]==INF)continue;
		for(int k=0;k<m;k++){
		  int nj=j+c[k];
		  nj=min(nj,255);
		  nj=max(nj,0);
		  dp[i+1][nj]=min(dp[i+1][nj],dp[i][j]+(nj-x[i])*(nj-x[i]));
		}
	  }
	}

	ll ans=INF;
	for(int i=0;i<256;i++){
	  ans=min(ans,dp[n][i]);
	}
	cout<<ans<<endl;
  }
  return 0;
}

