#include<iostream>

using namespace std;

const int inf=1<<28;

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

int n,m;
int c[16];
int x[20010];
int dp[20010][256];


int main(void){
	
	while(cin >> n >> m,n|m){
		rep(i,n+1)rep(j,256) dp[i][j]=inf;
		rep(i,m) cin >> c[i];
		rep(i,n) cin >> x[i];
		dp[0][128]=0;			
		rep(i,n)rep(j,256)rep(k,m){
			int next=j+c[k];
			if(next<0)next=0;
			if(next>255) next=255;
			dp[i+1][next]=min(dp[i+1][next],dp[i][j]+(next-x[i])*(next-x[i]));
		}
		int ans=inf;
		rep(i,256) ans=min(ans,dp[n][i]);
		cout << ans << endl;
	}
	return 0;

}