#include <bits/stdc++.h>
using namespace std;
int main(void) {
  cout << INT32_MAX << endl;
  int N, M;
  while (cin >> N >> M, N) {
    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<int> > dp(256, vector<int>(N + 1));
    for (int i = 0; i < 256; ++i) {
      for (int j = 0; j < N + 1; ++j) {
        dp[i][j] = INT32_MAX;
      }
    }
    dp[128][0] = 0;
    for (int j = 0; j < N; ++j) {
      for (int i = 0; i < 256; ++i) {
        if (dp[i][j] != INT32_MAX) {
          for (int k = 0; k < M; ++k) {
            if (C[k] + i > 255) {
              dp[255][j + 1] =
                  min(dp[255][j + 1], dp[i][j] + (int)pow(x[j] - 255, 2));
            } else if (C[k] + i < 0) {
              dp[0][j + 1] =
                  min(dp[0][j + 1], dp[i][j] + (int)pow(x[j] - 0, 2));
            } else {
              dp[C[k] + i][j + 1] = min(
                  dp[C[k] + i][j + 1], dp[i][j] + (int)pow(x[j] - C[k] - i, 2));
            }
          }
        }
      }
    }
    int res = INT32_MAX;
    for (int i = 0; i < 256; ++i) {
      if (dp[i][N] < res) {
        res = dp[i][N];
      }
    }
    cout << res << endl;
  }
  return 0;
}
