#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M, C[17], x[20002], y;
  pair<long long, int> P[20002][17];
  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; i++) cin >> x[i];
    for (int i = 0; i < M; i++) {
      P[0][i].first = 0;
      P[0][i].second = 128;
    }
    for (int i = 1; i <= N; i++) {
      for (int j = 0; j < M; j++) {
        P[i][j].first = 100000000;
        P[i][j].second = 0;
      }
    }
    for (int i = 1; i <= N; i++) {
      for (int j = 0; j < M; j++) {
        for (int k = 0; k < M; k++) {
          y = (P[i - 1][k].second + C[j]);
          if (y < 0) y = 0;
          if (y > 255) y = 255;
          if (P[i][j].first > P[i - 1][k].first + (x[i] - y) * (x[i] - y)) {
            P[i][j].first = P[i - 1][k].first + (x[i] - y) * (x[i] - y);
            P[i][j].second = y;
          }
        }
      }
    }
    mi = 100000000;
    for (int i = 0; i < M; i++) mi = min(mi, P[N][i].first);
    cout << mi << endl;
  }
  return 0;
}
