#include <bits/stdc++.h>
using namespace std;

using ll = long long;
#define REP(i,n) for(int i=0; i < (n); ++i)
#define REPR(i,n) for(int i=(n); i >= 0; --i)
#define FOR(i, m, n) for(int i = (m); i < (n); ++i)
// #define INF 1e9
const ll INF = 1LL << 60;
ll square(ll x){
  return x * x;
}
ll dp[20005][300];

int main(){
  while(true){
    int N, M;
    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];
    }
    REP(i, 20005){
      REP(j, 258){
        dp[i][j] = INF;
      }
    }
    dp[0][128] = 0;
    REP(i, N){
      REP(j, 256){
        if(dp[i][j] == INF) continue;
        REP(k, M){
          int nx = max(0, min(255, j+C[k]));
          dp[i+1][nx] = min(dp[i+1][nx], dp[i][j] + square(x[i]-nx));
        }
      }
    }
    ll ans = INF;
    REP(i, 256){
      ans = min(ans, dp[N][i]);
    }
    cout << ans << endl;
  }
  return 0;
}

