#include <bits/stdc++.h>
using namespace std;
struct DP {
  int first, second;
};
int fix(int num) { return min(255, max(num, 0)); }
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;
    }
    DP dp[2][16];
    for (int i = 0; i < M; i++) {
      int score, nextX;
      nextX = fix(128 + in_C[i]);
      score = pow(abs(in_X[0] - nextX), 2.0);
      dp[0][i] = DP{score, nextX};
    }
    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 =
              (int)dp[0][i].first +
              pow(fix(abs((int)dp[0][i].second + in_C[j] - in_X[n])), 2.0);
          if ((int)tmpScore < (int)nextScore) {
            nextScore = tmpScore;
            nextX = fix((int)dp[0][i].second + in_C[j]);
          }
        }
        dp[1][i] = DP{(int)nextScore, nextX};
      }
      for (int i = 0; i < M; i++) dp[0][i] = dp[1][i];
    }
    int ans = (1 << 29);
    for (int i = 0; i < M; i++) {
      if (dp[0][i].first < ans) ans = dp[0][i].first;
    }
    cout << ans << endl;
  }
  return 0;
}
