#include <bits/stdc++.h>
using namespace std;
int n;
int m;
int price;
int a;
int b;
struct G {
  string name;
  double value;
};
bool operator>(const G& left, const G& right) {
  if (left.value > right.value) {
    return true;
  } else if (left.value < right.value) {
    return false;
  } else {
    return left.name < right.name;
  }
}
bool operator<(const G& left, const G& right) {
  if (left.value > right.value) {
    return false;
  } else if (left.value < right.value) {
    return true;
  } else {
    return left.name > right.name;
  }
}
int main() {
  while (true) {
    cin >> n;
    if (n == 0) {
      break;
    }
    cin >> m;
    if (m == 0) {
      break;
    }
    vector<int> codebook;
    vector<int> data;
    for (int i = 0; i < m; i++) {
      cin >> a;
      codebook.push_back(a);
    }
    for (int i = 0; i < n; i++) {
      cin >> a;
      data.push_back(a);
    }
    map<int, int> hash;
    hash[128] = 0;
    for (int i = 0; i < n; i++) {
      int d = data[i];
      map<int, int>::iterator it = hash.begin();
      map<int, int> next_hash;
      map<int, int>::iterator next_hash_end = next_hash.end();
      while (it != hash.end()) {
        for (int k = 0; k < m; k++) {
          int f = max(min((*it).first + codebook[k], 255), 0);
          int div = f - d;
          int s = (*it).second + div * div;
          if (next_hash.find(f) != next_hash_end) {
            next_hash[f] = min(next_hash[f], s);
          } else {
            next_hash[f] = s;
          }
        }
        ++it;
      }
      hash = next_hash;
    }
    int mi = INT_MAX;
    map<int, int>::iterator it = hash.begin();
    while (it != hash.end()) {
      mi = min((*it).second, mi);
      it++;
    }
    cout << mi << endl;
  }
}
