#include <bits/stdc++.h>
using namespace std;
int N, M;
int C[16], x[20000];
int dp[20000][256];
void solve() {
  for (int i = (int)(0); i < (int)(20000); i++)
    for (int j = (int)(0); j < (int)(256); j++) dp[i][j] = INT_MAX / 3;
  dp[0][128] = 0;
  for (int i = (int)(0); i < (int)(N); i++)
    for (int j = (int)(0); j < (int)(256); j++)
      for (int k = (int)(0); k < (int)(M); k++) {
        int xx = j + C[k];
        if (xx < 0) xx = 0;
        if (xx > 255) xx = 255;
        int cost = (x[i] - xx) * (x[i] - xx);
        dp[i + 1][xx] = min(dp[i + 1][xx], dp[i][j] + cost);
      }
  int res = INT_MAX / 3;
  for (int i = (int)(0); i < (int)(256); i++) res = min(res, dp[N][i]);
  printf("%d\n", res);
}
int main() {
  while (1) {
    scanf("%d %d", &N, &M);
    if (N == 0) break;
    for (int i = (int)(0); i < (int)(M); i++) {
      scanf("%d", &C[i]);
    }
    for (int i = (int)(0); i < (int)(N); i++) scanf("%d", &x[i]);
    solve();
  }
  return 0;
}
