#include <bits/stdc++.h>
using namespace std;
int r(int a) { return max(0, min(255, a)); }
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    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];
    }
    static int a[20000][16], e[20000][16];
    for (int j = 0; j < m; j++) {
      a[0][j] = r(128 + c[j]);
      e[0][j] = (x[0] - a[0][j]) * (x[0] - a[0][j]);
    }
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < m; j++) {
        e[i][j] = 1000000000;
        for (int k = 0; k < m; k++) {
          int at = r(a[i - 1][k] + c[j]);
          int et = e[i - 1][k] + (x[i] - at) * (x[i] - at);
          if (et < e[i][j]) {
            a[i][j] = at;
            e[i][j] = et;
          }
        }
      }
    }
    int em = 1000000000;
    for (int j = 0; j < m; j++) {
      em = min(em, e[n - 1][j]);
    }
    cout << em << endl;
  }
  return 0;
}
