#include <bits/stdc++.h>
using namespace std;
using ll = long long;

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<int> cs(m), x(n);
    for (int i = 0; i < m; i++) cin >> cs[i];
    for (int i = 0; i < n; i++) cin >> x[i];
    vector<vector<ll>> dp(n + 1, vector<ll>(256, 1e15));
    dp[0][128] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 256; j++) {
        for (auto c : cs) {
          int v = min(255, max(0, j + c));
          dp[i + 1][v] = min(dp[i + 1][v], dp[i][j] + (x[i] - v) * (x[i] - v));
        }
      }
    }
    ll ans = *min_element(begin(dp[n]), end(dp[n]));
    cout << ans << endl;
  }
}

