#include <bits/stdc++.h>
using namespace std;
inline int normalize(int y) { return min(255, max(0, y)); }
inline int get_error(int y, int y0) {
  y -= y0;
  return y * y;
}
int solve(int n, int m, int c[], int x[]) {
  int min_error = -1;
  int errors[n + 1][256];
  for (int t = 0; t <= n; ++t) {
    for (int y = 0; y < 256; ++y) {
      errors[t][y] = -1;
    }
  }
  errors[0][128] = 0;
  for (int t = 0; t < n; ++t) {
    for (int y = 0; y < 256; ++y) {
      if (errors[t][y] < 0) {
        continue;
      }
      for (int i = 0; i < m; ++i) {
        int yn = normalize(y + c[i]);
        int e = errors[t][y] + get_error(yn, x[t]);
        if (errors[t + 1][yn] < 0 || e < errors[t + 1][yn]) {
          errors[t + 1][yn] = e;
        }
      }
    }
  }
  for (int y = 0; y < 256; ++y) {
    if (errors[n][y] < 0) {
      continue;
    }
    if (min_error < 0 || errors[n][y] < min_error) {
      min_error = errors[n][y];
    }
  }
  return min_error;
}
int main() {
  int n, m;
  int c[16], x[20000];
  while (true) {
    cin >> n >> m;
    if (n == 0) {
      return 0;
    }
    for (int i = 0; i < m; ++i) {
      cin >> c[i];
    }
    for (int i = 0; i < n; ++i) {
      cin >> x[i];
    }
    cout << solve(n, m, c, x) << endl;
  }
}
