#include<bits/stdc++.h>
#define _ ios_base::sync_with_stdio(0);cin.tie(0);
#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;
typedef long long int ll;

const int MAX_C=16;
const int MAX_Y=256;
const int MAX_N=20000;
const ll INF=(ll)MAX_Y*MAX_Y*MAX_N;
ll c[MAX_C];
ll x[MAX_N];
ll dp[MAX_Y][MAX_N+1];
int n,m;

ll sq(ll a){return a*a;}
void solve(){
  REP(i,MAX_Y) dp[i][n]=0;
  for(int j=n-1;j>=0;j--){
    REP(i,MAX_Y){
      dp[i][j]=INF;
      REP(k,m){
	int yn1 = i+c[k]; yn1=min(max(yn1,0),MAX_Y-1);
	dp[i][j]=min(dp[i][j],dp[yn1][j+1]+sq(i-x[j]));
      }
    }
  }
  ll result=INF;
  REP(k,m){
    int yn1=128+c[k]; yn1=min(max(yn1,0),MAX_Y-1);
    result=min(result,dp[yn1][0]);
  }
  cout<<result<<endl;
}


int main(){ _;
  while(cin>>n>>m,(n|m)!=0){
    REP(i,m) cin>>c[i];
    REP(i,n) cin>>x[i];
    solve();
  }
}