#include <bits/stdc++.h>
using namespace std;
using LL = long long;
using P = pair<int, int>;
using Tapris = tuple<int, int, int>;
const int INF = (int)1e9;
const LL INFL = (LL)1e15;
const int MOD = 1e9 + 7;
int dy[] = {0, 0, 1, -1, 0};
int dx[] = {1, -1, 0, 0, 0};
int n, m;
vector<int> c, x;
LL dp[20005][550];
signed main() {
  while (cin >> n >> m, n != 0 || m != 0) {
    c.resize(m);
    x.resize(n);
    for (LL i = 0; i < m; ++i) cin >> c[i];
    for (LL i = 0; i < n; ++i) cin >> x[i];
    for (LL i = 0; i < 20005; ++i)
      for (LL j = 0; j < 550; ++j) dp[i][j] = INF;
    dp[0][128 + 255] = 0;
    for (LL i = 1; i < n + 1; ++i) {
      for (int j = -255; j <= 255; j++) {
        bool flag = false;
        if (j == -255) {
          for (LL k = 0; k < m; ++k) {
            for (int z = -255 - c[k]; z >= -255; z--) {
              if (dp[i - 1][z + 255] != INF) {
                dp[i][-255 + 255] = min(
                    dp[i][-255 + 255],
                    dp[i - 1][z + 255] + (LL)pow(abs(x[i - 1] - (-255)), 2));
              }
            }
          }
        } else if (j == 255) {
          for (LL k = 0; k < m; ++k) {
            for (int z = 255 - c[k]; z <= 255; z++) {
              if (dp[i - 1][z + 255] != INF) {
                dp[i][255 + 255] =
                    min(dp[i][255 + 255],
                        dp[i - 1][z + 255] + (LL)pow(abs(x[i - 1] - (255)), 2));
              }
            }
          }
        } else {
          for (LL k = 0; k < m; ++k) flag |= dp[i - 1][j - c[k] + 255] != INF;
          if (flag) {
            for (LL k = 0; k < m; ++k) {
              if (j - c[k] >= 0 && j - c[k] <= 255 &&
                  dp[i - 1][j - c[k] + 255] != INF) {
                dp[i][j + 255] =
                    min(dp[i][j + 255], dp[i - 1][j - c[k] + 255] +
                                            (LL)pow(abs(x[i - 1] - j), 2));
              }
            }
          }
        }
      }
    }
    LL ans = INF;
    for (int i = -255; i <= 255; i++) ans = min(ans, dp[n][i + 255]);
    cout << ans << endl;
  }
}
