#include <bits/stdc++.h>
using namespace std;
int dp[200005][300];
int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0) break;
    vector<int> c(m);
    for (int i = 0; i < m; i++) cin >> c[i];
    vector<int> x(n);
    for (int i = 0; i < n; i++) cin >> x[i];
    for (int i = 0; i < n + 1; i++)
      for (int j = 0; j < 300; j++) dp[i][j] = 1 << 30;
    dp[0][128] = 0;
    for (int i = 1; i < n + 1; i++) {
      for (int j = 0; j < 256; j++) {
        if (dp[i - 1][j] == 1 << 30) continue;
        for (int k = 0; k < m; k++) {
          int to = j + c[k];
          to = max(to, 0);
          to = min(to, 255);
          int ret = (x[i - 1] - to) * (x[i - 1] - to);
          dp[i][to] = min(dp[i][j], dp[i - 1][j] + ret);
        }
      }
    }
    int ans = 1 << 30;
    for (int i = 0; i < 300; i++) ans = min(ans, dp[n][i]);
    cout << ans << endl;
  }
  return 0;
}
