#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (!n && !m) break;
    vector<int> c(m), x(n);
    for (int i = 0; i < m; ++i) {
      cin >> c[i];
    }
    for (int i = 0; i < n; ++i) {
      cin >> x[i];
    }
    vector<vector<long long>> dp(n + 2);
    for (int i = 0; i < n + 2; ++i) {
      dp[i].resize(256);
      for (int j = 0; j < 256; ++j) {
        dp[i][j] = LONG_MAX;
      }
    }
    dp[0][128] = 1;
    for (int i = 1; i <= n; ++i) {
      for (int j = 0; j < m; ++j) {
        for (int k = 0; k < 256; ++k) {
          if (dp[i - 1][k] == LONG_MAX) continue;
          int next = k + c[j];
          if (next > 256) next = 255;
          if (next < 0) next = 0;
          dp[i][next] =
              min(dp[i][next], dp[i - 1][k] + (int)pow(x[i - 1] - next, 2));
        }
      }
    }
    long long ans = LONG_MAX;
    for (int i = 0; i < 256; ++i) {
      ans = min(ans, (dp[n][i] != 0 ? dp[n][i] : LONG_MAX));
    }
    cout << ans - 1 << endl;
  }
  return 0;
}
