#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n, m;
  while (cin >> n >> m && n && m) {
    vector<int> C(m);
    for (int i = 0; i < m; i++) {
      cin >> C[i];
    }
    vector<int> x(n + 1);
    x[0] = 0;
    for (int i = 0; i < n; i++) {
      cin >> x[i + 1];
    }
    vector<vector<int> > dp =
        vector<vector<int> >(n + 1, vector<int>(256, 1 << 24));
    dp[0][128] = 0;
    for (int k = 1; k <= n; k++) {
      for (int i = 0; i < 256; i++) {
        for (int j = 0; j < m; j++) {
          int next = i + C[j];
          next = next < 0 ? 0 : next;
          next = next > 255 ? 255 : next;
          dp[k][next] =
              min(dp[k][next], dp[k - 1][i] + ((next - x[k]) * (next - x[k])));
        }
      }
    }
    int answer = 1 << 24;
    for (int i = 0; i < 256; i++) answer = min(answer, dp[n][i]);
    cout << answer << endl;
  }
  return 0;
}
