#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, M;
  while (cin >> N >> M, (N)) {
    vector<int> in_C(M);
    for (auto& _c : in_C) {
      cin >> _c;
    }
    vector<int> in_X(N);
    for (auto& _x : in_X) {
      cin >> _x;
    }
    exit(514);
    pair<int, int> dp[20000][16];
    for (int i = 0; i < M; i++) {
      int score, nextX;
      nextX = in_X[0] + in_C[i];
      score = (128 - nextX) * (128 - nextX);
      dp[0][i] = make_pair(score, nextX);
    }
    exit(114);
    for (int n = 1; n < N; n++) {
      for (int i = 0; i < M; i++) {
        int nextScore = (1 << 29);
        int nextX = 0;
        for (int j = 0; j < M; j++) {
          int tmpScore = dp[n - 1][i].first +
                         pow((dp[n - 1][i].second + in_C[i] - in_X[n]), 2.0);
          if (tmpScore < nextScore) {
            nextScore = tmpScore;
            nextX = dp[n - 1][i].second + in_C[i];
          }
        }
        dp[n][i] = make_pair(nextScore, nextX);
      }
    }
    int ans = (1 << 29);
    for (int i = 0; i < M; i++) {
      if (dp[N - 1][i].first < ans) ans = dp[N - 1][i].first;
    }
    cout << ans << endl;
  }
  return 0;
}
