#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

inline int in() { int x; std::scanf("%d", &x); return x; }
inline void pr(int x, char c=10){ std::printf("%d%c", x, c); }

using ll = long long;

using namespace std;

int C[20];
int X[20200];
ll dp[20200][256];
int N, M;

ll inf = 1e18;

signed main() {
    while (N = in(), M = in()) {
        rep(i, M) C[i] = in();
        rep(i, N) X[i] = in();
        rep(i, N + 1) rep(j, 256) dp[i][j] = inf;
        dp[0][128] = 0;
        rep(i, N) rep(j, 256) {
            if (dp[i][j] == inf) continue;
            rep(k, M) {
                const int nxt = max(min(255, j + C[k]), 0);
                ll& d = dp[i + 1][nxt];
                int c = (X[i] - nxt);
                if (dp[i][j] + c * c < d) {
                    d = dp[i][j] + c * c;
                }
            }
        }
        pr(*min_element(dp[N], dp[N] + 256));
    }

    return 0;
}