#include <bits/stdc++.h>
int square(int x) { return x * x; }
int main() {
  using namespace std;
  int N, M, i, j, c, k, v, y, sub_sq, min;
  vector<int> C, x;
  unordered_map<int, int>::iterator it;
  while (true) {
    cin >> N >> M;
    if (N == 0 and M == 0) break;
    C.resize(M);
    x.resize(N);
    for (i = 0; i < M; i++) cin >> C[i];
    for (i = 0; i < N; i++) cin >> x[i];
    unordered_map<int, int> dp;
    dp[128] = 0;
    for (i = 0; i < N; i++) {
      unordered_map<int, int> tmpdp;
      for (it = dp.begin(); it != dp.end(); it++) {
        k = it->first;
        v = it->second;
        for (j = 0; j < M; j++) {
          c = C[j];
          if (k + c > 255)
            y = 255;
          else if (k + c < 0)
            y = 0;
          else
            y = k + c;
          sub_sq = square(x[i] - y);
          if (not(tmpdp.count(y) != 0 and v + sub_sq >= tmpdp[y]))
            tmpdp[y] = v + sub_sq;
        }
      }
      dp = tmpdp;
    }
    it = dp.begin();
    min = it->second;
    it++;
    for (; it != dp.end(); it++) min = min < it->second ? min : it->second;
    cout << min << endl;
  }
  return 0;
}
