#include <bits/stdc++.h>
int solve(int N, int M) {
  std::vector<int> data(N);
  std::vector<int> codes(M);
  for (int i = 0; i < M; i++) {
    std::cin >> codes[i];
  }
  for (int i = 0; i < N; i++) {
    std::cin >> data[i];
  }
  struct Traverse {
    int n;
    int y;
    int diff;
  };
  auto compare = [](Traverse const& lhs, Traverse const& rhs) {
    return lhs.diff > rhs.diff;
  };
  std::priority_queue<Traverse, std::vector<Traverse>, decltype(compare)> queue(
      compare);
  queue.push(Traverse{0, 128, 0});
  while (!queue.empty()) {
    auto node = queue.top();
    queue.pop();
    if (node.n == N) {
      return node.diff;
    }
    for (auto code : codes) {
      int new_y = node.y + code;
      new_y = new_y < 0 ? 0 : new_y > 255 ? 255 : new_y;
      int diff = (new_y - data[node.n]) * (new_y - data[node.n]);
      queue.push(Traverse{node.n + 1, new_y, node.diff + diff});
    }
  }
}
int main() {
  int N, M;
  while (std::cin >> N >> M) {
    if (N == 0 && M == 0) {
      break;
    }
    std::cout << solve(N, M) << std::endl;
  }
}
