#include <bits/stdc++.h>
using namespace std;
const int INF = 987654321;
int main(void) {
  for (;;) {
    int n, m;
    cin >> n >> m;
    if (!n) break;
    vector<int> c;
    for (int i = 0; i < m; ++i) {
      int cc;
      cin >> cc;
      c.push_back(cc);
    }
    array<int, 256> dp;
    fill(dp.begin(), dp.end(), INF);
    dp[128] = 0;
    for (int i = 1; i <= n; ++i) {
      array<int, 256> ndp;
      fill(ndp.begin(), ndp.end(), INF);
      int x;
      cin >> x;
      for (int val = 0; val < 256; ++val) {
        if (val == 0) {
          int gap = -1;
          for (int ck : c) gap = max(gap, -ck);
          for (int old = 0; old <= gap; ++old) {
            ndp[val] = min(ndp[val], dp[old] + (x - val) * (x - val));
          }
        } else if (val == 255) {
          int gap = -1;
          for (int ck : c) gap = max(gap, ck);
          for (int old = 255 - gap; old < 256; ++old) {
            ndp[val] = min(ndp[val], dp[old] + (x - val) * (x - val));
          }
        }
        for (int ck : c) {
          int old = val - ck;
          if (old < 0) old = 0;
          if (old >= 256) old = 255;
          ndp[val] = min(ndp[val], dp[old] + (x - val) * (x - val));
        }
      }
      dp = ndp;
    }
    int ans = INF;
    for (int can : dp) {
      ans = min(ans, can);
    }
    cout << ans << endl;
  }
  return 0;
}
