#include <bits/stdc++.h>
using namespace std;
int N, M, C[16], X, dp[20000 + 1][256];
int main() {
  while (true) {
    for (int i = 0; i < 20000 + 1; i++) {
      for (int j = 0; j < 256; j++) {
        dp[i][j] = (1 << 29);
      }
    }
    dp[0][128] = 0;
    cin >> N >> M;
    if (N == 0 && M == 0) {
      break;
    }
    for (int i = 0; i < M; i++) {
      cin >> C[i];
    }
    for (int i = 1; i <= N; i++) {
      cin >> X;
      for (int j = 0; j < 256; j++) {
        int sabun = (X - j) * (X - j);
        for (int k = 0; k < M; k++) {
          int ptr = j - C[k];
          if (ptr < 0) {
            ptr = 0;
          }
          if (ptr > 255) {
            ptr = 255;
          }
          dp[i][j] = min(dp[i][j], dp[i - 1][ptr] + sabun);
        }
      }
    }
    cout << *min_element(dp[N], dp[N] + 256) << endl;
  }
  return 0;
}
