#include <bits/stdc++.h>
using namespace std;
int n;
int m;
uint x;
vector<int> c;
bool bef = 0;
bool aft = 1;
const int size = 255 + 1;
vector<vector<uint>> vec(2, vector<uint>(size, -1));
inline uint mse(uint a, uint b) {
  uint diff = (a > b) ? (a - b) : (b - a);
  return diff * diff;
}
uint calcBefMin(int i) {
  uint min_val = -1;
  for (auto v : c) {
    int key = i - v;
    if (key < 0 or key > 255) {
      continue;
    }
    min_val = min(min_val, vec[bef][key]);
  }
  return min_val;
}
inline uint myplus(uint a, uint b) {
  uint m = -1;
  if (b <= m - a) {
    return a + b;
  }
  return -1;
}
void solve() {
  c.resize(m);
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }
  int c_max = *std::max_element(begin(c), end(c));
  int c_min = *std::min_element(begin(c), end(c));
  for (int i = 0; i <= 255; i++) {
    vec[bef][i] = mse(128, i);
  }
  for (int i = 0; i < n; i++) {
    cin >> x;
    int bef_key;
    for (bef_key = 1; bef_key <= 254; bef_key++) {
      vec[aft][bef_key] = myplus(calcBefMin(bef_key), mse(bef_key, x));
    }
    bef_key = 255;
    uint min_val = -1;
    for (int key = bef_key - c_max; key <= 255; key++) {
      min_val = min(min_val, vec[bef][key]);
    }
    vec[aft][bef_key] = myplus(min_val, mse(bef_key, x));
    bef_key = 0;
    min_val = -1;
    for (int key = bef_key - c_min; key >= 0; key--) {
      min_val = min(min_val, vec[bef][key]);
    }
    vec[aft][bef_key] = myplus(min_val, mse(bef_key, x));
    bef = !bef;
    aft = !aft;
  }
  cout << *std::min_element(begin(vec[bef]), end(vec[bef])) << endl;
}
int main() {
  while (true) {
    cin >> n >> m;
    if (n == 0 and m == 0) {
      break;
    }
    solve();
  }
  return 0;
}
