#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int N, M;
  while (cin >> N >> M, N || M) {
    int c[M];
    for (int i = (0); i < (M); ++i) cin >> c[i];
    int x[N];
    for (int i = (0); i < (N); ++i) cin >> x[i];
    long long dp[N + 1][256];
    for (int i = (0); i < (N + 1); ++i)
      for (int j = (0); j < (256); ++j) dp[i][j] = 1e15;
    dp[0][128] = 0;
    for (int i = (0); i < (N); ++i) {
      for (int j = (0); j < (256); ++j) {
        if (dp[i][j] == 1e15) continue;
        for (int k = (0); k < (M); ++k) {
          int y = max(0, min(255, j + c[k]));
          dp[i + 1][y] =
              min(dp[i + 1][y], dp[i][j] + (long long)(x[i] - y) * (x[i] - y));
        }
      }
    }
    long long ans = 1e15;
    for (int i = (0); i < (256); ++i) ans = min(ans, dp[N][i]);
    cout << ans << endl;
  }
  return 0;
}
