#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, x, cb[20];
  int temp;
  while (cin >> n >> m, n || m) {
    vector<int> input;
    vector<int> dp[256];
    for (int i = 0; i < m; i++) {
      cin >> cb[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> x;
      input.push_back(x);
    }
    for (int i = 0; i <= n; i++)
      for (int j = 0; j <= 255; j++) dp[j].push_back(10000000);
    dp[128][0] = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j <= 255; j++) {
        if (dp[j][i] != 10000000) {
          for (int k = 0; k < m; k++) {
            temp = j + cb[k];
            if (temp > 255)
              temp = 255;
            else if (temp < 0)
              temp = 0;
            dp[temp][i + 1] =
                min(dp[temp][i + 1],
                    dp[j][i] + (temp - input[i]) * (temp - input[i]));
          }
        }
      }
    int ans = 10000000;
    for (int i = 0; i <= 255; i++) {
      if (ans > dp[i][n]) ans = dp[i][n];
    }
    cout << ans << endl;
  }
  return 0;
}
