#include <bits/stdc++.h>
const long long inf = 900900900100100100;
const int MOD = (int)1e9 + 7;
const double EPS = 1e-9;
const int MID = 255;
using namespace std;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n, m;
  while (cin >> n >> m, n) {
    vector<int> c(m);
    for (int i = 0; i < (int)(m); i++) cin >> c[i];
    vector<int> x(n);
    for (int i = 0; i < (int)(n); i++) cin >> x[i];
    vector<vector<long long> > dp(20100, vector<long long>(MID * 3, inf));
    dp[0][128 + MID] = 0;
    for (int i = 0; i < (int)(n); i++) {
      for (int j = (int)(-MID); j < (int)(MID + 1); j++) {
        for (int k = 0; k < (int)(m); k++) {
          int nj;
          if (j + c[k] > 255)
            nj = 255;
          else if (j + c[k] < -255)
            nj = -255;
          else
            nj = j + c[k];
          dp[i + 1][nj + MID] = min<long long>(
              dp[i + 1][nj + MID], dp[i][j + MID] + (nj - x[i]) * (nj - x[i]));
        }
      }
    }
    long long ans = inf;
    for (int i = (int)(-MID); i < (int)(MID + 1); i++)
      ans = min<long long>(dp[n][i + MID], ans);
    cout << ans << endl;
  }
  return 0;
}
