#include <bits/stdc++.h>
using namespace std;
int N, M, C[16], x[20000];
int dp[20000][256];
int main() {
  while (cin >> N >> M, (N | M)) {
    for (int i = 0; i < M; i++) {
      cin >> C[i];
    }
    for (int i = 0; i < N; i++) {
      cin >> x[i];
    }
    int ans = 1e8;
    fill(dp[0], dp[20000], 1e8);
    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 t = j + C[k];
          if (t < 0) t = 0;
          if (t > 255) t = 255;
          dp[i + 1][t] = min(dp[i + 1][t], dp[i][j] + (t - x[i]) * (t - x[i]));
        }
      }
    }
    for (int i = 0; i <= 255; i++) {
      ans = min(ans, dp[N][i]);
    }
    cout << ans << endl;
  }
  return 0;
}
