#include <bits/stdc++.h>
using namespace std;
int N, M, C[16], x[20000];
int main() {
  while (cin >> N >> M, (N | M)) {
    for (int i = 0; i < M; i++) {
      cin >> C[i];
    }
    for (int i = 0; i < N; i++) {
      cin >> x[i];
    }
    long long min = LLONG_MAX;
    for (int i = 0; i <= 255; i++) {
      long long value = 0, ii = i;
      for (int j = 0; j < N; j++) {
        long long min_y = LLONG_MAX, next;
        for (int k = 0; k < M; k++) {
          int cmp = abs(x[j] - (ii + C[k]));
          if (cmp < 0) {
            cmp = 0;
          } else if (cmp > 255) {
            cmp = 255;
          }
          if (cmp < min_y) {
            min_y = cmp;
            next = ii + C[k];
          }
        }
        value += min_y * min_y;
        ii = next;
      }
      min = std::min(min, value);
    }
    cout << min << endl;
  }
  return 0;
}
