#include <bits/stdc++.h>
using namespace std;
struct edge {
  int from, to, cost;
};
int n, m;
int c[20];
int x[20005];
int memo[20005][300];
int dp(int y, int i) {
  if (i == n) return 0;
  if (memo[i][y] != 114514810) return memo[i][y];
  int res = 114514810;
  for (int j = (0); j < (m); j++) {
    int tmp = y;
    tmp += c[j];
    if (tmp > 255) tmp = 255;
    if (tmp < 0) tmp = 0;
    res = min(res, dp(tmp, i + 1) + (tmp - x[i]) * (tmp - x[i]));
  }
  return memo[y][i] = res;
}
int main() {
  while (cin >> n >> m, n) {
    for (int i = (0); i < (20005); i++)
      for (int j = (0); j < (300); j++) memo[i][j] = 114514810;
    for (int i = (0); i < (m); i++) cin >> c[i];
    for (int i = (0); i < (n); i++) cin >> x[i];
    cout << dp(128, 0) << endl;
  }
  return 0;
}
