#include <bits/stdc++.h>
int main(void) {
  int i, j, k;
  int n, m;
  int x[20001], c[16];
  int **dp;
  int tmp1, tmp2;
  int min;
  dp = (int **)malloc(sizeof(int) * (20001));
  for (i = 0; i <= n; i++) {
    dp[i] = (int *)malloc(sizeof(int) * 256);
  }
  while (1) {
    scanf("%d %d ", &n, &m);
    if (n == 0 && m == 0) break;
    for (i = 0; i < m; i++) {
      scanf("%d ", &c[i]);
    }
    for (i = 1; i <= n; i++) {
      for (j = 0; j < 256; j++) {
        dp[i][j] = -1;
      }
    }
    for (i = 1; i <= n; i++) {
      scanf("%d ", &x[i]);
      if (i == 1) {
        for (k = 0; k < m; k++) {
          tmp1 = 128 + c[k];
          if (tmp1 < 0) tmp1 = 0;
          if (tmp1 > 255) tmp1 = 255;
          tmp2 = (tmp1 - x[i]) * (tmp1 - x[i]);
          dp[i][tmp1] = tmp2;
        }
      } else {
        for (j = 0; j < 256; j++) {
          if (dp[i - 1][j] >= 0) {
            for (k = 0; k < m; k++) {
              tmp1 = j + c[k];
              if (tmp1 < 0) tmp1 = 0;
              if (tmp1 > 255) tmp1 = 255;
              tmp2 = (tmp1 - x[i]) * (tmp1 - x[i]);
              if (dp[i][tmp1] < 0) dp[i][tmp1] = dp[i - 1][j] + tmp2;
              if (dp[i][tmp1] > dp[i - 1][j] + tmp2)
                dp[i][tmp1] = dp[i - 1][j] + tmp2;
            }
          }
        }
      }
    }
    min = 2000000000;
    for (i = 0; i < 256; i++) {
      if (min > dp[n][i] && dp[n][i] >= 0) min = dp[n][i];
    }
    printf("%d\n", min);
  }
  return 0;
}
