#include <stdio.h>
#include <string.h>
#define ll long long
#define rep(i,l,r)for(ll i=(l);i<(r);i++)
#define repp(i,l,r,k)for(ll i=(l);i<(r);i+=(k))
#define INF ((1LL<<62)-(1LL<<31))
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))
#define bit(n,m)((n>>m)&1)
int upll(const void*a, const void*b){return*(ll*)a<*(ll*)b?-1:*(ll*)a>*(ll*)b?1:0;}
int downll(const void*a, const void*b){return*(ll*)a<*(ll*)b?1:*(ll*)a>*(ll*)b?-1:0;}
void sortup(ll*a,int n){qsort(a,n,sizeof(ll),upll);}
void sortdown(ll*a,int n){qsort(a,n,sizeof(ll),downll);}

ll dddd[2][300],*dp1=dddd[0],*dp2=dddd[1],*temp;
ll c[20];
ll x[20010];
int main(){
	ll n,m;
	while(scanf("%lld%lld\n",&n,&m),n){
		rep(i,0,m)scanf("%lld",c+i);
		rep(i,0,n)scanf("%lld",x+i);
		rep(i,0,256)dp1[i]=INF;
		dp1[128]=0;
		rep(nnnn,0,n){
			rep(i,0,256)dp2[i]=INF;		
			//配るDP
			rep(i,0,256)rep(j,0,m){
				ll nxt=min(255,max(0,i+c[j]));
				dp2[nxt]=min(dp2[nxt],dp1[i]+(nxt-x[nnnn])*(nxt-x[nnnn]));
			}
			temp=dp1;dp1=dp2;dp2=temp;
		}
		ll ans=INF;
		rep(i,0,256)ans=min(ans,dp1[i]);
		printf("%lld\n",ans);
	}
	return 0;
}
