#include <bits/stdc++.h>
using namespace std;
int main() {
  while (true) {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0) {
      break;
    }
    vector<int> C, x;
    int temp;
    for (int(i) = (0); i < (M); (i)++) {
      cin >> temp;
      C.push_back(temp);
    }
    for (int(i) = (0); i < (N); (i)++) {
      cin >> temp;
      x.push_back(temp);
    }
    vector<vector<int>> dp(N + 1, vector<int>(256, 10000000));
    dp[0][128] = 0;
    for (int(i) = (0); i < (N); (i)++) {
      for (int(j) = (0); j < (256); (j)++) {
        if (dp[i][j] == 10000000) {
          continue;
        }
        for (int(k) = (0); k < (M); (k)++) {
          int yn = j + C[k];
          yn = min(255, yn);
          yn = max(0, yn);
          int sum = (x[i] - yn) * (x[i] - yn);
          dp[i + 1][yn] = min(dp[i + 1][yn], dp[i][j] + sum);
        }
      }
    }
    int ans = dp[N][0];
    for (int(i) = (0); i < (256); (i)++) {
      ans = min(ans, dp[N][i]);
    }
    cout << ans << endl;
  }
  return 0;
}
