#include <bits/stdc++.h>
using namespace std;
long long int dp[20000][255 + 1];
int c[16], x[20000];
int n, m;
long long int solve(int back, int depth, int k) {
  if (depth == n) return 0;
  int t = back + c[k];
  if (t < 0)
    t = 0;
  else if (t > 255)
    t = 255;
  if (dp[depth][t] < LONG_MAX) return dp[depth][t];
  long long int thismin = LONG_MAX;
  for (int i = 0; i < m; i++) {
    thismin = min(thismin, solve(t, depth + 1, i));
  }
  int a = x[depth] - t;
  int b = thismin + a * a;
  dp[depth][t] = b;
  return dp[depth][t];
}
void init() {
  for (int i = 0; i < 16; i++)
    for (int j = 0; j < 255 + 1; j++) dp[i][j] = LONG_MAX;
}
int main() {
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0) return 0;
    for (int i = 0; i < m; i++) cin >> c[i];
    for (int i = 0; i < n; i++) cin >> x[i];
    init();
    long long int thismin = LONG_MAX;
    for (int i = 0; i < m; i++) {
      thismin = min(thismin, solve(128, 0, i));
    }
    cout << thismin << endl;
  }
}
