#include <bits/stdc++.h>
using namespace std;
int i, j, k, n, m, c[17], N[20001], dp[256][20001], x;
int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0) break;
    for (i = 0; i < m; i++) cin >> c[i];
    for (i = 0; i < n; i++) cin >> N[i];
    for (i = 0; i < 256; i++) {
      for (j = 0; j <= n; j++) {
        dp[i][j] = 100000000;
        ;
      }
    }
    dp[128][0] = 0;
    for (j = 0; j < n; j++) {
      for (i = 0; i <= 255; i++) {
        for (k = 0; k < m; k++) {
          x = (i + c[k]);
          if (x < 0) x = 0;
          if (x > 255) x = 255;
          dp[x][j + 1] = min(dp[x][j + 1], dp[i][j] + (x - N[j]) * (x - N[j]));
        }
      }
    }
    int Min = 1000000;
    for (i = 0; i < 256; i++) {
      if (Min > dp[i][n]) {
        Min = dp[i][n];
      }
    }
    cout << Min << endl;
  }
  return 0;
}
