#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n && m) {
    vector<int> c(m), x(n);
    for (int i = 0; i < m; i++) {
      cin >> c[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> x[i];
    }
    vector<vector<int> > dp(n + 1, vector<int>(256, INT_MAX));
    dp[0][128] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 256; j++) {
        if (dp[i][j] == INT_MAX) continue;
        for (int k = 0; k < m; k++) {
          int y = j + c[k];
          if (y < 0) y = 0;
          if (y > 255) y = 255;
          dp[i + 1][y] =
              min(dp[i + 1][y], dp[i][j] + (int)pow((double)y - x[i], 2));
        }
      }
    }
    int ans = INT_MAX;
    for (int i = 0; i < 256; i++) {
      ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
}
