#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;
unsigned int memo[256][20000];
unsigned int solve(int now, int prev) {
  if (now == n) return 0;
  if (memo[now][prev] != 0xFFFFFFFF) return memo[now][prev];
  unsigned int ret = 0xFFFFFFFF;
  for (int i = 0; i < (int)(m); i++) {
    int t = min(255, max(0, prev + c[i]));
    unsigned int g = (unsigned int)(t - x[now]) * (t - x[now]);
    ret = min(ret, g + solve(now + 1, t));
  }
  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, 0xFF, sizeof(memo));
    printf("%u\n", solve(0, 128));
  }
  return 0;
}
