// ------------------
// author: MatsuTaku
// date  : 27/02/2020
// ------------------

#include <bits/stdc++.h>

using namespace std;
using ll = long long;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);

  constexpr ll INF = 1e18;
  int n,m;
  while (cin>>n>>m and (n!=0 or m!=0)) {
    vector<int> code(m); for(auto&v:code) cin >> v;
    vector<array<ll, 256>> dp(n+1);
    for (int j = 0; j < n+1; j++) for (int i = 0; i < 256; i++)
      dp[j][i] = INF;
    dp[0][128] = 0;
    for (int i = 0; i < n; i++) {
      ll v; cin>>v;
      for (int f = 0; f < 256; f++) {
        if (dp[i][f]==INF) continue;
        for (auto c:code) {
          ll t = max(min(f+c, 255), 0);
          dp[i+1][t] = min(dp[i+1][t], dp[i][f]+(v-t)*(v-t));
        }
      }
    }
    cout << *min_element(dp[n].begin(), dp[n].end()) << endl;
  }

  return 0;
}

