#include <bits/stdc++.h>
using namespace std;
int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (!n) break;
    vector<int> codes(m);
    for (int i = 0; i < (int)(m); i++) cin >> codes[i];
    vector<int> data(n);
    for (int i = 0; i < (int)(n); i++) cin >> data[i];
    vector<int64_t> dp(256, 0);
    for (int i = 0; i < (int)(n); i++) {
      vector<int64_t> tmp(256, INT64_C(1000000000000000));
      for (int j = 0; j < (int)(256); j++) {
        for (int k = 0; k < (int)(m); k++) {
          int newv = min(255, max(0, j + codes[k]));
          int diff = data[i] - newv;
          int sqd = diff * diff;
          tmp[newv] = min(tmp[newv], dp[j] + sqd);
        }
      }
      dp.swap(tmp);
    }
    cout << *min_element(begin(dp), end(dp)) << endl;
  }
  return 0;
}
