#include <bits/stdc++.h>
using namespace std;
long long INF = 1000000000000;
int main(){
  while (1){
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0){
      break;
    }
    vector<int> C(M);
    for (int i = 0; i < M; i++){
      cin >> C[i];
    }
    vector<int> x(N);
    for (int i = 0; i < N; i++){
      cin >> x[i];
    }
    vector<vector<long long>> dp(N + 1, vector<long long>(256, INF));
    dp[0][128] = 0;
    for (int i = 0; i < N; i++){
      for (int j = 0; j < 256; j++){
        for (int k = 0; k < M; k++){
          int next = min(max(j + C[k], 0), 255);
          dp[i + 1][next] = min(dp[i + 1][next], dp[i][j] + abs(next - x[i]) * abs(next - x[i]));
        }
      }
    }
    long long ans = INF;
    for (int i = 0; i < 256; i++){
      ans = min(ans, dp[N][i]);
    }
    cout << ans << endl;
  }
}
