#include <bits/stdc++.h>
using namespace std;

const int MAXN = 20001;
const int MAXM = 16;
const int MAXX = 256;
const int INF = 1<<28;

int N, M;
int C[MAXM], x[MAXN];
int dp[2][MAXX];

int main() {
  while(cin >> N >> M && (N|M)) {
    for(int i = 0; i < M; ++i) cin >> C[i];
    for(int i = 0; i < N; ++i) cin >> x[i];
    fill(dp[0], dp[2], INF);
    dp[0][128] = 0;
    for(int i = 0; i < N; ++i) {
      const int now = i & 1, next = 1 - now;
      for(int j = 0; j < MAXX; ++j) dp[next][j] = INF;
      for(int j = 0; j < MAXX; ++j) {
        for(int k = 0; k < M; ++k) {
          int nj = max(0, min(MAXX - 1, j + C[k]));
          dp[next][nj] = min(dp[next][nj],
                             dp[now][j] + (x[i] - nj) * (x[i] - nj));
        }
      }
    }
    cout << *min_element(dp[N&1], dp[N&1] + MAXX) << endl;
  }
  return 0;
}