#include <bits/stdc++.h>
using namespace std;
class DP {
 public:
  int cost, n, x;
  int Map[22 * 22];
  DP() {}
  DP(int xx, int nn, int cc) {
    cost = cc;
    x = xx;
    n = nn;
  }
  bool operator<(const DP &opp) const { return cost > opp.cost; }
};
int C[20];
int x[20001];
int visit[20001][256];
int N, M;
int main() {
  int i, j;
  while (cin >> N >> M, N) {
    memset(visit, 0, sizeof(visit));
    for (i = 0; i < M; i++) cin >> C[i];
    for (i = 0; i < N; i++) cin >> x[i];
    priority_queue<DP> dp;
    DP S;
    dp.push(DP(128, 0, 0));
    while (!dp.empty()) {
      S = dp.top();
      dp.pop();
      if (visit[S.n][S.x]) continue;
      visit[S.n][S.x]++;
      if (S.n == N) break;
      for (i = 0; i < M; i++) {
        DP T = S;
        T.x += C[i];
        if (T.x > 255) T.x = 255;
        if (T.x < 0) T.x = 0;
        T.cost += (x[T.n] - T.x) * (x[T.n] - T.x);
        T.n++;
        dp.push(T);
      }
    }
    cout << S.cost << endl;
  }
  return 0;
}
