#include <bits/stdc++.h>
using namespace std;
int dp[25000][300];
int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    int c[20], x[25000];
    if (n == 0 && m == 0) break;
    for (int i = 0; i < 25000; i++) {
      for (int j = 0; j < 300; j++) {
        dp[i][j] = -1;
      }
    }
    for (int i = 0; i < m; i++) {
      cin >> c[i];
    };
    for (int i = 0; i < n; i++) {
      cin >> x[i];
    };
    dp[0][128] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < 256; j++) {
        if (dp[i - 1][j] != -1) {
          for (int k = 0; k < m; k++) {
            if (dp[i][max(min(j + c[k], 255), 0)] == -1) {
              dp[i][max(min(j + c[k], 255), 0)] =
                  dp[i - 1][j] + (max(min(j + c[k], 255), 0) - x[i - 1]) *
                                     (max(min(j + c[k], 255), 0) - x[i - 1]);
            } else
              dp[i][max(min(j + c[k], 255), 0)] = min(
                  dp[i - 1][j] + (max(min(j + c[k], 255), 0) - x[i - 1]) *
                                     (max(min(j + c[k], 255), 0) - x[i - 1]),
                  dp[i][max(min(j + c[k], 255), 0)]);
          }
        }
      }
    }
    int ans = 10000000;
    for (int i = 0; i < 300; i++) {
      if (dp[n][i] != -1) ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
  return 0;
}
