#include <bits/stdc++.h>
using namespace std;
int a[16], b[20000], c[20001][256], n, m;
int fi(int p, int q) {
  int i;
  if (c[p][q] >= 0) return c[p][q];
  int mn = 1 << 30;
  for (i = 0; i < m; ++i) {
    int r = min(max(0, q + a[i]), 255), s = (b[p] - r) * (b[p] - r);
    if (0) {
    } else if (mn > fi(p + 1, r) + s) {
      mn = c[p + 1][r] + s;
    } else if (mn < c[p + 1][r] + s) {
      break;
    }
  }
  return c[p][q] = mn;
}
int main() {
  int i;
  while (cin >> n >> m, n || m) {
    for (i = 0; i < m; ++i) cin >> a[i];
    sort(a, a + m);
    for (i = 0; i < n; ++i) cin >> b[i];
    memset(c, -1, n * 256 * sizeof(int));
    memset(c[n], 0, 256 * sizeof(int));
    cout << fi(0, 128) << endl;
  }
  return 0;
}
