#include <bits/stdc++.h>
using namespace std;
template <class T, class U>
inline void chmin(T &t, U f) {
  if (t > f) t = f;
}
template <class T, class U>
inline void chmax(T &t, U f) {
  if (t < f) t = f;
}
long long n, m;
long long dp[200001][256];
long long c[16], x[20000];
long long rec(long long p, long long y) {
  if (dp[p][y] >= 0) return dp[p][y];
  if (p == n) return 0;
  long ret = LONG_MAX;
  for (long long i = 0; i < m; ++i) {
    long long ny = y + c[i];
    if (ny < 0)
      ny = 0;
    else if (ny > 255)
      ny = 255;
    ret = ((ret) > (rec(p + 1, ny) + (ny - x[p]) * (ny - x[p]))
               ? (rec(p + 1, ny) + (ny - x[p]) * (ny - x[p]))
               : (ret));
  }
  return dp[p][y] = ret;
}
void solve() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  while (1) {
    cin >> n >> m;
    if (n == 0) break;
    for (int i = (int)(0); i < (int)(m); i++) cin >> c[i];
    for (int i = (int)(0); i < (int)(n); i++) cin >> x[i];
    memset(dp, -1, sizeof(dp));
    printf("%lld\n", rec(0, 128));
  }
}
int main() {
  solve();
  return 0;
}
