#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const ll MOD = 1000000007;
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(2e4 + 5);
#define REP(i, n) for (int i = 0; i < n; i++)

int n, m, c[16], x[MAX_N], dp[MAX_N][256];  // iまでみて信号をjにしたときの最小

int main() {
    while(cin >> n >> m, n){
        REP(i,m) cin >> c[i];
        REP(i,n) cin >> x[i];

        REP(i,n+1)fill(dp[i],dp[i]+256,IINF/2);
        dp[0][128] = 0;

        REP(i,n){
            REP(j,256){
                REP(k,m){
                    int y = max(0,min(255,j+c[k]));
                    dp[i+1][y] = min(dp[i+1][y], dp[i][j]+int(pow(x[i]-y,2)));
                }
            }
        }
        int ans = IINF;
        REP(i,256) ans = min(ans, dp[n][i]);
        cout << ans << endl;
    }
    return 0;
}

