#include <bits/stdc++.h>
int c[20];
int x[50000];
int y[50000][20];
int sum[50000][20];
using namespace std;
int dif(int a, int b) { return (a - b) * (a - b); }
int main(void) {
  int n, m;
  int i1, i2, i3;
  int minn;
  int temp;
  int i;
  while (1) {
    cin >> n >> m;
    if (n + m == 0) break;
    memset(y, 0, sizeof(y));
    fill((int*)sum, (int*)(sum + (sizeof(sum) / sizeof(*(sum)))), INT_MAX);
    for (i1 = 0; i1 < m; i1++) {
      cin >> c[i1];
      y[0][i1] = 128;
      sum[0][i1] = 0;
    }
    for (i1 = 0; i1 < n; i1++) {
      cin >> x[i1];
    }
    for (i1 = 1; i1 <= n; i1++) {
      for (i2 = 0; i2 < m; i2++) {
        minn = INT_MAX;
        for (i3 = 0; i3 < m; i3++) {
          temp = y[i1 - 1][i3] + c[i2];
          if (temp < 0) temp = 0;
          if (255 < temp) temp = 255;
          if (minn >= dif(x[i1 - 1], temp)) {
            minn = dif(x[i1 - 1], temp);
            y[i1][i2] = temp;
            sum[i1][i2] = min(sum[i1][i2], sum[i1 - 1][i3] + minn);
          }
        }
      }
    }
    minn = sum[n][0];
    for (i1 = 0; i1 < m; i1++) {
      if (minn > sum[n][i1]) minn = sum[n][i1];
    }
    cout << minn << endl;
  }
  return 0;
}
