#include <bits/stdc++.h>
using namespace std;
int n, m;
long long ans = LLONG_MAX;
long long c[16], x[200001];
void dfs(int index, long long score, long long prev) {
  if (index == n) {
    ans = min(ans, score);
    return;
  }
  for (int i = 0; i < m; i++) {
    long long now = min(255LL, max(0LL, prev + c[i]));
    long long diff = x[index] - now;
    dfs(index + 1, score + diff * diff, now);
  }
}
int main() {
  while (scanf("%d%d", &n, &m) && n) {
    for (int i = 0; i < m; i++) scanf("%lld", &c[i]);
    for (int i = 0; i < n; i++) scanf("%lld", &x[i]);
    dfs(0, 0LL, 128LL);
    printf("%lld\n", ans);
  }
}
