#include <bits/stdc++.h>
using namespace std;
namespace {
template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()) {
  v.assign(a, vector<T>(b, t));
}
template <class F, class T>
void convert(const F &f, T &t) {
  stringstream ss;
  ss << f;
  ss >> t;
}
static const int INF = 1 << 24;
vector<int> dp(256, 0);
void mainmain() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<int> c(m), x(n);
    for (int(i) = (0); (i) < ((m)); ++(i)) cin >> c[i];
    for (int(i) = (0); (i) < ((n)); ++(i)) cin >> x[i];
    for (int(i) = (0); (i) < ((256)); ++(i)) dp[i] = INF;
    dp[128] = 0;
    for (int(i) = (0); (i) < ((n)); ++(i)) {
      vector<int> tmp(256, INF);
      for (int(j) = (0); (j) < ((m)); ++(j)) {
        for (int(k) = (0); (k) < ((256)); ++(k)) {
          int t = k + c[j];
          if (t < 0)
            tmp[0] = min(tmp[0], dp[k]);
          else if (t > 255)
            tmp[255] = min(tmp[255], dp[k]);
          else
            tmp[t] = min(tmp[t], dp[k]);
        }
      }
      for (int(j) = (0); (j) < ((256)); ++(j)) {
        if (tmp[j] == INF) continue;
        tmp[j] += (j - x[i]) * (j - x[i]);
      }
      dp = tmp;
    }
    int ans = INF;
    for (int(i) = (0); (i) < ((256)); ++(i)) {
      ans = min(ans, dp[i]);
    }
    cout << ans << endl;
  }
}
}  // namespace
int main() try { mainmain(); } catch (...) {
}
