#include <bits/stdc++.h>
using namespace std;
int n, m, c[16], x[20000], table[20000][256];
const int INF = 2147483647;
int min(int a, int b) { return a < b ? a : b; }
int main() {
  cin >> n >> m;
  while (n != 0 || m != 0) {
    int i;
    for (i = 0; i < m; i++) {
      cin >> c[i];
    }
    for (i = 0; i < n; i++) {
      cin >> x[i];
    }
    for (i = 0; i <= n; i++) {
      for (int j = 0; j < 256; j++) {
        table[i][j] = INF;
      }
    }
    table[0][128] = 0;
    for (i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        for (int k = 0; k <= 255; k++) {
          if (table[i][k] < INF) {
            int t = k + c[j];
            if (t < 0) t = 0;
            if (t > 255) t = 255;
            int s = (t - x[i]) * (t - x[i]);
            table[i + 1][t] = min(table[i][k] + s, table[i + 1][t]);
          }
        }
      }
    }
    int ans = INF;
    for (int i = 0; i <= 255; i++) {
      if (table[n][i] >= INF) continue;
      if (ans < 0) ans = table[n][i];
      ans = min(table[n][i], ans);
    }
    cout << ans << endl;
    cin >> n >> m;
  }
  return 0;
}
