#include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

using namespace std;
LL dp[256][2];
int d(int v){
  return v*v;
}
int ran(int v){
  if(v<0) return 0;
  return min(255,v);
}
int main(void)
{
  for(;;){
    int n,m;
    cin >> n >> m;
    if(!n&&!m) return 0;
    vector<int> alt;
    alt.resize(m);
    REP(i,m) cin >> alt[i];
    REP(i,256) dp[i][0]=1147483647;
    dp[128][0]=0;
    REP(i,n){
      int x;
      cin >> x;
      REP(j,256) dp[j][(i+1)%2]=2147483647;
      REP(j,256){
	REP(k,m){
	  int nv=ran(j+alt[k]);
	  dp[nv][(i+1)%2]=min(dp[nv][(i+1)%2],dp[j][i%2]+d(nv-x));
	}
      }
    }
    LL answer=2147483647;
    REP(i,256) answer=min(answer,dp[i][n%2]);
    cout << answer << endl;
    
  }
}