#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, m;
  while (cin >> n >> m && n && m) {
    vector<int> C(m);
    for (int i = 0; i < m; i++) {
      cin >> C[i];
    }
    vector<int> x(n + 1);
    x[0] = 0;
    for (int i = 0; i < n; i++) {
      cin >> x[i + 1];
    }
    vector<int> y(n + 1);
    y[0] = 128;
    for (int p = 1; p <= n; p++) {
      int prev_x = x[p];
      long good = INT_MAX;
      int good_i = 0;
      for (int i = 0; i < m; i++) {
        int prev_y = y[p - 1] + C[i];
        for (int j = 0; j < m; j++) {
          int next_x = p < n ? x[p + 1] : 0;
          int next_y = p < n ? prev_y + C[j] : 0;
          if (next_y < 0) next_y = 0;
          if (next_y > 255) next_y = 255;
          long sum = (prev_x - prev_y) * (prev_x - prev_y) +
                     (next_x - next_y) * (next_x - next_y);
          if (sum < good) {
            good = sum;
            good_i = i;
          }
        }
      }
      y[p] = y[p - 1] + C[good_i];
      if (y[p] < 0) y[p] = 0;
      if (y[p] > 255) y[p] = 255;
    }
    long answer = 0;
    for (int p = 1; p <= n; p++) {
      answer += (x[p] - y[p]) * (x[p] - y[p]);
    }
    cout << answer << endl;
  }
  return 0;
}
