#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const int MOD = 1000000007;
const int INF = 1000000000;
const long long int LINF = 1ll << 50;
template <typename T>
void printv(vector<T> const& s) {
  for (int i = (0); i < (((int)(s).size())); i++) {
    cout << s[i] << " ";
  }
  cout << endl;
}
int main() {
  cin.tie(0);
  cout << setprecision(10);
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) break;
    vector<int> c(m), x(n + 1);
    for (int i = (0); i < (m); i++) {
      cin >> c[i];
    }
    for (int i = (0); i < (n); i++) {
      cin >> x[i + 1];
    }
    vector<vector<long long int>> dp(n + 1, vector<long long int>(256, LINF));
    for (int i = (0); i < (256); i++) {
      dp[0][i] = 0;
    }
    for (int i = (1); i < (n + 1); i++) {
      for (int j = (0); j < (256); j++) {
        for (int k = (0); k < (m); k++) {
          long long int next = min(255, max(0, j + c[k]));
          dp[i][next] =
              min(dp[i][next], dp[i - 1][j] + (x[i] - next) * (x[i] - next));
        }
      }
    }
    long long int ans = LINF;
    for (int i = (0); i < (256); i++) {
      ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
}
