#include<iostream>
#include<vector>
#include<algorithm>

using namespace std;
int N,M;
vector<int> C,X;

void input(){
  C.resize(M);
  X.resize(N);
  for(int i = 0; i < M; i++) cin >> C[i];
  for(int i = 0; i < N; i++) cin >> X[i];
}

void solve(){
  const int Row = 2;
  const int Col = 256;
  const long long INF = (1LL<<50);
  
  long long dp[Row][Col];
  fill(dp[0],dp[0]+Row*Col,INF);

  dp[0][128] = 0;

  for(int i = 0; i < N; i++){
    for(int pos = 0; pos < Col; pos++) dp[(i+1)%2][pos] = INF;

    for(int j = 0; j < Col; j++){
      if(dp[i%2][j] == INF) continue;
      for(int k = 0; k < M; k++){
	int nex = j+C[k];
	nex = min(255,nex);
	nex = max(0,nex);
	dp[(i+1)%2][nex] = min(dp[(i+1)%2][nex],dp[i%2][j]+(X[i]-nex)*(X[i]-nex));
      }
    }
  }

  long long ans = INF;

  for(int i = 0; i < Col; i++) ans = min(ans,dp[N%2][i]);;
  cout << ans << endl;
}

int main(){
  while(cin >> N >> M && N+M){
    input();
    solve();
  }
  return 0;
}