#include <bits/stdc++.h>
using namespace std;
const long long INF = (1 << 30) - 1;
long long n, m, data[16], dp[2][256], in;
int main() {
  while (cin >> n >> m && (n | m)) {
    for (int i = 0; i < m; i++) cin >> data[i];
    fill(dp[0], dp[2], INF);
    dp[0][128] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < 256; j++) dp[(i + 1) % 2][j] = INF;
      cin >> in;
      for (int j = 0; j < 256; j++) {
        if (dp[i][j] == INF) continue;
        for (int k = 0; k < m; k++) {
          long long next = j + data[k];
          if (next < 0) next = 0;
          if (next > 255) next = 255;
          dp[(i + 1) % 2][next] =
              min(dp[(i + 1) % 2][next],
                  abs(in - (next)) * abs(in - (next)) + dp[i % 2][j]);
        }
      }
    }
    long long ans = INF;
    for (int i = 0; i < 256; i++) ans = min(ans, dp[n % 2][i]);
    cout << ans << endl;
  }
}
