#include <bits/stdc++.h>
using namespace std;
struct data {
  int base;
  int val;
  data() {}
  data(int base, int val) {
    this->base = base;
    this->val = val;
  }
};
int main() {
  int N, M;
  while (cin >> N >> M, (N | M)) {
    vector<int> C(M);
    vector<int> X(N + 1);
    for (int i = 0; i < M; ++i) {
      cin >> C[i];
    }
    for (int i = 1; i < N + 1; ++i) {
      cin >> X[i];
    }
    data dp[20010][17];
    for (int j = 0; j < M; ++j) {
      dp[0][j] = data(128, 0);
    }
    for (int i = 1; i < N + 1; ++i) {
      for (int j = 0; j < M; ++j) {
        dp[i][j] = data(-1, 1e9);
      }
    }
    for (int i = 1; i < N + 1; ++i) {
      for (int j = 0; j < M; ++j) {
        int b = -1;
        int v = 1e9;
        for (int k = 0; k < M; ++k) {
          int s = dp[i - 1][j].base + C[k];
          if (s < 0) s = 0;
          if (s > 255) s = 255;
          int tmp = (X[i] - s) * (X[i] - s);
          if (dp[i][k].val > dp[i - 1][j].val + tmp) {
            v = tmp + dp[i - 1][j].val;
            b = s;
            dp[i][k] = data(b, v);
          }
        }
      }
    }
    int ans = 1e9;
    for (int j = 0; j < M; ++j) {
      ans = min(ans, dp[N][j].val);
    }
    cout << ans << endl;
  }
  return 0;
}
