#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using VL = vector<LL>;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T>>;
const LL LINF = 1e18;
LL dist[20004][260];
inline int cha(int s) {
  if (s >= 255) return 255;
  if (s <= 0) return 0;
  return s;
}
inline LL cost(int y, int x) { return (y - x) * (y - x); }
void init() { fill(*dist, *dist + 20004 * 260, LINF); }
void solve() {
  int N, M;
  while (cin >> N >> M, N) {
    init();
    V<int> c(M);
    for (int i = 0; i < M; i++) {
      cin >> c[i];
    }
    V<int> x(N);
    for (int i = 0; i < N; i++) {
      cin >> x[i];
    }
    dist[0][128] = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < M; j++) {
        for (int p = 0; p < 260; p++) {
          dist[i + 1][cha(p + c[j])] =
              min(dist[i + 1][cha(p + c[j])],
                  dist[i][cha(p)] + cost(x[i], cha(p + c[j])));
        }
      }
    }
    LL ans = LINF;
    for (int i = 0; i < 260; i++) {
      ans = min(ans, dist[N][i]);
    }
    cout << ans << endl;
  }
}
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  solve();
  return 0;
}
