#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  while (true) {
    uint32_t N, M;
    cin >> N >> M;
    if (N == 0 && M == 0) break;
    vector<int32_t> C(M);
    vector<int32_t> x(N + 1);
    for (uint32_t i = 0; i < M; i++) {
      cin >> C[i];
    }
    for (uint32_t i = 1; i <= N; i++) {
      cin >> x[i];
    }
    vector<vector<int32_t>> dp(N + 1, vector<int32_t>(256, 1 << 25));
    dp[0][128] = 0;
    for (uint32_t i = 0; i < N; i++) {
      for (int32_t j = 0; j < 256; j++) {
        if (dp[i][j] == 1 << 25) continue;
        for (uint32_t k = 0; k < C.size(); k++) {
          int32_t val = min(max(0, j + C[k]), 255);
          int32_t diff = (x[i + 1] - val);
          dp[i + 1][val] = min(dp[i + 1][val], dp[i][j] + diff * diff);
        }
      }
    }
    int32_t minval = 1 << 25;
    for (int32_t i = 0; i < 256; i++) {
      minval = min(minval, dp[N][i]);
    }
    cout << minval << endl;
  }
  return 0;
}
