#include <bits/stdc++.h>
using namespace std;
template <typename T>
T &chmin(T &a, const T &b) {
  return a = min(a, b);
}
template <typename T>
T &chmax(T &a, const T &b) {
  return a = max(a, b);
}
using ll = long long;
using ld = long double;
const int INF = 1e9;
const ld eps = 1e-9, pi = acos(-1.0);
int N, M;
vector<int> code(16);
vector<int> sig(20000);
int score[20005][16];
int val[20005][16];
int main() {
  while (cin >> N >> M and N + M) {
    for (int i = 0; i < (int)(M); i++) code[i] = 0;
    for (int i = 0; i < (int)(N); i++) sig[i] = 0;
    for (int i = 0; i < (int)(N + 1); i++)
      for (int j = 0; j < (int)(M); j++) score[i][j] = INF;
    for (int j = 0; j < (int)(M); j++) score[0][j] = 0;
    for (int i = 0; i < (int)(M); i++) cin >> code[i];
    for (int i = 0; i < (int)(N); i++) cin >> sig[i];
    for (int i = 0; i < (int)(M); i++) val[0][i] = 128;
    for (int i = 0; i < (int)(N); i++) {
      for (int j = 0; j < (int)(M); j++)
        for (int k = 0; k < (int)(M); k++) {
          int tgt = min(255, max(0, val[i][j] + code[k]));
          if (score[i + 1][k] > score[i][j] + (sig[i] - tgt) * (sig[i] - tgt)) {
            score[i + 1][k] = score[i][j] + (sig[i] - tgt) * (sig[i] - tgt);
            val[i + 1][k] = tgt;
          }
        }
    }
    int minim = INF;
    for (int i = 0; i < (int)(M); i++) chmin(minim, score[N][i]);
    cout << minim << endl;
  }
  return 0;
}
