#include <bits/stdc++.h>
using namespace std;
struct data {
  vector<int> base;
  int val;
  data() {}
  data(vector<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];
    vector<int> a;
    a.push_back(128);
    for (int j = 0; j < M; ++j) {
      dp[0][j] = data(a, 0);
    }
    a.clear();
    for (int i = 1; i < N + 1; ++i) {
      for (int j = 0; j < M; ++j) {
        dp[i][j] = data(a, 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) {
          for (int q = 0; q < dp[i - 1][j].base.size(); ++q) {
            int s = dp[i - 1][j].base[q] + 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;
              vector<int> t;
              t.push_back(s);
              dp[i][k] = data(t, v);
            } else if (dp[i][k].val == dp[i - 1][j].val + tmp) {
              dp[i][k].base.push_back(s);
            }
          }
        }
      }
    }
    int ans = 1e9;
    for (int j = 0; j < M; ++j) {
      ans = min(ans, dp[N][j].val);
    }
    cout << ans << endl;
  }
  return 0;
}
