#include <bits/stdc++.h>
using namespace std;
int foo(int huga) {
  if (huga <= 0)
    return 0;
  else if (huga >= 255)
    return 255;
  return huga;
}
int main() {
  int N, M, C[16], x, y = 128, i, j, k, l, mi;
  while (1) {
    int temp[20004][256] = {}, temp2[2][256] = {};
    temp2[0][128] = 1;
    cin >> N >> M;
    if (N == 0 && M == 0) break;
    for (i = 0; i < M; i++) {
      cin >> C[i];
      temp2[1][foo(C[i] + 128)] = 1;
    }
    for (i = 1; i <= N; i++) {
      cin >> x;
      for (j = 0; j < 256; j++) {
        if (!temp2[1][j]) continue;
        mi = 2147483647;
        for (k = 0; k < M; k++) {
          if (foo(j - C[k]) == 0) {
            for (l = 255 - C[k]; l <= 255; l++) {
              if (temp2[0][l]) {
                mi = min(temp[i - 1][l], mi);
              }
            }
          } else if (foo(j - C[k]) == 255) {
            for (l = 0; l <= -C[k]; l++) {
              if (temp2[0][l]) {
                mi = min(temp[i - 1][l], mi);
              }
            }
          } else {
            if (!temp2[0][foo(j - C[k])]) {
              continue;
            }
            mi = min(temp[i - 1][foo(j - C[k])], mi);
          }
        }
        temp[i][j] = mi + (x - j) * (x - j);
      }
      for (j = 0; j < 256; j++) {
        temp2[0][j] = temp2[1][j];
        temp2[1][j] = 0;
      }
      for (j = 0; j < 256; j++) {
        if (!temp2[0][j]) continue;
        for (k = 0; k < M; k++) {
          temp2[1][foo(C[k] + j)] = 1;
        }
      }
    }
    mi = 2147483647;
    for (i = 0; i < 256; i++) {
      if (!temp2[0][i]) continue;
      mi = min(mi, temp[N][i]);
    }
    cout << mi << endl;
  }
  return 0;
}
