#include <bits/stdc++.h>
using namespace std;
vector<int> c, x;
const int MA = 99999999;
const int MAX_X = 300;
int dp[20000][MAX_X];
int main() {
  int n, m, f, ans;
  int te = 0;
  while (cin >> n >> m && n) {
    te++;
    for (int i = 0; i < 20000; i++)
      for (int j = 0; j < MAX_X; j++) dp[i][j] = MA;
    c.clear();
    x.clear();
    ans = MA;
    for (int i = 0; i < m; i++) {
      cin >> f;
      c.push_back(f);
    }
    for (int i = 0; i < n; i++) {
      cin >> f;
      x.push_back(f);
    }
    dp[0][128] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 256; j++) {
        if (dp[i][j] == MA) continue;
        for (int k = 0; k < m; k++) {
          int nex = j + c[k];
          if (nex < 0) nex = 0;
          if (nex > 255) nex = 255;
          dp[i + 1][nex] =
              min(dp[i + 1][nex], dp[i][j] + (x[i] - nex) * (x[i] - nex));
        }
      }
    }
    for (int j = 0; j < 256; j++) ans = min(ans, dp[n][j]);
    cout << ans << endl;
  }
}
