#include <bits/stdc++.h>
using namespace std;
unsigned int errorSum[2][256];
bool concerning[2][256];
short samples[20001];
int codeBook[16];
int main() {
  int n, m;
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < m; i++) {
      cin >> codeBook[i];
    }
    for (int i = 1; i <= n; i++) {
      cin >> samples[i];
    }
    for (int i = 0; i < 256; i++) {
      errorSum[0][i] = errorSum[1][i] = INT32_MAX;
      concerning[0][i] = concerning[1][i] = false;
    }
    concerning[0][128] = true;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 256; j++) {
        if (concerning[i % 2][j]) {
          concerning[i % 2][j] = false;
          for (int k = 0; k < m; k++) {
            int next = min(max(0, j + codeBook[k]), 255);
            unsigned int error =
                (errorSum[i % 2][j] == INT32_MAX ? 0 : errorSum[i % 2][j]) +
                (next - samples[i + 1]) * (next - samples[i + 1]);
            if (error <= errorSum[(i + 1) % 2][next]) {
              concerning[(i + 1) % 2][next] = true;
              errorSum[(i + 1) % 2][next] = error;
            }
          }
        }
        errorSum[i % 2][j] = INT32_MAX;
      }
    }
    unsigned int minError = INT32_MAX;
    int lastIndex = n % 2;
    for (int i = 0; i < 256; i++) {
      if (concerning[lastIndex][i] && errorSum[lastIndex][i] < minError) {
        minError = errorSum[lastIndex][i];
      }
    }
    cout << minError << endl;
  }
  return 0;
}
