#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
double EPS = 1e-10;
int main() {
  int n, m;
  while (cin >> n >> m && (n || m)) {
    int a[n], b[m];
    long long dp[n + 1][256];
    for (int i = 0; i < (m); i++) cin >> b[i];
    for (int i = 0; i < (n); i++) cin >> a[i];
    for (int i = 0; i < (n + 1); i++)
      for (int j = 0; j < (256); j++) dp[i][j] = -1;
    dp[0][128] = 0;
    for (int i = 0; i < (n); i++) {
      for (int j = 0; j < (256); j++) {
        if (dp[i][j] == -1) continue;
        for (int k = 0; k < (m); k++) {
          int nxt = j + b[k];
          nxt = max(0, min(255, nxt));
          if (dp[i + 1][nxt] == -1)
            dp[i + 1][nxt] = dp[i][j] + (a[i] - nxt) * (a[i] - nxt);
          else
            dp[i + 1][nxt] =
                min(dp[i + 1][nxt], dp[i][j] + (a[i] - nxt) * (a[i] - nxt));
        }
      }
    }
    long long ans = -1;
    for (int i = 0; i < (256); i++)
      if (dp[n][i] != -1) {
        if (ans == -1)
          ans = dp[n][i];
        else
          ans = min(ans, dp[n][i]);
      }
    cout << ans << endl;
  }
}
