#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::fixed;
using std::list;
using std::make_pair;
using std::map;
using std::max;
using std::min;
using std::pair;
using std::priority_queue;
using std::queue;
using std::set;
using std::setprecision;
using std::stack;
using std::string;
using std::vector;
template <typename T>
using Vec = vector<T>;
int main(void) {
  while (1) {
    int n;
    int m;
    cin >> n >> m;
    if (n == 0 && m == 0) {
      break;
    }
    Vec<int> c(m);
    Vec<int> x(n + 1);
    for (int i = 0; i < m; i++) {
      cin >> c[i];
    }
    for (int j = 1; j <= n; j++) {
      cin >> x[j];
    }
    Vec<Vec<int> > dp(m, Vec<int>(n + 1));
    for (int j = 0; j <= n; j++) {
      for (int i = 0; i < m; i++) {
        dp[i][j] = 0;
      }
    }
    Vec<int> now(m, 128);
    Vec<int> upd(m);
    for (int j = 1; j <= n; j++) {
      for (int i = 0; i < m; i++) {
        for (int k = 0; k < m; k++) {
          int div = now[k] + c[i];
          if (div > 255) {
            div = 255;
          } else if (div < 0) {
            div = 0;
          }
          div = x[j] - div;
          if (k == 0 || dp[i][j] > dp[k][j - 1] + div * div) {
            dp[i][j] = dp[k][j - 1] + div * div;
            upd[i] = -div + x[j];
          }
        }
      }
      for (int i = 0; i < m; i++) {
        now[i] = upd[i];
      }
    }
    int ans;
    for (int i = 0; i < m; i++) {
      if (i == 0 || ans > dp[i][n]) {
        ans = dp[i][n];
      }
    }
    cout << ans << endl;
  }
  return 0;
}
