#include <bits/stdc++.h>
using namespace std;
int N, M;
int C[20];
int X[20004];
const long long INF = 1000000000000;
long long dp[20004][130];
long long dfs(int i_, int y_) {
  if (i_ == N) return dp[i_][y_] = 0;
  if (dp[i_][y_] != INF) return dp[i_][y_];
  long long ret = INF;
  for (int i = 0; i < (M); i++) {
    int y = y_ + C[i];
    y = max(y, 0);
    y = min(y, 255);
    ret = min(ret, dfs(i_ + 1, y) + (X[i_] - y) * (X[i_] - y));
  }
  return dp[i_][y_] = ret;
}
void solve() {
  for (int i = 0; i < (20004); i++)
    for (int j = 0; j < (130); j++) dp[i][j] = INF;
  for (int i = 0; i < (M); i++) cin >> C[i];
  for (int i = 0; i < (N); i++) cin >> X[i];
  long long ans = INF;
  ans = min(ans, dfs(0, 128));
  for (int j = 0; j < (130); j++) {
    ans = min(ans, dp[0][j]);
  }
  cout << ans << endl;
  for (int i = 0; i < (M); i++) C[i] = 0;
  for (int i = 0; i < (N); i++) X[i] = 0;
}
int main() {
  while (cin >> N >> M) {
    if (N + M == 0) return 0;
    solve();
  }
  return 0;
}
