#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i,n) for(ll i=0;i<ll(n);++i)
#define FOR(i,a,b) for(ll i=(a);i<ll(b);++i)

int main(){
  ll n,m;
  ll inf = ll(1)<<50;
  while(cin>>n>>m,n){
    vector<ll> c(m);
    rep(i,m) cin>>c[i];
    vector<ll> x(n);
    rep(i,n) cin>>x[i];

    vector<vector<ll>> dp(n+1,vector<ll>(256,inf));
    dp[0][128] = 0;

    rep(i,n){
      rep(j,256){
	rep(k,m){
	  ll nj = j + c[k];
	  if(nj < 0) nj = 0;
	  else if(255 < nj) nj = 255;
	  dp[i+1][nj] = min(dp[i+1][nj], dp[i][j]+(x[i]-nj)*(x[i]-nj));
	}
      }
    }

    ll ans = inf;
    rep(j,256) ans = min(ans, dp[n][j]);
    cout << ans << endl;
  }
}
	
      

