#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 20000;
const int MAX_M = 16;
const long long INF = 1e18;
const int y0 = 128;
int N, M;
int C[MAX_M + 1], x[MAX_N + 1];
long long dp[MAX_N + 10][260];
int main() {
  while (cin >> N >> M, N || M) {
    for (int i = 0; i < M; ++i) cin >> C[i];
    for (int i = 0; i < N; ++i) cin >> x[i];
    for (int i = 0; i <= N; ++i) fill(dp[i], dp[i] + 259, INF);
    for (int i = 0; i <= 255; ++i) dp[0][i] = 0;
    for (int n = 0; n < N; ++n) {
      for (int y = 0; y <= 255; ++y) {
        for (int i = 0; i < M; ++i) {
          int next_y = y + C[i];
          if (next_y < 0)
            next_y = 0;
          else if (next_y > 255)
            next_y = 255;
          dp[n + 1][next_y] = min(dp[n + 1][next_y],
                                  dp[n][y] + (x[n] - next_y) * (x[n] - next_y));
        }
      }
    }
    long long ans = INF;
    for (int i = 0; i <= 255; ++i) ans = min(ans, dp[N][i]);
    cout << ans << endl;
  }
  return 0;
}
