#include <bits/stdc++.h>
using namespace std;
int n, m;
int c[16];
int dp[20001][256];
void init() {
  for (int i = 0; i < 20001; ++i) {
    for (int j = 0; j < 256; ++j) {
      dp[i][j] = 20001 * 256;
    }
  }
  dp[0][128] = 0;
}
int code(int k) {
  if (k < 0) return 0;
  if (k >= 256) return 256 - 1;
  return k;
}
void solve(int& x, int& u, int& v) {
  for (int i = 0; i < m; ++i) {
    int j = code(v + c[i]);
    dp[u][j] = min(dp[u][j], (x - j) * (x - j) + dp[u - 1][v]);
  }
}
int main() {
  int x;
  while (1) {
    cin >> n >> m;
    if (!n && !m) break;
    init();
    for (int i = 0; i < m; ++i) cin >> c[i];
    for (int i = 1; i <= n; ++i) {
      cin >> x;
      for (int j = 0; j < 256; ++j) {
        if (dp[i - 1][j] != 20001 * 256) solve(x, i, j);
      }
    }
    int ans = 20001 * 256;
    for (int j = 0; j < 256; ++j) ans = min(ans, dp[n][j]);
    cout << ans << endl;
  }
  return 0;
}
