#include <bits/stdc++.h>
using namespace std;
const int INF = 1e+8;
const int MAX_M = 16;
const int MAX_N = 20000;
int dp[MAX_N][256];
int main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    int C[MAX_M] = {0};
    for (int i = 0; i < M; i++) {
      cin >> C[i];
    }
    for (int i = 0; i <= N; i++) {
      for (int j = 0; j < 256; j++) {
        dp[i][j] = INF;
      }
    }
    dp[0][128] = 0;
    for (int i = 1; i <= N; i++) {
      int x;
      cin >> x;
      for (int prev = 0; prev < 256; prev++) {
        for (int m = 0; m < M; m++) {
          int next = prev + C[m];
          if (next < 0) next = 0;
          if (next > 255) next = 255;
          int diff = (next - x) * (next - x);
          dp[i][next] = min(dp[i][next], dp[i - 1][prev] + diff);
        }
      }
    }
    int ans = INF;
    for (int i = 0; i < 256; i++) {
      ans = min(ans, dp[N][i]);
    }
    cout << ans << endl;
  }
}
