#include <bits/stdc++.h>
using namespace std;
long long int dp[2][16][2];
long long int sample[20010];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  while (1) {
    long long int N, M, code[16];
    cin >> N >> M;
    if (N == 0 && M == 0) break;
    for (int i = 0; i < (int)(M); i++) cin >> code[i];
    for (int i = 0; i < (int)(N); i++) cin >> sample[i];
    for (int i = 0; i < (int)(M); i++) {
      dp[0][i][0] = 0;
      dp[0][i][1] = 128;
    }
    for (int i = 0; i < (int)(N); i++) {
      for (int j = 0; j < (int)(M); j++) {
        dp[(i + 1) % 2][j][0] = 1300500000;
        for (int k = 0; k < (int)(M); k++) {
          long long int now = max(0LL, min(255LL, dp[i % 2][k][1] + code[j]));
          long long int diff = sample[i] - now;
          if (dp[i % 2][k][0] + diff * diff < dp[(i + 1) % 2][j][0]) {
            dp[(i + 1) % 2][j][0] = dp[i % 2][k][0] + diff * diff;
            dp[(i + 1) % 2][j][1] = now;
          }
        }
      }
    }
    long long int ans = 1300500000;
    for (int i = 0; i < (int)(M); i++) ans = min(ans, dp[N % 2][i][0]);
    cout << ans << endl;
  }
  return 0;
}
