#include <bits/stdc++.h>
using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;
using vs = vector<string>;
using msi = map<string, int>;
using mii = map<int, int>;
using pii = pair<int, int>;
using vlai = valarray<int>;
using ll = long long;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vi code(m);
    for (int i = 0; i < m; i++) cin >> code[i];
    vector<vector<pii>> dp(n + 1, vector<pii>(m, make_pair(1e9, 0)));
    fill(dp[0].begin(), dp[0].end(), make_pair(0, 128));
    for (int i = 1; i < n + 1; i++) {
      int num;
      cin >> num;
      for (int j = 0; j < m; j++) {
        for (int k = 0; k < m; k++) {
          int next = min(max(dp[i - 1][k].second + code[j], 0), 255);
          int diff = num - next;
          diff *= diff;
          if (dp[i][j].first > dp[i - 1][k].first + diff) {
            dp[i][j] = make_pair(dp[i - 1][k].first + diff, next);
          }
        }
      }
    }
    int ans = 1e9;
    for (int i = 0; i < m; i++) ans = min(ans, dp[n][i].first);
    cout << ans << endl;
  }
  return 0;
}
