#include <bits/stdc++.h>
using namespace std;
#define int long long 
#define REP(i, s, e) for(int i = (int)(s); i < (int)(e); ++i)
#define rep(i, n) REP(i, 0, n)
using ll = int;
const int INF = 1e9;
ll SQ(ll x) {return x*x;}

signed main() {
  int n, m;
  while(cin >> n >> m) {
    if(n == 0 && m == 0) break;
    vector<int> c(m), x(n);
    rep(i, m) cin >> c[i];
    rep(i, n) cin >> x[i];
    vector<int> cur(256, INF);
    cur[128] = 0;
    rep(i, n) {
      vector<int> nxt(256, INF);
      rep(j, m) {
	rep(k, 256) {
	  int tmp = k + c[j];
	  if(tmp < 0) tmp = 0;
	  if(tmp > 255) tmp = 255;
	  nxt[tmp] = min((ll)nxt[tmp], cur[k]+SQ(x[i] - tmp));
	}
      }
      swap(cur, nxt);
    }
    int ans = INF;
    for(auto& x : cur) ans = min(ans, x);
    cout << ans << endl;
  }
  return 0;
}

