#include <bits/stdc++.h>
using namespace std;
long long c[20000], x[16];
long long dp[21][256];
int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n + m == 0) break;
    for (int i = 0; i < m; i++) cin >> c[i];
    for (int i = 0; i < n; i++) cin >> x[i];
    for (int i = 0; i < 21; i++)
      for (int j = 0; j < 256; j++) dp[i][j] = 1e18;
    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 nxt = 0;
          if (j + c[k] < 0) nxt = 0;
          if (j + c[k] > 255) nxt = 255;
          nxt = j + c[k];
          dp[i + 1][nxt] =
              min(dp[i + 1][nxt], dp[i][j] + (x[i] - nxt) * (x[i] - nxt));
        }
      }
    }
    long long ans = 1e18;
    for (int i = 0; i < 256; i++) {
      ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
  return 0;
}
