#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  while (cin >> N >> M, N) {
    long long int C[M];
    long long int INF = 10000000000LL;
    for (int i = 0; i < M; ++i) cin >> C[i];
    sort(C, C + M);
    long long int x[N];
    for (int i = 0; i < N; ++i) cin >> x[i];
    long long int dp[N][256];
    for (int i = 0; i < N; ++i) {
      fill(dp[i], dp[i] + 256, INF);
    }
    for (int i = 0; i < M; ++i) {
      long long int t = max(min(255LL, 128LL + C[i]), 0LL);
      dp[0][t] = (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) {
          long long int t = max(min(255LL, (long long int)j + C[k]), 0LL);
          dp[i][t] = min(dp[i][t], dp[i - 1][j] + (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;
}
