#include <vector>
#include <iostream>

int main() {
  while (true) {
    int N, M;
    std::cin >> N >> M;
    if (N == 0 && M == 0) {
      break;
    }
    std::vector<int> C, X;
    for (int i = 0; i < M; i++) {
      int c;
      std::cin >> c;
      C.push_back(c);
    }
    for (int i = 0; i < N; i++) {
      int x;
      std::cin >> x;
      X.push_back(x);
    }
    const int kMaxErr = 255 * 255 * N;
    std::vector<int> dp(256, kMaxErr);
    dp[128] = 0;
    for (int x : X) {
      std::vector<int> new_dp(256, kMaxErr);
      for (int i = 0; i < 256; i++) {
        for (int c: C) {
          int new_val = std::max(0, std::min(255, i + c));
          int err = (new_val - x) * (new_val - x);
          new_dp[new_val] = std::min(new_dp[new_val], dp[i] + err);
        }
      }
      dp = std::move(new_dp);
    }
    int ans = kMaxErr;
    for (int err : dp) {
      ans = std::min(ans, err);
    }
    std::cout << ans << std::endl;
  }
}




