//Yattazo! Watashiha-Yosen-tsuukashita! It's 460points/600points! OHO!!!!!
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int x[21000][256],n,m,y[64000],z[64000],d,minx;
int main(){
	while(true){
		minx=2000000000;
		for(int i=0;i<21000;i++){for(int j=0;j<256;j++){x[i][j]=2000000000;}}
		cin>>n>>m;if(!n){break;}
		for(int i=0;i<m;i++){cin>>y[i];}
		for(int i=1;i<=n;i++){cin>>z[i];}
		x[0][128]=0;
		for(int i=1;i<=n;i++){
			for(int j=0;j<256;j++){
				if(x[i-1][j]<1000000000){
					for(int k=0;k<m;k++){
						d=y[k]+j;
						if(d<0){d=0;}
						if(d>255){d=255;}
						x[i][d]=min(x[i][d],x[i-1][j]+(int)pow(1.0*(z[i]-d),2));
					}
				}
			}
		}
		for(int i=0;i<256;i++){minx=min(minx,x[n][i]);}
		cout<<minx<<endl;
	}
}