#include <bits/stdc++.h>
using namespace std;
int n, m;
int c[20];
long long x[21000];
long long d[21000][260];
int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0) return 0;
    for (int i = 0; i < m; i++) {
      cin >> c[i];
    }
    for (int i = 1; i <= n; i++) {
      cin >> x[i];
    }
    for (int i = 0; i <= n; i++) {
      for (int j = 0; j <= 256; j++) {
        d[i][j] = 999999999999;
      }
    }
    d[0][128] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= 255; j++) {
        if (d[i][j] != 999999999999) {
          for (int k = 0; k < m; k++) {
            int l = j + c[k];
            if (l < 0)
              d[i + 1][0] = min(d[i + 1][0], x[i + 1] * x[i + 1]);
            else if (l > 255) {
              d[i + 1][255] = min(
                  d[i + 1][256], d[i][j] + (x[i + 1] - 255) * (x[i + 1] - 255));
            } else {
              d[i + 1][l] =
                  min(d[i + 1][l], d[i][j] + (x[i + 1] - l) * (x[i + 1] - l));
            }
          }
        }
      }
    }
    long long ans = 999999999999;
    for (int i = 0; i <= 255; i++) {
      ans = min(ans, d[n][i]);
    }
    cout << ans << endl;
  }
  return 0;
}
