#include <bits/stdc++.h>
using namespace std;
int main() {
  int C[16];
  int n, m;
  while (cin >> n >> m) {
    for (int i = 0; i < m; i++) cin >> C[i];
    unsigned int dp[2][256];
    for (int i = 0; i < 2; i++)
      for (int j = 0; j < 256; j++) dp[i][j] = INT_MAX;
    dp[0][128] = 0;
    for (int j = 1; j <= n; j++) {
      int x;
      cin >> x;
      for (int i = 0; i < 256; i++) dp[j & 1][i] = INT_MAX;
      for (int i = 0; i < 256; i++) {
        for (int k = 0; k < m; k++) {
          int y = i + C[k];
          y = min(y, 255);
          y = max(0, y);
          dp[j & 1][y] =
              min(dp[j & 1][y], dp[j & 1 ^ 1][i] + (y - x) * (y - x));
        }
      }
    }
    cout << *min_element(dp[n & 1], dp[n & 1] + 256) << endl;
  }
}
