#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 20001;
const int MAX_M = 16;
const int MAX_V = 256;
const long long INF = 1LL << 60;
long long T[MAX_N][MAX_V];
int N, M;
long long C[MAX_M];
long long x[MAX_N];
long long solve() {
  fill(T[0], T[N + 1], INF);
  T[0][128] = 0LL;
  for (int i = 1; i <= N; ++i) {
    for (int j = 0; j <= 255; ++j) {
      for (int k = 0; k < M; ++k) {
        long long value = j + C[k];
        if (value < 0LL) value = 0LL;
        if (value > 255LL) value = 255LL;
        T[i][value] =
            min(T[i][value], T[i - 1][j] + (x[i] - value) * (x[i] - value));
      }
    }
  }
  long long res = INF;
  for (int i = 1; i <= 255; ++i) {
    res = min(res, T[N][i]);
  }
  return res;
}
int main() {
  while (cin >> N >> M && (N || M)) {
    for (int i = 0; i < M; ++i) {
      cin >> C[i];
    }
    for (int i = 1; i <= N; ++i) {
      cin >> x[i];
    }
    cout << solve() << endl;
  }
}
