#include <bits/stdc++.h>
using namespace std;
int n, m;
int c[16 + 1], x[20000 + 1];
int dp[1][256 + 1];
int main() {
  while (cin >> n >> m && n + m) {
    for (long long i = 0; i < (20); i++) {
      for (long long j = 0; j < (300); j++) {
        dp[i][j] = 10000007;
      }
    }
    for (long long i = 0; i < (m); i++) {
      cin >> c[i];
    }
    for (long long i = 0; i < (n); i++) {
      cin >> x[i];
    }
    dp[0][128] = 0;
    for (long long i = 0; i < (n); i++) {
      for (long long j = 0; j < (m); j++) {
        for (long long k = 0; k < (256); k++) {
          int num = k + c[j];
          if (num < 0) num = 0;
          if (num > 255) num = 255;
          dp[i + 1][num] =
              min(dp[i][k] + (x[i] - num) * (x[i] - num), dp[i + 1][num]);
        }
      }
    }
    int ans = 10000007;
    for (long long i = 0; i < (300); i++) {
      ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
}
