#include <bits/stdc++.h>

using namespace std;

int N, M;
int C[17], x[20001];
int dp[256][20001];

int rec(int now, int cnt) {
  if ( cnt == N ) return 0;
  if ( dp[now][cnt] != -1 ) return dp[now][cnt];

  int ret = 1e8;
  for ( int i = 0; i < M; i++ ) {
    int tmp = max(0, min(255, now + C[i]));
    ret = min(ret, rec(tmp, cnt + 1) + (x[cnt] - tmp) * (x[cnt] - tmp));
  }

  return dp[now][cnt] = ret;
}

int main()
{
  while ( cin >> N >> M, N | M ) {
    for ( int i = 0; i < M; i++ ) cin >> C[i];
    for ( int i = 0; i < N; i++ ) cin >> x[i];
    
    fill_n(*dp, 256 * 20001, -1);
    
    cout << rec(128, 0) << endl;
  }
}