#include <bits/stdc++.h>
using namespace std;
long long dx[4] = {1, 0, -1, 0};
long long dy[4] = {0, -1, 0, 1};
long long nextInt() {
  long long a;
  cin >> a;
  return a;
}
char nextChar() {
  char a;
  cin >> a;
  return a;
}
double nextDouble() {
  double a;
  cin >> a;
  return a;
}
string nextString() {
  string a;
  cin >> a;
  return a;
}
void inputVector(vector<long long> &v, long long &n) {
  for (long long i = 0; i < (n); i++) {
    v.push_back(nextInt());
  }
}
void inputVector(vector<double> &v, long long &n) {
  for (long long i = 0; i < (n); i++) {
    v.push_back(nextDouble());
  }
}
void inputVector(vector<string> &v, long long &n) {
  for (long long i = 0; i < (n); i++) {
    v.push_back(nextString());
  }
}
struct Info {
  long long val;
  long long sum;
  Info() {}
  Info(long long v, long long s) {
    val = v;
    sum = s;
  }
  bool operator<(const Info &a) const { return sum < a.sum; }
};
signed main() {
  long long N, M;
  while (cin >> N >> M) {
    if (N == 0) break;
    vector<long long> C, x;
    inputVector(C, M);
    inputVector(x, N);
    vector<Info> v, w;
    v.push_back(Info(128, 0));
    for (long long i = 0; i < (N); i++) {
      w.clear();
      for (long long j = 0; j < (v.size()); j++) {
        for (long long k = 0; k < (M); k++) {
          Info info = v[j];
          info.val += C[k];
          if (info.val > 255)
            info.val = 255;
          else if (info.val < 0)
            info.val = 0;
          info.sum += (x[i] - info.val) * (x[i] - info.val);
          w.push_back(info);
        }
      }
      sort((w).begin(), (w).end());
      if (w.size() > 300) w.resize(300);
      swap(v, w);
    }
    cout << v[0].sum << endl;
  }
  return 0;
}
