#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::list;
using std::make_pair;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set;
using std::setprecision;
using std::stack;
using std::string;
using std::vector;
template <typename T>
using Vec = vector<T>;
int main(void) {
  while (1) {
    int n;
    int m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    Vec<int> c(m);
    Vec<int> x(n + 1);
    for (int i = 0; i < m; i++) {
      cin >> c[i];
    }
    for (int j = 1; j <= n; j++) {
      cin >> x[j];
    }
    Vec<Vec<int> > dp(n + 1, Vec<int>(256));
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < 256; j++) {
        dp[i][j] = (int)1e09;
      }
    }
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j < 256; j++) {
        for (int k = 0; k < m; k++) {
          int div = j + c[k];
          if (div > 255) {
            div = 255;
          } else if (div < 0) {
            div = 0;
          }
          int cost = (x[i] - div) * (x[i] - div);
          dp[i][div] = min(dp[i][div], dp[i - 1][j] + cost);
        }
      }
    }
    int ans = dp[n][0];
    for (int j = 1; j < 256; j++) {
      ans = min(ans, dp[n][j]);
    }
    cout << ans << endl;
  }
  return 0;
}
