#include <bits/stdc++.h>
using namespace std;
int main() {
  for (int n, m; cin >> n >> m, n | m;) {
    int cs[m], xs[n];
    for (int& c : cs) cin >> c;
    for (int& x : xs) cin >> x;
    queue<tuple<int, int, int>> q;
    q.emplace(0, 128, 0);
    int dp[n + 1][256];
    for (int i = 0; i <= n; i++)
      fill(dp[i], dp[i] + 256, numeric_limits<int>::max());
    while (!q.empty()) {
      auto cur = q.front();
      q.pop();
      int ci = get<0>(cur), cval = get<1>(cur), csum = get<2>(cur);
      if (dp[ci][cval] <= csum) continue;
      dp[ci][cval] = csum;
      if (ci == n) continue;
      for (int c : cs) {
        int nval = max(min(cval + c, 255), 0);
        int nsum = csum + (nval - xs[ci]) * (nval - xs[ci]);
        q.emplace(ci + 1, nval, nsum);
      }
    }
    cout << *min_element(dp[n], dp[n] + 256) << endl;
  }
  return 0;
}
