#include <bits/stdc++.h>
using namespace std;
using ll = long long;
ll dp[20][256];
ll dfs(int i, int y, vector<int>& c, vector<int>& x) {
  if (i >= x.size()) return 0;
  if (y < 0) y = 0;
  if (y > 255) y = 255;
  if (dp[i][y] != -1) return dp[i][y];
  ll res = 999999999999;
  for (int j = 0; j < c.size(); ++j) {
    res = min(res, dfs(i + 1, y + c[j], c, x) + (y - x[i]) * (y - x[i]));
  }
  return dp[i][y] = res;
}
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    for (int i = 0; i < 20; ++i) {
      for (int j = 0; j < 256; ++j) dp[i][j] = -1;
    }
    vector<int> c(m), x(n + 1);
    for (int i = 0; i < m; ++i) {
      cin >> c[i];
    }
    x[0] = 128;
    for (int i = 1; i < n + 1; ++i) {
      cin >> x[i];
    }
    cout << dfs(0, 128, c, x) << endl;
  }
  return 0;
}
