#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0) break;
    vector<int> c, x;
    for (long long i = 0; i < (long long)(m); i++) {
      int a;
      cin >> a;
      c.push_back(a);
    }
    for (long long i = 0; i < (long long)(n); i++) {
      int a;
      cin >> a;
      x.push_back(a);
    }
    static long long dp[30000][300];
    for (long long i = 0; i < (long long)(30000); i++)
      for (long long j = 0; j < (long long)(300); j++) dp[i][j] = 1LL << 50;
    dp[0][128] = 0;
    for (int i = 0; i < n; i++) {
      for (long long j = 0; j < (long long)(256); j++) {
        for (long long k = 0; k < (long long)(m); k++) {
          int val = j + c[k];
          if (val > 255) val = 255;
          if (val < 0) val = 0;
          int pow2 = (x[i] - val) * (x[i] - val);
          dp[i + 1][val] = min(dp[i + 1][val], dp[i][j] + pow2);
        }
      }
    }
    long long mini = 1LL << 50;
    for (long long i = 0; i < (long long)(256); i++) {
      mini = min(mini, dp[n][i]);
    }
    cout << mini << endl;
  }
}
