#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  int N, M;
  while (true) {
    cin >> N >> M;
    if (!N && !M) break;
    vector<long long int> C(M), x(N + 1);
    for (int i = (0); i < (M); ++i) cin >> C[i];
    for (int i = (1); i < (N + 1); ++i) cin >> x[i];
    vector<vector<long long int>> dp(N + 1, vector<long long int>(256, INF_));
    dp[0][128] = 0;
    long long int ans = INF_;
    for (int i = (0); i < (N + 1); ++i) {
      for (int j = (0); j < (256); ++j) {
        if (i != N) {
          for (int k = (0); k < (M); ++k) {
            long long int v = j + C[k];
            if (v < 0) v = 0;
            if (v > 256) v = 255;
            dp[i + 1][v] =
                min(dp[i + 1][v], dp[i][j] + (v - x[i + 1]) * (v - x[i + 1]));
          }
        }
        if (i == N) ans = min(ans, dp[i][j]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
