#include <bits/stdc++.h>
using namespace std;
template <class T>
void pv(T a, T b) {
  for (T i = a; i != b; ++i) cout << *i << " ";
  cout << endl;
}
template <class T>
void chmin(T &t, T f) {
  if (t > f) t = f;
}
template <class T>
void chmax(T &t, T f) {
  if (t < f) t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}
long long dp[20010][300];
int c[20];
int x[20010];
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int i, j, k;
    for (i = 0; i < m; i++) {
      cin >> c[i];
    }
    for (i = 0; i < n; i++) {
      cin >> x[i];
    }
    memset(dp, -1, sizeof(dp));
    dp[0][128] = 0;
    int next;
    for (i = 0; i < n; i++) {
      for (j = 0; j < 256; j++) {
        if (dp[i][j] == -1) continue;
        for (k = 0; k < m; k++) {
          next = j + c[k];
          if (next < 0) next = 0;
          if (next >= 256) next = 255;
          if (dp[i + 1][next] == -1 ||
              dp[i + 1][next] > dp[i][j] + (next - x[i]) * (next - x[i]))
            dp[i + 1][next] = dp[i][j] + (next - x[i]) * (next - x[i]);
        }
      }
    }
    long long mn = -1;
    for (j = 0; j < 256; j++) {
      if (dp[n][j] != -1) {
        if (mn == -1 || mn > dp[n][j]) mn = dp[n][j];
      }
    }
    cout << mn << endl;
  }
  return 0;
}
