#include <bits/stdc++.h>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T>
inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T>
inline T sqr(T x) {
  return x * x;
}
const double eps = 1e-10;
const double pi = acos(-1.0);
const long long inf = 1LL << 62;
int main(void) {
  pair<int, int> dp[2][16];
  int tar[20000];
  int d[16];
  int n, m;
  pair<int, int> INF;
  INF.first = 1 << 29;
  INF.second = 1 << 29;
  while (cin >> n >> m, n | m) {
    for (int i = (0); i < (m); ++i) cin >> d[i];
    for (int i = (0); i < (n); ++i) cin >> tar[i];
    pair<int, int> init;
    init.first = 128;
    init.second = 0;
    for (int i = (0); i < (m); ++i) {
      dp[0][i] = init;
      dp[1][i] = INF;
    }
    for (int i = (0); i < (n); ++i) {
      for (int j = (0); j < (m); ++j)
        for (int k = (0); k < (m); ++k) {
          pair<int, int> tmp = dp[0][j];
          tmp.first += d[k];
          tmp.first = max(tmp.first, 0);
          tmp.first = min(tmp.first, 255);
          tmp.second += (tmp.first - tar[i]) * (tmp.first - tar[i]);
          if (tmp.second < dp[1][k].second)
            dp[1][k] = tmp;
          else if (tmp.second == dp[1][k].second &&
                   abs(tmp.first - tar[i + 1]) <
                       abs(dp[1][k].first - tar[i + 1]))
            dp[1][k] = tmp;
        }
      for (int j = (0); j < (m); ++j) {
        dp[0][j] = dp[1][j];
        dp[1][j] = INF;
      }
    }
    int ans = 1 << 29;
    for (int i = (0); i < (m); ++i) ans = min(ans, dp[0][i].second);
    cout << ans << endl;
  }
  return 0;
}
