#include <bits/stdc++.h>
using namespace std;
int main() {
  int i, j, k, n, m;
  while (cin >> n >> m && n && m) {
    vector<int> C, x;
    for (i = 0; i < m; i++) {
      int a;
      cin >> a;
      C.push_back(a);
    }
    for (i = 0; i < n; i++) {
      int a;
      cin >> a;
      x.push_back(a);
    }
    long long y[256][2];
    for (i = 0; i < 256; i++) y[i][0] = y[i][1] = -1;
    y[128][0] = 0;
    for (i = 0; i < n; i++) {
      for (j = 0; j < m; j++) {
        for (k = 0; k < 256; k++) {
          if (y[k][0] != -1) {
            int a = k + C[j];
            if (a < 0)
              a = 0;
            else if (a > 255)
              a = 255;
            if (y[k][0] > (long long)(pow(10.0, 18))) continue;
            if (y[a][1] == -1)
              y[a][1] = y[k][0] + (x[i] - a) * (x[i] - a);
            else
              y[a][1] = min(y[a][1], y[k][0] + (x[i] - a) * (x[i] - a));
          }
        }
      }
      for (j = 0; j < 256; j++) {
        y[j][0] = y[j][1];
        y[j][1] = -1;
      }
    }
    long long ans = -1;
    for (i = 1; i < 256; i++) {
      if (y[i][0] != -1) {
        if (ans == -1)
          ans = y[i][0];
        else
          ans = min(ans, y[i][0]);
      }
    }
    cout << ans << endl;
  }
  return 0;
}
