#include <bits/stdc++.h>
using namespace std;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
struct UnionFind {
  vector<int> v;
  UnionFind(int n) : v(n) {
    for (int i = 0; i < n; i++) v[i] = i;
  }
  int find(int x) { return v[x] == x ? x : v[x] = find(v[x]); }
  void unite(int x, int y) { v[find(x)] = find(y); }
};
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<int> cb(m);
    for (int i = 0; i < (int)(m); ++i) cin >> cb[i];
    vector<int> os(n);
    for (int i = 0; i < (int)(n); ++i) cin >> os[i];
    vector<int> memo(256, 1000000000);
    memo[128] = 0;
    for (int i = 0; i < (int)(n); ++i) {
      vector<int> copy(256, 1000000000);
      for (int j = 0; j < (int)(256); ++j) {
        if (memo[j] != 1000000000) {
          for (int k = 0; k < (int)(m); ++k) {
            copy[min(255, max(0, j + cb[k]))] =
                memo[j] + (os[i] - min(255, max(0, j + cb[k]))) *
                              (os[i] - min(255, max(0, j + cb[k])));
          }
        }
      }
      memo = copy;
    }
    int ans = 1e9;
    for (int i = 0; i < (int)(256); ++i) ans = min(ans, memo[i]);
    cout << ans << endl;
  }
}
