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

#define int long long
#define rep(i,a,b) for(int i = a; i < b; i++)
const int mod = 1e9 + 7;
const int inf = 1e16;

signed main() {
    while (true) {
        int N,M;
        cin >> N >> M;
        if (N == 0 and M == 0) break;
        int C[M];
        rep(i,0,M) cin >> C[i];
        int X[N];
        rep(i,0,N) cin >> X[i];
        int dp[N+1][256];
        rep(i,0,N+1)rep(j,0,256) dp[i][j] = inf;
        dp[0][128] = 0;
        rep(i,1,N+1) {
            rep(j,0,256) {
                for(int c: C) {
                    int k = j + c;
                    k = min(k,255LL);
                    k = max(k,0LL);
                    dp[i][k] = min(dp[i][k], dp[i-1][j] + (X[i-1] - k)*(X[i-1] - k));
                }
            }
        }
        int ans = inf;
        rep(j,0,256) ans = min(ans, dp[N][j]);
        cout << ans << endl;
    }
}
