#include<bits/stdc++.h>
using namespace std;

const int Val=256;

long long int square(long long int val){return val*val;}

int main(){
  int n,m;
  while(cin>>n>>m,n){
    vector<int> c(m);
    for(int i=0;i<m;i++) cin>>c[i];
    vector<int> x(n);
    for(int i=0;i<n;i++) cin>>x[i];
    vector<vector<long long int>> dp(n+1,vector<long long int>(Val,(long long int)(1e13)));
    dp[0][128]=0;
    for(int i=0;i<n;i++){
      for(int j=0;j<Val;j++){
	//renew dp[i+1][j]
	for(int k=0;k<m;k++){
	  if(0<=j-c[k] && j-c[k]<Val) {dp[i+1][j]=min(dp[i+1][j],dp[i][j-c[k]]+square(x[i]-j));}
	  if(j==0){
	    for(int r=0;r<=-c[k];r++){
	      dp[i+1][j]=min(dp[i+1][j],dp[i][r]+square(x[i]-j));
	    }

	  }
	  if(j==Val-1){
	    for(int r=Val-1;r>=Val-1-c[k];r--){
	      dp[i+1][j]=min(dp[i+1][j],dp[i][r]+square(x[i]-j));
	    }
	  }
	}
      }
    }
    cout<<*min_element(dp[n].begin(),dp[n].end())<<endl;

  }
  
  return 0;
}