#include <bits/stdc++.h>
using namespace std;
int n, m;
int c[20] = {0};
int x[20005] = {0};
int dp[20005][20][2] = {0};
int solve();
int main() {
  int i, j, k;
  while (1) {
    cin >> n >> m;
    if (n + m == 0) break;
    for (i = 0; i < m; ++i) cin >> c[i];
    for (i = 0; i < n; ++i) cin >> x[i];
    for (i = 0; i < m; ++i)
      for (j = 0; j < n; ++j)
        for (k = 0; k < 2; ++k) dp[j][i][k] = 2000000000;
    cout << solve() << endl;
  }
  return 0;
}
int solve() {
  int i, j, k, ans = 2000000000;
  for (i = 0; i < n; ++i) {
    for (j = 0; j < m; ++j) {
      for (k = 0; k < m; ++k) {
        int now = 128, bf = 0;
        if (i != 0) {
          now = dp[i - 1][k][0];
          bf = dp[i - 1][k][1];
        }
        now += c[j];
        now = min(now, 255);
        now = max(now, 0);
        bf += (now - x[i]) * (now - x[i]);
        if (dp[i][j][1] > bf) {
          dp[i][j][1] = bf;
          dp[i][j][0] = now;
        }
        if (i == n - 1) ans = min(ans, dp[i][j][1]);
      }
    }
  }
  return ans;
}
