#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e15;
const long long Y = 128;
int main() {
  long long n, m;
  while (cin >> n >> m, n) {
    vector<long long> cod(m), signal(n + 1);
    vector<vector<pair<long long, long long>>> dp(
        n + 1, vector<pair<long long, long long>>(m, make_pair(INF, INF)));
    double ave_signal = 0;
    for (int i = 0; i < m; i++) {
      cin >> cod[i];
    }
    for (int i = 1; i < n + 1; i++) {
      cin >> signal[i];
      ave_signal += signal[i];
    }
    ave_signal /= n;
    for (int i = 0; i < m; i++) {
      dp[0][i].first = Y;
      dp[0][i].second = 0;
    }
    for (int i = 1; i < n + 1; i++) {
      for (int j = 0; j < m; j++) {
        int min_line = ave_signal;
        int max_line = signal[i];
        if (ave_signal > signal[i]) swap(min_line, max_line);
        bool is_bad = true;
        for (int k = 0; k < m; k++) {
          int tmp_signal = dp[i - 1][k].first + cod[j];
          if (tmp_signal > 255)
            tmp_signal = 255;
          else if (tmp_signal < 0)
            tmp_signal = 0;
          if (dp[i - 1][k].second +
                  (signal[i] - tmp_signal) * (signal[i] - tmp_signal) <
              dp[i][j].second) {
            is_bad = false;
            dp[i][j].second =
                dp[i - 1][k].second +
                (signal[i] - tmp_signal) * (signal[i] - tmp_signal);
            dp[i][j].first = tmp_signal;
          } else if (is_bad) {
            dp[i][j].second =
                dp[i - 1][k].second +
                (signal[i] - tmp_signal) * (signal[i] - tmp_signal);
            dp[i][j].first = tmp_signal;
          }
        }
      }
    }
    long long ans = INF;
    for (int i = 0; i < m; i++) ans = min(ans, dp[n][i].second);
    cout << ans << endl;
  }
  return 0;
}
