#include <iostream>
#include <vector>
#define int long long

using namespace std;

signed main() {
  int n, m;
  while(cin >> n >> m, n != 0) {
    vector<int> cs(m);
    for (int i = 0; i < m; i++) {
      cin >> cs[i];
    }
    vector<int> y(n);
    for (int i = 0; i < n; i++) {
      cin >> y[i];
    }
    const int inf = 10000000000ll;
    vector<vector<int>> dp(n+1, vector<int>(256, inf));
    dp[0][128] = 0;
    for (int i=0; i<n; i++){
      for (int k=0; k < 256; k++) {
        for (auto c : cs) {
          int to = k + c;
          to = (to < 0) ? 0 :
               (to > 255) ? 255 :
               to;
          int cost = (to - y[i]) * (to - y[i]);
          dp[i+1][to] = min(dp[i+1][to], dp[i][k] + cost);
        }
      }
    }
    int res = inf;
    for (int k=0; k < 256; k++) {
      res = min(res, dp[n][k]);
    }
    cout << res << endl;
  }
}
