#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)(n);++i)

int main() {
  for(int N, M; cin >> N >> M, (N || M); ) {
    int C[M]; REP(i, M) scanf("%d", &C[i]);
    int dp[2][256] = {};

    const int inf = 987654321;
    REP(i, 256) dp[0][i] = inf;
    dp[0][128] = 0;
    for(int n = 1; n <= N; n++) {
      int x; scanf("%d", &x);
      int cur = n & 1;
      int prev = 1 - cur;
      REP(i, 256) dp[cur][i] = inf;
      REP(i, 256) {
        REP(m, M) {
          int j = i + C[m];
          if( j < 0 ) j = 0;
          if( j > 255 ) j = 255;
          dp[cur][j] = min(dp[cur][j], dp[prev][i] + (x - j) * (x - j));
        }
      }
    }
    int best = inf;
    REP(i, 256) best = min(best, dp[N & 1][i] );
    cout << best << endl;
  }
  return 0;
}