#include <bits/stdc++.h>
using namespace std;
struct node {
  long long cost;
  int ind;
  long long val;
  node(long long c, int i, int v) {
    cost = c;
    ind = i;
    val = v;
  }
};
bool checked[20001][256];
bool operator<(const node& lhs, const node& rhs) {
  if (lhs.cost == rhs.cost) {
    return lhs.ind < rhs.ind;
  }
  return lhs.cost > rhs.cost;
}
int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    memset(checked, 0, sizeof(checked));
    vector<long long> c(m), x(n);
    for (int i = 0; i < m; i++) cin >> c[i];
    sort(c.begin(), c.end());
    for (int i = 0; i < n; i++) cin >> x[i];
    priority_queue<node> que;
    que.push(node(0, 0, 128));
    while (!que.empty()) {
      node nd = que.top();
      que.pop();
      if (nd.ind == n) {
        cout << nd.cost << endl;
        break;
      }
      if (checked[nd.ind][nd.val]) {
        continue;
      }
      checked[nd.ind][nd.val] = true;
      for (int i = 0; i < m; i++) {
        long long nval = min(max(nd.val + c[i], 0LL), 255LL);
        if (!checked[nd.ind + 1][nval]) {
          que.push(node(nd.cost + (x[nd.ind] - nval) * (x[nd.ind] - nval),
                        nd.ind + 1, nval));
        }
      }
    }
  }
  return 0;
}
