#include <bits/stdc++.h>
using namespace std;
int n, m;
int c[16], x[20000];
int dp[20][300];
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] = 1000000007;
    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 = 1000000007;
    for (long long i = 0; i < (300); i++) {
      ans = min(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
}
