#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0) break;
    int result[20001][256];
    for (int i = 0; i < n + 1; i++) {
      for (int j = 0; j < 256; j++) {
        result[i][j] = 10000000;
      }
    }
    result[0][128] = 0;
    int c[17], x[20001];
    for (int i = 1; i < m + 1; i++) {
      cin >> c[i];
    }
    for (int i = 1; i < n + 1; i++) {
      cin >> x[i];
    }
    for (int i = 1; i < n + 1; i++) {
      for (int j = 0; j < 256; j++) {
        int nexty = j + c[i];
        if (nexty < 0)
          nexty = 0;
        else if (nexty > 255)
          nexty = 255;
        result[i][nexty] =
            min(result[i][nexty],
                result[i - 1][j] + (x[i] - nexty) * (x[i] - nexty));
      }
    }
    int ans = 1000000;
    for (int i = 1; i < 256; i++) ans = min(ans, result[n][i]);
    cout << ans << endl;
  }
  return 0;
}
