#include<bits/stdc++.h>
#define rep(i, n) for (int i=0; i<(n); i++)
#define ll long long
using namespace std;

const ll INF=1e18+7;

int f(int x) { return max(0, min(x, 255)); }

int main() {
  int n,m; 

  while (1) {
    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<vector<ll>> dp(n+1, vector<ll>(256, INF));
    dp[0][128]=0;
    rep(i, n) {
      rep(j, 256) {
        if (dp[i][j]==INF) continue;
          rep(k, m) {
          int y=f(j+c[k]);
          dp[i+1][y]=min(dp[i+1][y], dp[i][j]+(ll)(x[i]-y)*(ll)(x[i]-y));
        }
      }
    }
    ll res=INF;
    rep(i, 256) res=min(res, dp[n][i]);
    cout<<res<<endl;
  }
}

