#include <bits/stdc++.h>
using namespace std;
typedef struct {
  int minv;
  int prevv;
} Pair;
int main() {
  int n, m;
  Pair pair = {1 << 30, 0};
  while (cin >> n >> m, n + m) {
    vector<int> cn(m);
    vector<int> xn(n);
    for (int i = 0; i < m; i++) cin >> cn[i];
    for (int i = 0; i < n; i++) cin >> xn[i];
    vector<vector<Pair> > dp(n + 1, vector<Pair>(m, pair));
    for (int i = 0; i < m; i++) {
      dp[0][i].prevv = 128;
      dp[0][i].minv = 0;
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        Pair p = dp[i][j];
        for (int k = 0; k < m; k++) {
          int calc = p.prevv + cn[k];
          if (calc < 0) calc = 0;
          if (calc > 255) calc = 255;
          int t = (xn[i] - calc) * (xn[i] - calc) + p.minv;
          if (t < dp[i + 1][k].minv) {
            dp[i + 1][k].minv = t;
            dp[i + 1][k].prevv = calc;
          }
        }
      }
    }
    int _min = 1 << 30;
    for (int i = 0; i < m; i++) {
      _min = min(_min, dp[n][i].minv);
    }
    cout << _min << endl;
  }
  return 0;
}
