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

constexpr int INF = 1e9;

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];
        }
        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) {
                for(int k=0; k<M; ++k) {
                    int next = min(255, max(0, j+C[k]));
                    dp[i+1][next] = min(dp[i+1][next], dp[i][j] + (x[i] - next) * (x[i] - next));
                }
            }
        }
        cout << *min_element(dp[N].begin(), dp[N].end()) << endl;
    }
}
