#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 29;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    int c[m];
    for (int i = 0; i < (int)m; ++i) cin >> c[i];
    int x[m];
    for (int i = 0; i < (int)n; ++i) cin >> x[i];
    int dp[256];
    for (int i = 0; i < (int)256; ++i) dp[i] = INF;
    dp[128] = 0;
    for (int i = 0; i < (int)n; ++i) {
      int next[256];
      for (int j = 0; j < (int)256; ++j) next[j] = INF;
      for (int j = 0; j < (int)256; ++j) {
        for (int k = 0; k < (int)m; ++k) {
          int hoge = max(min(j + c[k], 255), 0);
          next[hoge] = min(next[hoge], dp[j] + (hoge - x[i]) * (hoge - x[i]));
        }
      }
      memcpy(dp, next, sizeof(next));
    }
    cout << *min_element(dp, dp + 256) << endl;
  }
}
