#include <bits/stdc++.h>
using namespace std;
int N, M, c[16], dp[256];
int main() {
  while (cin >> N >> M && N) {
    fill(dp, dp + 256, 0);
    for (int i = 0; i < M; i++) cin >> c[i];
    for (int i = 0; i < N; i++) {
      int m, dp2[256];
      fill(dp2, dp2 + 256, 2e9);
      cin >> m;
      for (int j = 0; j < 256; j++)
        for (int k = 0; k < M; k++) {
          int n = j + c[k];
          if (n < 0) n = 0;
          if (n > 255) n = 255;
          dp2[n] = min(dp2[n], dp[j] + (m - n) * (m - n));
        }
      for (int j = 0; j < 256; j++) dp[j] = dp2[j];
    }
    int ans = 2e9;
    for (int i = 0; i < 256; i++) ans = min(ans, dp[i]);
    cout << ans << endl;
  }
}
