#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> mp;
ll inf =  1e9;


int main(){
 while(1){
  ll n,m;
  cin>>n>>m;
  if(n == 0 ) break;
  vector<ll> c(m),x(n);
  for(ll i=0;i<m;i++)cin>>c[i];
  for(ll i=0;i<n;i++)cin>>x[i];
  vector<vector<ll> > dp(n+1,vector<ll>(256,inf) );
  dp[0][128] = 0;
 //cout<<"OK"<<endl;
  for(ll i=0;i<n;i++){
   for(ll j=0;j<256;j++){
    if( dp[i][j] == inf ) continue;
    for(ll k=0;k<m;k++){
     //if( 0<= j+c[k] && j + c[k] < 256 ){
      ll next = min(max(0ll,j+c[k]),255ll);
      dp[i+1][next] = min( dp[i+1][next] , dp[i][j] + ( next - x[i] )*( next - x[i] ) );
     //}
 /*    if( j - c[k] >= 0 ){
      dp[i+1][j-c[k]] = min( dp[i+1][j-c[k]] , dp[i][j] + ( j-c[k] - x[i] )*( j-c[k] - x[i] ) );
     }*/
    }

   }
  }
  ll res = inf;
  for(ll i=0;i<256;i++) res = min( res , dp[n][i] );
  cout<<res<<endl;

 }
    return 0;
}

