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