#include <bits/stdc++.h>
int zMIN(int a, int b) {
  if (b == 0) return a;
  if (a == 0 || a > b) return b;
  return a;
}
int main() {
  int n, m, i, j, k, min, s[20], a, nj;
  while (scanf("%d %d", &n, &m), n || m) {
    int d[2][256] = {0};
    for (i = 0; i < m; i++) scanf("%d", &s[i]);
    d[0][128] = 1;
    for (i = 0; i < n; i++) {
      scanf("%d", &a);
      for (j = 0; j < 256; j++) d[1 - i % 2][j] = 0;
      for (j = 0; j < 256; j++) {
        if (d[i % 2][j] == 0) continue;
        for (k = 0; k < m; k++) {
          nj = j + s[k];
          if (nj < 0) nj = 0;
          if (nj > 255) nj = 255;
          d[1 - i % 2][nj] =
              zMIN(d[1 - i % 2][nj], d[i % 2][j] + (a - nj) * (a - nj));
        }
      }
    }
    min = 21000000;
    for (j = 0; j < 256; j++) min = zMIN(min, d[i % 2][j]);
    printf("%d\n", min - 1);
  }
  return 0;
}
