#include <bits/stdc++.h>
using namespace std;
int dp[20000][256];
int main() {
  for (int n, m; cin >> n >> m, n;) {
    int c[20000], x[20];
    for (int i = 0; i < n; i++) fill(dp[i], dp[i] + 256, (int)1e9);
    for (int i = 0; i < m; i++) cin >> x[i];
    for (int i = 0; i < n; i++) cin >> c[i];
    for (int i = 0; i < m; i++) dp[0][x[i] + 128] = pow(x[i] + 128 - c[0], 2);
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < 256; j++) {
        if (dp[i - 1][j] != 1e9) {
          for (int k = 0; k < m; k++) {
            int num = j + x[k];
            if (num < 0) num = 0;
            if (num > 255) num = 255;
            dp[i][num] =
                min(dp[i][num], dp[i - 1][j] + (int)pow(c[i] - num, 2));
          }
        }
      }
    }
    int sm = 1e9;
    for (int i = 0; i < 256; i++) sm = min(sm, dp[n - 1][i]);
    cout << sm << endl;
  }
  return 0;
}
