#include <bits/stdc++.h>
using namespace std;
int table[20001][20][2];
int main() {
  int n, m, c[20], p[20001];
  while (cin >> n >> m, n) {
    for (int i = 0; i < m; i++) cin >> c[i];
    for (int i = 0; i < n; i++) cin >> p[i];
    for (int i = 0; i < m; i++) {
      int y = 128 + c[i];
      if (y < 0)
        y = 0;
      else if (y > 255)
        y = 255;
      table[0][i][0] = y;
      table[0][i][1] = pow(p[0] - table[0][i][0], 2);
    }
    for (int i = 1; i < n; i++) {
      for (int j = 0; j < m; j++) {
        int y = table[i - 1][j][0] + c[j];
        if (y < 0)
          y = 0;
        else if (y > 255)
          y = 255;
        int x = table[i - 1][j][1] + pow(p[i] - y, 2);
        int y2 = y;
        for (int k = 0; k < m; k++) {
          if (j == k) continue;
          y = table[i - 1][k][0] + c[j];
          if (y < 0)
            y = 0;
          else if (y > 255)
            y = 255;
          if (x > table[i - 1][k][1] + pow(p[i] - y, 2)) {
            y2 = y;
            x = table[i - 1][k][1] + pow(p[i] - y2, 2);
          }
        }
        table[i][j][0] = y2;
        table[i][j][1] = x;
      }
    }
    int ans = table[n - 1][0][1];
    for (int i = 1; i < m; i++) {
      if (ans > table[n - 1][i][1]) ans = table[n - 1][i][1];
    }
    cout << ans << endl;
  }
}
