#include<bits/stdc++.h>
using namespace std;
#define lp(i,n) for(int i=0;i<n;i++)
#define lpin(a,i,n) for(int i=0;i<n;i++){cin>>a[i];}
int dp[25000][300];
int  main(){
  while(1){
    int n,m;
    cin>>n>>m;
    int c[20],x[25000];
    if(n==0&& m==0) break;
    lp(i,25000){
      lp(j,300){
	dp[i][j]=-1;
      }
    }
    lpin(c,i,m);
    lpin(x,i,n);
    dp[0][128]=0;
    for(int i=1;i<=n;i++){
      lp(j,256){
	if(dp[i-1][j]!=-1){
	  lp(k,m){
	    if(dp[i][max(min(j+c[k],255),0)]==-1){
	      dp[i][max(min(j+c[k],255),0)]=dp[i-1][j]+(max(min(j+c[k],255),0)-x[i-1])*(max(min(j+c[k],255),0)-x[i-1]);
	    }
	    else
	    dp[i][max(min(j+c[k],255),0)]=min(dp[i-1][j]+(max(min(j+c[k],255),0)-x[i-1])*(max(min(j+c[k],255),0)-x[i-1]),dp[i][max(min(j+c[k],255),0)]);
	  }
	}
      }
    }
    int ans=-1;
    lp(i,300){
      if(dp[n][i]!=-1){
	if(ans==-1)
	  ans=dp[n][i];
	else ans=min(ans,dp[n][i]);
      }
    }
    cout<<ans<<endl;
  }
  return 0;
}

