#include <bits/stdc++.h>
using namespace std;
const int _dx[] = {0, 1, 0, -1};
const int _dy[] = {-1, 0, 1, 0};
int getInt() {
  int ret = 0, c;
  c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) {
    ret *= 10;
    ret += c - '0';
    c = getchar();
  }
  return ret;
}
int c[16];
int x[20000];
int n, m;
int memo[20000][256];
int solve(int now, int prev, int kabe) {
  if (now == n) return 0;
  if (memo[now][prev] != -1) return memo[now][prev];
  int ret = INT_MAX;
  for (int i = 0; i < (int)(m); i++) {
    int t = min(255, max(0, prev + c[i]));
    int g = (t - x[now]) * (t - x[now]);
    if (kabe - g > 0) {
      int second = solve(now + 1, t, kabe - g);
      if (second == INT_MAX) continue;
      ret = min(ret, g + second);
      kabe = min(kabe, ret);
    }
  }
  return memo[now][prev] = ret;
}
int main() {
  while (scanf("%d%d", &n, &m), n + m) {
    for (int i = 0; i < (int)(m); i++) scanf("%d", c + i);
    for (int i = 0; i < (int)(n); i++) scanf("%d", x + i);
    memset(memo, -1, sizeof(int) * 256 * n);
    printf("%d\n", solve(0, 128, INT_MAX));
  }
  return 0;
}
