#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
int main() {
  int N, M, C[16], x[20000];
  int dp[2][256];
  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]);
    }
    fill_n(dp[1], 256, INF);
    dp[0][128] = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < 256; j++) {
        if (dp[i & 1][j] == INF) continue;
        for (int k = 0; k < M; k++) {
          int value = min(255, max(0, j + C[k]));
          dp[(i + 1) & 1][value] =
              min(dp[(i + 1) & 1][value],
                  dp[i & 1][j] + (x[i] - value) * (x[i] - value));
        }
      }
      for (int j = 0; j < 256; j++) dp[i & 1][j] = INF;
    }
    int ret = INF;
    for (int i = 0; i < 256; i++) {
      ret = min(ret, dp[N & 1][i]);
    }
    printf("%d\n", ret);
  }
}
