#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
double EPS = 1e-10;
int main() {
  int n, m;
  while (cin >> n >> m && (n || m)) {
    int a[n], b[m];
    long long ans = 1e18;
    for (int i = 0; i < (m); i++) cin >> b[i];
    for (int i = 0; i < (n); i++) cin >> a[i];
    for (int i = 0; i < (m); i++) {
      int now = 128;
      long long res = 0;
      for (int j = 0; j < (n); j++) {
        now += b[i];
        now = min(255, max(0, now));
        res += (a[j] - now) * (a[j] - now);
      }
      ans = min(ans, res);
    }
    cout << ans << endl;
  }
}
