#include <bits/stdc++.h>
using namespace std;
int n, m;
int x[30000];
int c[100];
int memo[30000][300];
inline long long square(long long x) { return x * x; }
long long calc(int depth, int color) {
  assert(0 <= color && color <= 255);
  if (memo[depth][color] != -1) {
    return memo[depth][color];
  }
  if (depth == n) {
    return memo[depth][color] = 0;
  }
  long long ret = 1LL << 60;
  for (int(i) = 0; i < (int)(m); ++i) {
    int ncolor = color + c[i];
    ncolor = max(0, min(255, ncolor));
    int cost = square(abs(x[depth + 1] - ncolor));
    ret = min(ret, calc(depth + 1, ncolor) + cost);
  }
  return memo[depth][color] = ret;
}
int main() {
  while (scanf("%d %d", &n, &m), n | m) {
    memset(memo, -1, sizeof(memo));
    for (int(i) = 0; i < (int)(m); ++i) {
      scanf("%d", &c[i]);
    }
    for (int(i) = 0; i < (int)(n); ++i) {
      scanf("%d", &x[i + 1]);
    }
    printf("%lld\n", calc(0, 128));
  }
}
