#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M, C[17], x[20002], y, a, b;
  pair<long long, int> DP[2][256];
  long long mi;
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0) break;
    for (int i = 0; i < M; i++) cin >> C[i];
    for (int i = 1; i < N + 1; i++) cin >> x[i];
    for (int i = 0; i < 256; i++) {
      DP[0][i].first = 0;
      DP[0][i].second = 128;
    }
    for (int i = 1; i < N + 1; i++) {
      for (int j = 0; j < M; j++) {
        for (int n = 0; n < M; n++) {
          mi = 100000001;
          for (int m = 0; m < M; m++) {
            b = C[j] + DP[(i - 1) % 2][n * M + m].second;
            if (b < 0) b = 0;
            if (b > 255) b = 255;
            a = x[i] - b;
            a = a * a;
            a += DP[(i - 1) % 2][n * M + m].first;
            if (mi > a) {
              mi = a;
              y = b;
            }
          }
          DP[i % 2][j * M + n].first = mi;
          DP[i % 2][j * M + n].second = y;
        }
      }
    }
    for (int i = 0; i < M * M; i++) {
      if (N % 2 == 0)
        mi = min(mi, DP[0][i].first);
      else
        mi = min(mi, DP[1][i].first);
    }
    cout << mi << endl;
  }
  return 0;
}
