#include <bits/stdc++.h>
using namespace std;
int min(int p, int q) {
  if (p < q) return p;
  return q;
}
int rss(int p, int q) { return (p - q) * (p - q); }
int limit(int i) {
  if (i > 255) return 255;
  if (i < 0) return 0;
  return i;
}
int main(void) {
  vector<vector<int> > dp;
  vector<int> dpp;
  for (int i = 0; i < 20001; i++) dpp.push_back(1 << 30);
  for (int i = 0; i < 257; i++) dp.push_back(dpp);
  while (1) {
    int N, M;
    vector<int> c, x;
    cin >> N >> M;
    if (N == 0 && M == 0) break;
    for (int i = 0; i < M; i++) {
      int tmp;
      cin >> tmp;
      c.push_back(tmp);
    }
    for (int i = 0; i < N; i++) {
      int tmp;
      cin >> tmp;
      x.push_back(tmp);
    }
    for (int i = 0; i < 256; i++)
      for (int j = 0; j < 10000; j++) dp[i][j] = 1 << 30;
    for (int i = 0; i < 256; i++) dp[i][0] = 0;
    for (int i = 0; i < M; i++) {
      dp[limit(128 + c[i])][1] = rss(limit(128 + c[i]), x[0]);
    }
    for (int i = 1; i < N; i++)
      for (int j = 0; j < M; j++)
        for (int k = 0; k < 256; k++)
          dp[limit(k + c[j])][i + 1] =
              min(dp[k][i] + rss(limit(k + c[j]), x[i]),
                  dp[limit(k + c[j])][i + 1]);
    int ans = 1 << 30;
    for (int i = 0; i < 256; i++)
      if (dp[i][N] < ans) ans = dp[i][N];
    cout << ans << endl;
  }
  return 0;
}
