#include <bits/stdc++.h>
using namespace std;
const long long inf = 1LL << 60;
long long memo[20000][256];
int main() {
  for (int n, m; scanf("%d%d", &n, &m), n;) {
    int c[16];
    static int x[20001];
    for (int i = 0; i < m; i++) scanf("%d", c + i);
    for (int i = 0; i < n; i++) scanf("%d", x + i);
    for (int i = 0; i <= n; i++)
      for (int j = 0; j < 256; j++) memo[i][j] = inf;
    memo[0][128] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < 256; j++) {
        if (memo[i - 1][j] == inf) continue;
        for (int k = 0; k < m; k++) {
          int next = min(255, max(0, j + c[k]));
          memo[i][next] =
              min(memo[i][next],
                  memo[i - 1][j] + (next - x[i - 1]) * (next - x[i - 1]));
        }
      }
    }
    long long emin = inf;
    for (int j = 0; j < 256; j++) emin = min(emin, memo[n][j]);
    printf("%lld\n", emin);
  }
  return 0;
}
