#include <bits/stdc++.h>
using namespace std;
long long dp[20010][256];
int book[20];
int input[20010];
int N, M;
static const long long MAX = 10000000000;
void init() { fill(&dp[0][0], &dp[20009][256], MAX); }
void solve() {
  dp[0][128] = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 256; j++) {
      for (int k = 0; k < M; k++) {
        int tar = j + book[k];
        if (tar < 0) {
          tar = 0;
        } else if (tar > 255) {
          tar = 255;
        }
        long long sub = tar - input[i];
        sub *= sub;
        dp[i + 1][tar] = min(dp[i + 1][tar], dp[i][j] + sub);
      }
    }
  }
  long long mn = MAX;
  for (int i = 0; i < 256; i++) {
    mn = min(mn, dp[N][i]);
  }
  printf("%ld\n", mn);
}
int main() {
  while (true) {
    scanf("%d %d ", &N, &M);
    if (N == 0 && M == 0) {
      break;
    }
    init();
    for (int i = 0; i < M; i++) {
      scanf("%d ", &book[i]);
    }
    for (int i = 0; i < N; i++) {
      scanf("%d ", &input[i]);
    }
    solve();
  }
}
