#include <bits/stdc++.h>
using namespace std;
int x[63000][260], n, m, y[63000], z[63000], d, minx;
int main() {
  while (true) {
    minx = 2000000000;
    for (int i = 0; i < 65536; i++) {
      for (int j = 0; j < 256; j++) {
        x[i][j] = 2000000000;
      }
    }
    cin >> n >> m;
    if (!n) {
      break;
    }
    for (int i = 0; i < m; i++) {
      cin >> y[i];
    }
    for (int i = 1; i <= n; i++) {
      cin >> z[i];
    }
    x[0][128] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < 256; j++) {
        if (x[i - 1][j] < 1000000000) {
          for (int k = 0; k < m; k++) {
            d = y[k] + j;
            if (d < 0) {
              d = 0;
            }
            if (d > 255) {
              d = 255;
            }
            x[i][d] = min(x[i][d], x[i - 1][j] + (int)pow(1.0 * (z[i] - d), 2));
          }
        }
      }
    }
    for (int i = 0; i < 256; i++) {
      minx = min(minx, x[n][i]);
    }
    cout << minx << endl;
  }
}
