#include <bits/stdc++.h>
using namespace std;
template <typename T>
T& max(T& a, T& b) {
  if (a >= b) return a;
  return b;
}
template <typename T>
T& min(T& a, T& b) {
  if (a < b) return a;
  return b;
}
template <typename T>
bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T>
T get() {
  T a;
  cin >> a;
  return a;
}
template <typename T>
T rev(T a) {
  reverse(a.begin(), a.end());
  return a;
}
template <typename T>
vector<T>& sort(vector<T>& a) {
  sort(a.begin(), a.end());
  return a;
}
const long long inf = 1e9;
const long long linf = 3e18;
const double eps = 1e-9;
long long N, M;
long long c[16], x[20001];
long long dp[20001][256];
signed main() {
  for (long long i = 0; i <= 20000; ++i)
    for (long long j = 0; j < 256; ++j) dp[i][j] = inf;
  cin >> N >> M;
  for (long long i = 0; i < M; ++i) cin >> c[i];
  for (long long i = 1; i <= N; ++i) cin >> x[i];
  dp[0][128] = 0;
  for (long long i = 1; i <= N; ++i) {
    for (long long j = 0; j < 256; ++j) {
      if (dp[i - 1][j] != inf) {
        for (long long k = 0; k < M; ++k) {
          long long v = (c[k] < 0 ? max(j + c[k], 0ll) : min(j + c[k], 255ll));
          long long w = abs(v - x[i]);
          chmin(dp[i][v], dp[i - 1][j] + w * w);
        }
      }
    }
  }
  cout << *min_element(dp[N], dp[N] + 256) << endl;
}
