#include <bits/stdc++.h>
int dx[4] = {0, -1, 1, 0}, dy[4] = {-1, 0, 0, 1};
using namespace std;
const int INF = 1e9;
const double EPS = 1e-6;
int round(int y) {
  y = min(y, 255);
  y = max(y, 0);
  return y;
}
int sq(int x) { return x * x; }
int dp[20000][256];
int main() {
  int N, M;
  while (cin >> N >> M, N) {
    vector<int> C(M), x(N);
    for (int i = 0; i < (int)(M); i++) cin >> C[i];
    for (int i = 0; i < (int)(N); i++) cin >> x[i];
    fill(dp[0], dp[0] + 20000 * 16, INF);
    for (int k = 0; k < M; k++) {
      int y = round(128 + C[k]);
      dp[0][y] = sq(x[0] - y);
    }
    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 y = round(j + C[k]);
          dp[i][y] = min(dp[i][y], dp[i - 1][j] + sq(x[i] - y));
        }
      }
    }
    int ans = INF;
    for (int j = 0; j < 256; j++) {
      ans = min(dp[N - 1][j], ans);
    }
    cout << ans << endl;
  }
}
