#include <bits/stdc++.h>
using namespace std;
int N, M, C[16], X[20001];
unsigned long long T[256][20001];
unsigned long long compute() {
  for (int i = 0; i < 256; i++)
    for (int j = 0; j < N + 1; j++) T[i][j] = (1 << 30);
  T[128][0] = 0;
  for (int j = 0; j < N; j++) {
    for (int i = 0; i < 256; i++) {
      if (T[i][j] == (1 << 30)) continue;
      for (int k = 0; k < M; k++) {
        int p = i + C[k];
        if (p > 255) p = 255;
        if (p < 0) p = 0;
        T[p][j + 1] =
            min(T[p][j + 1], T[i][j] + (X[j + 1] - p) * (X[j + 1] - p));
      }
    }
  }
  unsigned long long minv = (1 << 30);
  for (int i = 0; i < 256; i++) minv = min(minv, T[i][N]);
  return minv;
}
int main() {
  while (cin >> N >> M && N) {
    for (int i = 0; i < M; i++) cin >> C[i];
    for (int i = 0; i < N; i++) cin >> X[i + 1];
    cout << compute() << endl;
  }
}
