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

constexpr int INF = (1<<30)-1;

bool solve() {
    int N, M;
    int C[16];

    cin >> N >> M;
    if (N == 0) return (false);
    for (int i = 0; i < M; i++) cin >> C[i];

    vector<vector<int>> dp(20005, vector<int>(256, INF));
    dp[0][128] = 0;
    for (int i = 0; i < N; i++) {
        int v;
        cin >> v;
        for (int j = 0; j < 256; j++) {
            for (int k = 0; k < M; k++) {
                int nn = max(0, min(255, j + C[k]));
                int &next = dp[i + 1][nn];
                next = min(next, dp[i][j] + (nn-v)*(nn-v));
            }
        }
    }
    cout << *min_element(begin(dp[N]), end(dp[N])) << endl;
    return (true);
}
int main() {
    while (solve());
    return (0);
}
