#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int INF = 1e9;

int main() {
    int n, m;
    while (cin >> n >> m, n | m) {
        vector<int> c(m, 0), x(n, 0);
        for (int i = 0; i < m; i++) cin >> c[i];
        for (int i = 0; i < n; i++) cin >> x[i];
        // dp[i][j] := i個目の信号まででyがjのときの最小値
        vector<vector<int> > dp(20005, vector<int>(256, INF));
        dp[0][128] = 0;
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 256; j++) {
                if (dp[i][j] == INF) continue;
                for (int k = 0; k < m; k++) {
                    int y = max(0, min(255, j + c[k]));
                    dp[i + 1][y] = min(dp[i + 1][y], dp[i][j] + (x[i] - y) * (x[i] - y));
                }
            }
        }
        int ans = 1e9;
        for (int i = 0; i < 256; i++) ans = min(ans, dp[n][i]);
        cout << ans << endl;
    }
    return 0;
}

