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

int main() {
    int N, M;
    while (cin >> N >> M, N) {
        vector<int> C(M), x(N);
        for (int i = 0; i < M; ++i) cin >> C[i];
        for (int i = 0; i < N; ++i) cin >> x[i];
        const int INF = 1 << 30;
        vector<vector<int>> dp(N + 1, 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) {
            for (int k = 0; k < M; ++k) {
                int nj = max(0, min(255, j + C[k]));
                dp[i + 1][nj] = min(dp[i + 1][nj], dp[i][j] + (nj - x[i]) * (nj - x[i]));
            }
        }
        int ans = INF;
        for (int i = 0; i < 256; ++i) {
            ans = min(ans, dp[N][i]);
        }
        cout << ans << endl;
    }
}
