#include <bits/stdc++.h>
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<int> dp(256, inf);
        dp[128] = 0;
        for(int i = 0; i < n; ++i) {
            vector<int> nxt(256, inf);
            for(int j = 0; j <= 255; ++j) {
                for(int k = 0; k < m; ++k) {
                    int nc = max(0, min(255, j + c[k]));
                    nxt[nc] = min(nxt[nc], dp[j] + (x[i] - nc) * (x[i] - nc));
                }
            }
            dp = move(nxt);
        }

        cout << *min_element(begin(dp), end(dp)) << endl;
    }
}
