#include <bits/stdc++.h>
using namespace std;
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
  os << "[";
  for (const auto& v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}
long long dp[20000 + 10][256 + 10];
int add(int y, int diff) {
  int ny = y + diff;
  if (ny < 0) ny = 0;
  if (ny > 255) ny = 255;
  return ny;
}
void solve(int N, int 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 < 20000; i++)
    for (int j = 0; j < 256; j++) dp[i][j] = LLONG_MAX / 3;
  dp[0][128] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 256; j++) {
      for (int k = 0; k < M; k++) {
        long long nx = add(j, c[k]);
        long long dif = x[i] - nx;
        dp[i + 1][add(j, c[k])] =
            min(dp[i + 1][add(j, c[k])], dp[i][j] + dif * dif);
      }
    }
  }
  long long ans = LLONG_MAX / 3;
  for (int i = 0; i < 256; i++) {
    ans = min(ans, dp[N][i]);
  }
  cout << ans << endl;
}
int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 and m == 0) break;
    solve(n, m);
  }
}
