#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) return 0;
    vector<int> c(m);
    for (int i = 0; i < m; i++) {
      cin >> c[i];
    }
    vector<int> x(n);
    for (int i = 0; i < n; i++) {
      cin >> x[i];
    }
    long long inf = 1LL << 60;
    vector<vector<long long>> dp(n + 1, vector<long long>(256, inf));
    dp[0][128] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= 255; j++) {
        if (dp[i][j] == inf) continue;
        for (int k = 0; k < m; k++) {
          int s = min(255, max(0, j + c[k]));
          dp[i + 1][s] = min(dp[i + 1][s], dp[i][j] + 1LL * (s - x[i]) * (s - x[i]));
        }
      }
    }
    long long ans = inf;
    for (int i = 0; i <= 255; i++) {
      ans = min(ans, dp[n][i]);
    }
    cout << ans << '\n';
  }
  return 0;
}
