#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

const int INF = 1 << 29;

int clamp(int n, int mi, int ma) {
    return max(mi, min(ma, n));
}

int dp[20010][256];
int solve_testcase() {
    int N, M; cin >> N >> M;
    if(N == 0 and M == 0) return 1;

    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];

    const int mi = 0, ma = 255;
    fill(dp[0], dp[N+1], INF);
    dp[0][128] = 0;
    for(int i=0; i<N; i++) {
        for(int j=0; j<256; j++) {
            for(int k=0; k<M; k++) {
                int nj = clamp(j + C[k], mi, ma);
                int er = (nj - x[i]) * (nj - x[i]);
                dp[i+1][nj] = min(dp[i+1][nj], dp[i][j] + er);
            }
        }
    }

    cout << *min_element(dp[N], dp[N] + 256) << endl;
    return 0;
}

int main() {
    while(!solve_testcase());
    return 0;
}

