#include "bits/stdc++.h"

#define REP(i, n ,N) for(ll i = (n); i < (N); i++)
#define p(s) cout<<(s)<<endl
#define p2(a, b) cout<<(a)<<" "<<(b)<<endl
using namespace std;
typedef long long ll;
ll MOD = 1e9+7;
ll inf = 1e18;


ll N, M;
ll dp[20010][256];
int main() {
	while (cin >> N >> M && N > 0) {
		ll C[20];
		ll x[20010];
		REP(i,0,20010){
			REP(j,0,256){
				dp[i][j] = inf;
			}
		}
		dp[0][128] = 0;
		REP(i,0,M) cin >> C[i];
		REP(j,0,N) cin >> x[j];
		REP(i,0,N){
			REP(y,0,256){
				REP(k,0,M){
					ll next_y = max(min(y + C[k], 255LL), 0LL);
					dp[i+1][next_y] = min(dp[i+1][next_y], dp[i][y] + (x[i] - next_y)*(x[i] - next_y));
				}
			}
		}
		ll ans = inf;
		REP(y,0,256){
			ans = min(ans, dp[N][y]);
		}
		p(ans);
	}

	return 0;
}
