#include <bits/stdc++.h>
using namespace std;
long long dp[20001][256];
signed main() {
  long long n, m, c[16];
  while (cin >> n >> m, n) {
    for (long long i = 0; i <= n; i++) {
      for (long long j = 0; j < 256; j++)
        if (i)
          dp[i][j] = 1e+18;
        else
          dp[i][j] = 0;
    }
    for (long long i = 0; i < m; i++) cin >> c[i];
    for (long long i = 0; i < n; i++) {
      long long x;
      cin >> x;
      for (long long j = 0; j < 256; j++) {
        for (long long k = 0; k < m; k++) {
          long long d = j + c[k];
          if (d < 0)
            d = 0;
          else if (d > 255)
            d = 255;
          dp[i + 1][d] = min(dp[i + 1][d], dp[i][j] + (x - d) * (x - d));
        }
      }
    }
    long long mi = 1e+18;
    for (long long i = 0; i < 256; i++) mi = min(mi, dp[n][i]);
    cout << mi << endl;
  }
  return 0;
}
