#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for (int (i)=(0);(i)<(int)(n);++(i))
using ll = long long;

int n, m;
int c[16], x[20001];

ll dp[20001][256];

ll rec(int p, int y) {
    if (dp[p][y] >= 0) return dp[p][y];

    if (p == n) return 0;

    ll ret=1e10;
    for (int i=0; i<m; ++i) {
        int ny=y+c[i];
        if (ny<0) ny=0;
        else if (ny>255) ny=255;
        ret = min(ret, rec(p+1, ny)+(ny-x[p])*(ny-x[p]));
    }

    return dp[p][y] = ret;
}

int main() {
    while (cin >> n >> m and (n or m)) {
        rep(i,m) cin >> c[i];
        rep(i,n) cin >> x[i];
        fill(dp[0], dp[20001], -1);
        cout << rec(0, 128) << endl;
    }
}