#include <bits/stdc++.h>
using namespace std;
const int vx[4] = {0, 1, 0, -1};
const int vy[4] = {1, 0, -1, 0};
int dp[21000][300];
void f(int n, int m) {
  for (int i = 1; i < n + 1; i++) {
    for (int j = 0; j < 300; j++) {
      dp[i][j] = 1e9;
    }
  }
  int c[1000];
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }
  for (int i = 1; i < n + 1; i++) {
    int x;
    cin >> x;
    for (int j = 0; j < 256; j++) {
      for (int k = 0; k < m; k++) {
        int a = j + c[k];
        a = min(255, a);
        a = max(0, a);
        int d = (a - x) * (a - x);
        dp[i][a] = min(dp[i][a], dp[i - 1][j] + d);
      }
    }
  }
  int ans = 1e9;
  for (int i = 0; i < 300; i++) {
    ans = min(ans, dp[n][i]);
  }
  cout << ans << endl;
}
int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) break;
    f(n, m);
  }
  return 0;
}
