#include <bits/stdc++.h>
using namespace std;
int c[16], x[16];
int dp[20001][256];
int d(int a) {
  if (a < 0) return 0;
  if (a > 255) return 255;
  return a;
}
int diff(int i, int a) { return (x[i] - a) * (x[i] - a); }
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    for (int i = 0; i < (m); i++) scanf("%d", &c[i]);
    for (int i = 0; i < (n); i++) scanf("%d", &x[i]);
    memset(dp, 0x3f, sizeof(dp));
    dp[0][128] = 0;
    for (int i = 0; i < (n); i++)
      for (int j = 0; j < (256); j++)
        for (int k = 0; k < (m); k++) {
          int a = d(j + c[k]);
          dp[i + 1][a] = min(dp[i + 1][a], dp[i][j] + diff(i, a));
        }
    int Min = INT_MAX;
    for (int j = 0; j < (256); j++) Min = min(Min, dp[n][j]);
    printf("%d\n", Min);
  }
}
