#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.sync_with_stdio(false);
  int n, m;
  while (cin >> n >> m, n | m) {
    vector<int> c(m), x(n);
    for (int i = (int)(0); i < (int)(m); i++) cin >> c[i];
    for (int i = (int)(0); i < (int)(n); i++) cin >> x[i];
    long long dp[n + 1][256];
    for (int i = (int)(0); i < (int)(n + 1); i++)
      for (int j = (int)(0); j < (int)(256); j++) dp[i][j] = INT_MAX / 3;
    dp[0][128] = 0;
    for (int i = (int)(0); i < (int)(n); i++) {
      for (int j = (int)(0); j < (int)(256); j++) {
        if (dp[i][j] < INT_MAX / 3) {
          for (int k = (int)(0); k < (int)(m); k++) {
            int tmp = j + c[k];
            if (tmp > 255)
              tmp = 255;
            else if (tmp < 0)
              tmp = 0;
            dp[i + 1][tmp] =
                min(dp[i + 1][tmp], dp[i][j] + (x[i] - tmp) * (x[i] - tmp));
          }
        }
      }
    }
    long long ans = INT_MAX / 3;
    for (int i = (int)(0); i < (int)(256); i++) ans = min(ans, dp[n][i]);
    cout << ans << endl;
  }
  return 0;
}
