#include <iostream>
#include <algorithm>

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

using namespace std;

const int INF = 0x3fffffff;

int C[20], dp[20005][300];
int main(){
	int N, M;
	while(cin >> N >> M, N|M){
		rep(i,M) cin >> C[i];
		fill( (int*)dp[0], (int*)dp[20005], INF );
		dp[0][128] = 0;
		rep(i,N){
			int x;
			cin >> x;
			rep(j,256) rep(k,M){
				int nx = j + C[k];
				nx = max( nx, 0 );
				nx = min( nx, 255 );
				int df = abs( nx - x );
				dp[i+1][nx] = min( dp[i+1][nx], dp[i][j] + (df * df) );
			}
		}
		int ans = INF;
		rep(i,256){
			ans = min( ans, dp[N][i] );
		}
		cout << ans << endl;
	}
	return 0;
}