#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
const double EPS = 1e-9;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
int dp[20010][300];
int C[20];
int X[20010];
int n, m;
int main() {
  while (cin >> n >> m, n || m) {
    memset(dp, 0, sizeof(dp));
    memset(C, 0, sizeof(C));
    memset(X, 0, sizeof(X));
    for (int i = 0; i < m; i++) {
      cin >> C[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> X[i];
    }
    memset(dp, INF, sizeof(dp));
    dp[0][128] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        for (int k = 0; k <= 255; k++) {
          if (dp[i][k] == INF) continue;
          int val = k + C[j];
          if (val > 255) val = 255;
          if (val < 0) val = 0;
          dp[i + 1][val] =
              min(dp[i + 1][val], dp[i][k] + (X[i] - val) * (X[i] - val));
        }
      }
    }
    int ans = INF;
    for (int i = 0; i <= 255; i++) {
      ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
  return 0;
}
