#include <bits/stdc++.h>
using namespace std;
int ta[20], c[25000];
int dp[20000][256];
const int INF = 1800000000;
int main() {
  int n, m;
  while (cin >> n >> m, n + m) {
    for (int i = 0; i < m; i++) cin >> ta[i];
    for (int j = 0; j < n; j++) cin >> c[j];
    for (int i = 0; i < 20000; i++) fill(dp[i], dp[i] + 256, INF);
    dp[0][128] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 256; j++) {
        if (dp[i][j] != INF) {
          int tmp = j;
          for (int k = 0; k < m; k++) {
            if (j + ta[k] < 0)
              tmp = 0;
            else if (j + ta[k] > 255)
              tmp = 255;
            else
              tmp = j + ta[k];
            dp[i + 1][tmp] =
                min(dp[i + 1][tmp], dp[i][j] + (c[i] - tmp) * (c[i] - tmp));
          }
        }
      }
    }
    int ans = INF;
    for (int i = 0; i < 256; i++) ans = min(ans, dp[n][i]);
    cout << ans << endl;
  }
  return 0;
}
