#include <bits/stdc++.h>
using namespace std;
const int MAX = 20001 * 256;
int N, M;
int dp[20001][256];
vector<int> C;
vector<int> x;
int round255(int n) {
  if (n > 255) {
    return 255;
  } else if (n < 0) {
    return 0;
  } else {
    return n;
  }
}
int main() {
  while (cin >> N >> M && N) {
    C.resize(M);
    x.resize(N);
    fill_n((int *)dp, sizeof(dp) / sizeof(int), MAX);
    for (int i = 0; i < (int)(M); ++i) {
      cin >> C[i];
    }
    for (int i = 0; i < (int)(N); ++i) {
      cin >> x[i];
    }
    dp[0][128] = 0;
    for (int i = 0; i < (int)(N); ++i) {
      for (int j = 0; j < (int)(256); ++j) {
        if (dp[i][j] == MAX) {
          continue;
        }
        for (int k = 0; k < (int)(M); ++k) {
          int yn = j + C[k];
          yn = round255(yn);
          int sum2 = (x[i] - yn) * (x[i] - yn);
          dp[i + 1][yn] = min(dp[i + 1][yn], dp[i][j] + sum2);
        }
      }
    }
    cout << *min_element(dp[N], dp[N] + 256) << endl;
  }
  return 0;
}
