#include <bits/stdc++.h>
using namespace std;
long long int ans;
int min(int a, int b) {
  if (a >= b)
    return b;
  else
    return a;
}
int main() {
  int n, m;
  long long int dp[20][256];
  int next;
  while (cin >> n >> m) {
    ans = 1000000000000;
    if (n == 0 && m == 0) {
      break;
    }
    int *c;
    int *x;
    c = new int[m];
    x = new int[n];
    for (int i = 0; i < m; i++) {
      cin >> c[i];
    }
    for (int i = 0; i < n; i++) {
      cin >> x[i];
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 256; j++) {
        dp[i][j] = 10000000000;
      }
    }
    for (int i = 0; i < m; i++) {
      next = 128 + c[i];
      if (next >= 255) next = 255;
      if (next <= 0) next = 0;
      dp[0][next] = (x[0] - next) * (x[0] - next);
    }
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < 256; j++) {
        for (int k = 0; k < m; k++) {
          next = j + c[k];
          if (next >= 255) next = 255;
          if (next <= 0) next = 0;
          dp[i][next] =
              min(dp[i][next], dp[i - 1][j] + (x[i] - next) * (x[i] - next));
        }
      }
    }
    for (int i = 0; i < 256; i++) {
      ans = min(ans, dp[n - 1][i]);
    }
    cout << ans << endl;
  }
  return 0;
}
