#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 20000;
const int MAX_M = 16;
const int y0 = 128;
int N, M;
int C[MAX_M + 1], x[MAX_N + 1];
int ans = 1e9;
void Rec(int index, int sum, int y) {
  if (index == N) {
    ans = min(ans, sum);
    return;
  }
  for (int i = 0; i < M; ++i) {
    int y_n = y + C[i];
    if (y_n < 0)
      y_n = 0;
    else if (y_n > 255)
      y_n = 255;
    int sum_n = sum + (x[index] - y_n) * (x[index] - y_n);
    if (sum_n < ans) Rec(index + 1, sum_n, y_n);
  }
}
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];
    ans = 1e9;
    Rec(0, 0, y0);
    cout << ans << endl;
  }
  return 0;
}
