#include <bits/stdc++.h>
using namespace std;
long long dp[256][2];
int d(int v) { return v * v; }
int ran(int v) {
  if (v < 0) return 0;
  return min(255, v);
}
int main(void) {
  for (;;) {
    int n, m;
    cin >> n >> m;
    if (!n && !m) return 0;
    vector<int> alt;
    alt.resize(m);
    for (int i = (0); i < (m); ++i) cin >> alt[i];
    for (int i = (0); i < (256); ++i) dp[i][0] = 2147483647;
    dp[128][0] = 0;
    for (int i = (0); i < (n); ++i) {
      int x;
      cin >> x;
      for (int j = (0); j < (256); ++j) dp[j][(i + 1) % 2] = 2147483647;
      for (int j = (0); j < (256); ++j) {
        for (int k = (0); k < (m); ++k)
          dp[ran(j + alt[k])][(i + 1) % 2] =
              min(dp[ran(j + alt[k])][(i + 1) % 2],
                  dp[j][i] + d(ran(j + alt[k]) - x));
      }
    }
    long long answer = 2147483647;
    for (int i = (0); i < (256); ++i) answer = min(answer, dp[i][n % 2]);
    cout << answer << endl;
  }
}
