#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 22;
void db_print(int line[]) {
  for (int i = 100; i < 200; ++i) {
    cout << i << "[" << line[i] << "]" << endl;
  }
  cout << endl;
}
int main() {
  int N, M;
  while (true) {
    cin >> N >> M;
    if ((N | M) == 0) {
      break;
    }
    int book[M];
    for (int i = 0; i < M; ++i) {
      cin >> book[i];
    }
    int x[N];
    for (int i = 0; i < N; ++i) {
      cin >> x[i];
    }
    int table[N + 1][256];
    fill_n((int *)table, (N + 1) * 256, INF);
    table[0][128] = 0;
    for (int i = 0; i < N; ++i) {
      for (int j = 0; j < 256; ++j) {
        for (int b = 0; b < M; ++b) {
          int d = book[b];
          if (0 <= j + d && j + d <= 255) {
            int t = (x[i] - (j + d)) * (x[i] - (j + d));
            table[i + 1][j + d] = min(table[i + 1][j + d], table[i][j] + t);
          }
          if (j + d < 0) {
            int t = (x[i] - 0) * (x[i] - 0);
            table[i + 1][0] = min(table[i + 1][0], table[i][j] + t);
          }
          if (j + d > 255) {
            int t = (x[i] - 255) * (x[i] - 255);
            table[i + 1][255] = min(table[i + 1][255], table[i][j] + t);
          }
        }
      }
    }
    int ans = INF;
    for (int i = 0; i < 256; ++i) {
      ans = min(ans, table[N][i]);
    }
    cout << ans << endl;
  }
  return 0;
}
