#include <bits/stdc++.h>
using namespace std;
const int e = 1e9 + 7;
int cut(int n) {
  if (n > 255) {
    n = 255;
  }
  if (n < 0) {
    n = 0;
  }
  return n;
}
int dp[25000][256];
int main() {
  while (1) {
    int n, m;
    fill(&dp[0][0], &dp[24999][256], e);
    vector<int> in, c;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < m; i++) {
      int d;
      cin >> d;
      c.push_back(d);
    }
    for (int i = 0; i < n; i++) {
      int d;
      cin >> d;
      in.push_back(d);
    }
    dp[0][128] = 0;
    for (int i = 0; i < n; i++) {
      for (int t = 0; t < 256; t++) {
        for (int h = 0; h < c.size(); h++) {
          if (dp[i][t] != e) {
            dp[i + 1][cut(t + c[h])] =
                min(dp[i + 1][cut(t + c[h])],
                    dp[i][t] + (int)pow((in[i] - cut(t + c[h])), 2));
          }
        }
      }
    }
    int ans = e;
    for (int i = 0; i < 256; i++) {
      ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
  return 0;
}
