#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  int dp[256][256];
  while (cin >> n >> m, n) {
    for (int i = 0; i < 256; i++)
      for (int j = 0; j < 256; j++) dp[i][j] = 1 << 30;
    dp[0][128] = 0;
    int c[m], x[n];
    for (int i = 0; i < m; i++) cin >> c[i];
    for (int i = 0; i < n; i++) cin >> x[i];
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 256; j++) {
        for (int k = 0; k < m; k++) {
          if (j + c[k] < 0) {
            dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + x[i] * x[i]);
          } else if (j + c[k] > 255) {
            dp[i + 1][255] =
                min(dp[i + 1][255], dp[i][j] + (x[i] - 255) * (x[i] - 255));
          } else {
            dp[i + 1][j + c[k]] =
                min(dp[i + 1][j + c[k]],
                    dp[i][j] + (x[i] - j - c[k]) * (x[i] - j - c[k]));
          }
        }
      }
    }
    int res = 1 << 30;
    for (int i = 0; i < 256; i++) res = min(res, dp[n][i]);
    cout << res << endl;
  }
  return 0;
}
