#include <bits/stdc++.h>
using namespace std;
void pkuassert(bool t) { t = 1 / t; };
int dx[] = {0, 1, 0, -1, 1, 1, -1, -1};
int dy[] = {1, 0, -1, 0, -1, 1, 1, -1};
int n, m;
int c[16];
bool vis[256][20001];
void solve() {
  for (int i = 0; i < (int)(m); ++i) cin >> c[i];
  int x[n];
  for (int i = 0; i < (int)(n); ++i) cin >> x[i];
  priority_queue<pair<int, pair<int, int> > > q;
  q.push(make_pair(0, make_pair(0, 128)));
  memset(vis, 0, sizeof(vis));
  while (!q.empty()) {
    int cc = -q.top().first;
    int ne = q.top().second.first;
    int cv = q.top().second.second;
    q.pop();
    if (ne == n) {
      cout << cc << endl;
      return;
    }
    if (vis[cv][ne]) continue;
    vis[cv][ne] = true;
    for (int j = 0; j < (int)(m); ++j) {
      long long nv = cv + c[j];
      nv = max(nv, 0LL);
      nv = min(nv, 255LL);
      if (vis[ne + 1][nv]) continue;
      q.push(
          make_pair(-cc - (nv - x[ne]) * (nv - x[ne]), make_pair(ne + 1, nv)));
    }
  }
}
int main(int argc, char *argv[]) {
  while (cin >> n >> m && n) solve();
  return 0;
}
