#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mx = 1LL << 62;
ll dp[20002][256];
int main() {
  while (1) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) break;
    vector<int> c;
    vector<int> x;
    for (int i = 0; i < m; ++i) {
      int ci;
      cin >> ci;
      c.push_back(ci);
    }
    for (int i = 0; i < n; ++i) {
      int xi;
      cin >> xi;
      x.push_back(xi);
    }
    int y = 128;
    for (int i = 0; i <= n + 1; ++i) {
      for (int j = 0; j < 256; ++j) {
        dp[i][j] = -1;
      }
    }
    dp[0][128] = 0;
    for (int i = 0; i < n; ++i) {
      int next = x[i];
      for (int j = 0; j < m; ++j) {
        int cj = c[j];
        for (int k = 0; k < 256; ++k) {
          if (dp[i][k] == -1) continue;
          int high = min(255, k + cj);
          dp[i + 1][high] = min((dp[i + 1][high] == -1 ? mx : dp[i + 1][high]),
                                dp[i][k] + (high - next) * (high - next));
        }
      }
    }
    ll mincost = mx;
    for (int i = 0; i < 256; ++i) {
      if (dp[n][i] < mincost && dp[n][i] != -1) mincost = dp[n][i];
    }
    cout << mincost << endl;
  }
}
