#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main(){
  int N, M;
  while(cin >> N >> M, N){
    int C[M];
    long long int INF = 10000000000LL;
    for(int i = 0; i < M; ++i)
      cin >> C[i];
    sort(C,C+M);
    int x[N];
    for(int i = 0; i < N; ++i)
      cin >> x[i];
    vector< vector<long long int> > dp(N, vector<long long int>(256,INF));
    for(int i = 0; i < M; ++i){
      int t = max(min(255,128+C[i]),0);
      dp[0][t] = (long long int)(x[0]-t)*(x[0]-t);
    }
    for(int i = 1; i < N; ++i){
      for(int j = 0; j < 256; ++j){
	if(dp[i-1][j] == INF) continue;
	for(int k = 0; k < M; ++k){
	  int t = max(min(255,j+C[k]),0);
	  dp[i][t] = min(dp[i][t], dp[i-1][j] + (long long int)(x[i]-t)*(x[i]-t));
	  if(j + C[k] > 255) break;
	}
      }
    }

    long long int ans = INF;
    for(int i = 0; i < 256; ++i){
      ans = min(ans, dp[N-1][i]);
    }
    cout << ans << endl;
  }
  return 0;
}