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

const long long INF = 1e10;

int main() {
    int N, M;
    while (cin >> N >> M, N) {
        vector<int> C(M);
        for (int i=0; i<M; ++i) cin >> C[i];
        vector<int> x(N);
        for (int i=0; i<N; ++i) cin >> x[i];

        vector<long long> cost(256, INF);
        cost[128] = 0;
        for (int i=0; i<N; ++i) {
            vector<long long> cost2(256, INF);
            for (int cd: C) {
                for (int k=0; k<256; ++k) {
                    int kk = max(min(k+cd, 255), 0);
                    cost2[kk] = min(cost2[kk], cost[k] + (kk - x[i]) * (kk - x[i]));
                }
            }
            cost = cost2;
        }

        long long ans = *min_element(cost.begin(), cost.end());
        cout << ans << endl;
    }

    return 0;
}
