#include <bits/stdc++.h>
using namespace std;
bool cheak(int x, int y, int xMax, int yMax) {
  return x >= 0 && y >= 0 && xMax > x && yMax > y;
}
const int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
const int INF = 2147483647;
const int MAX = 20020;
int dp[MAX][256];
int main() {
  int n, m;
  while (cin >> n >> m && n) {
    int c[20], x[MAX];
    for (int i = (0); i < (m); ++i) cin >> c[i];
    for (int i = (0); i < (n); ++i) cin >> x[i];
    fill((int*)dp, (int*)(dp + ((sizeof(dp)) / (sizeof(*(dp))))), INF);
    dp[0][128] = 0;
    for (int i = (0); i < (n); ++i)
      for (int j = (0); j < (256); ++j)
        if (dp[i][j] != INF)
          for (int k = (0); k < (m); ++k) {
            int tmp = j + c[k];
            if (tmp < 0) tmp = 0;
            if (tmp >= 256) tmp = 255;
            int val = pow(x[i] - tmp, 2);
            if (val < dp[i + 1][tmp]) dp[i + 1][tmp] = val + dp[i][j];
          }
    int ans = INF;
    for (int j = (0); j < (256); ++j) ans = min(dp[n][j], ans);
    cout << ans << endl;
  }
  return 0;
}
