#include <bits/stdc++.h>
using namespace std;
int dp[20001][256];
int main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    int C[16], x[20000];
    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 < 20001; i++)
      for (int j = 0; j < 256; j++) dp[i][j] = (1 << 25);
    dp[0][128] = 0;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < 256; j++) {
        if (dp[i][j] == (1 << 25)) continue;
        for (int k = 0; k < M; k++) {
          int y = j + C[k];
          if (y < 0)
            y = 0;
          else if (y > 255)
            y = 255;
          int sqsum = (x[i] - y) * (x[i] - y);
          dp[i + 1][y] = min(dp[i + 1][y], dp[i][j] + sqsum);
        }
      }
    }
    cout << *min_element(dp[N], dp[N] + 256) << endl;
  }
  return 0;
}
