#include <bits/stdc++.h>
using namespace std;
const int inf = 1e8;
void hoge(int N, int M) {
  int c[M];
  for (int i = 0; i < M; i++) cin >> c[i];
  int x[N];
  for (int i = 0; i < N; i++) cin >> x[i];
  int memo[N + 1][256];
  for (int i = 0; i < N + 1; i++) {
    for (int j = 0; j < 256; j++) {
      memo[i][j] = inf;
    }
  }
  memo[0][128] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 256; j++) {
      if (memo[i][j] == inf) continue;
      for (int k = 0; k < M; k++) {
        int p = max(0, min(255, j + c[k]));
        memo[i + 1][p] =
            min(memo[i][j] + (p - x[i]) * (p - x[i]), memo[i + 1][p]);
      }
    }
  }
  int mi = 1e8;
  for (int j = 0; j < 256; j++) mi = min(mi, memo[N][j]);
  cout << mi << endl;
}
int main() {
  while (1) {
    int N, M;
    cin >> N >> M;
    if (!(N || M)) break;
    hoge(N, M);
  }
  return 0;
}
