#include <bits/stdc++.h>
int N, M, C[20], x[20005];
long long res, dp[20005][20][2] = {0};
int main() {
  for (; scanf("%d%d", &N, &M), N + M;) {
    res = 100000000000000;
    memset(dp, 0x7f, sizeof(dp));
    for (int i = 0; i < M; i++) scanf("%d", &C[i]);
    for (int j = 1; j <= N; j++) scanf("%d", &x[j]);
    for (int i = 0; i < M; i++) {
      dp[0][i][0] = 128;
      dp[0][i][1] = 0;
    }
    for (int i = 1; i <= N; i++) {
      for (int k = 0; k < M; k++)
        for (int j = 0; j < M; j++) {
          long long p = dp[i - 1][k][0] + C[j];
          if (p > 255) p = 255;
          if (p < 0) p = 0;
          int a = x[i] - p;
          int tmp = a * a;
          if (dp[i - 1][k][1] + tmp < dp[i][j][1]) {
            dp[i][j][1] = dp[i - 1][k][1] + tmp;
            dp[i][j][0] = p;
          }
        }
    }
    for (int i = 0; i < M; i++) res = (res > dp[N][i][1] ? dp[N][i][1] : res);
    printf("%lld\n", res);
  }
  return 0;
}
