#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, dp[20][260], cb[20], input[20001];
  int temp;
  while (cin >> n >> m, n || m) {
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i < m; i++) cin >> cb[i];
    for (int i = 0; i < n; i++) {
      cin >> input[i];
    }
    for (int i = 0; i <= n; i++) fill(dp[i], dp[i] + 256, 100000000);
    dp[0][128] = 0;
    for (int i = 0; i < n; i++)
      for (int j = 0; j <= 255; j++) {
        if (dp[i][j] != 100000000) {
          for (int k = 0; k < m; k++) {
            temp = j + cb[k];
            if (temp > 255)
              temp = 255;
            else if (temp < 0)
              temp = 0;
            dp[i + 1][temp] =
                min(dp[i + 1][temp],
                    dp[i][j] + (temp - input[i]) * (temp - input[i]));
          }
        }
      }
    int ans = 100000000;
    for (int i = 0; i <= 255; i++) {
      if (ans > dp[n][i]) ans = dp[n][i];
    }
    cout << ans << endl;
  }
  return 0;
}
