#include "bits/stdc++.h"
#include "math.h"
 
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vin;
typedef vector<vin> vvin;
typedef pair<ll,ll> P;
typedef vector<P> vp;
#define rep(i,a,b) for(ll i=(a);i<(b);++i)
#define drep(i,a,b) for(ll i=(a);i>=(b);--i)
#define SIZE(a) int((a).size())
const int MOD=1e9+7;

int main(){

  ll n=1,m=1;
  while(n!=0){
  cin>>n>>m;ll ans=MOD;
  if(n==0&&m==0){break;}
  vll c(m),x(n);
  vvll dp(n+1,vll(256,MOD));
  rep(i,0,m){cin>>c[i];}
  rep(i,0,n){cin>>x[i];}
  dp[0][128]=0;
  rep(i,0,n){
    rep(j,0,256){
      rep(k,0,m){
        int yn=j+c[k];
        if(yn>=255){yn=255;}
        if(yn<=0){yn=0;}
        dp[i+1][yn]=min(dp[i+1][yn],dp[i][j]+(yn-x[i])*(yn-x[i]));
      }
    }
  }
  rep(i,0,256){
    ans=min(dp[n][i],ans);
  }
  cout<<ans<<endl;
  }
}

