#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 50;
long long dp[20010][260];
long long c[16];
long long x[20000];
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  while (cin >> n >> m, n) {
    for (int i = 0; i < m; i++) {
      cin >> c[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> x[i];
    }
    fill((long long*)begin(dp), (long long*)end(dp), INF);
    dp[0][128] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= 255; j++) {
        for (int k = 0; k < m; k++) {
          int nj = max(min(j + c[k], 255LL), 0LL);
          dp[i + 1][nj] =
              min(dp[i + 1][nj], dp[i][j] + (x[i] - nj) * (x[i] - nj));
        }
      }
    }
    long long ans = INF;
    for (int j = 0; j <= 255; j++) {
      ans = min(ans, dp[n][j]);
    }
    cout << ans << endl;
  }
}
