#include <bits/stdc++.h>
using namespace std;
int N, M;
int cb[16];
int sa[20000];
long long dp[20000][256];
long long dfs(long long n, long long d, long long k) {
  if (dp[d][k] != -1 && dp[d][k] <= n) return dp[d][k];
  if (d == N) return n;
  long long a = 100000000000000;
  int kk;
  for (int i = 0; i < M; i++) {
    kk = k + cb[i];
    if (kk > 255) kk = 255;
    if (kk < 0) kk = 0;
    a = min(a, dfs(n + (kk - sa[d]) * (kk - sa[d]), d + 1, k + cb[i]));
  }
  if (dp[d][k] != -1)
    dp[d][k] = min(a, dp[d][k]);
  else
    dp[d][k] = a;
  return a;
}
int main(void) {
  std::ios_base::sync_with_stdio();
  while (1) {
    cin >> N >> M;
    if (N == 0 && M == 0) break;
    for (int i = 0; i < M; i++) {
      cin >> cb[i];
    }
    for (int i = 0; i < N; i++) {
      cin >> sa[i];
    }
    for (int i = 0; i < 20000; i++) {
      for (int j = 0; j < 256; j++) {
        dp[i][j] = -1;
      }
    }
    cout << dfs(0, 0, 128) << endl;
  }
  return 0;
}
