/*
AizuOnline A2199
Title Differential Pulse Code Modulation
@kankichi573
*/
#include <stdio.h>
#include <string.h>
#include <limits.h>
#define min(x,y) (((x)<(y))?(x):(y))

int N,M;
int C[16];
int x[20001];
int old_sum_sqr_dif[256];
int new_sum_sqr_dif[256];
//
int find_min(int array[])
{
        int i,min_;
        min_=INT_MAX;
        for(i=0;i<=255;i++)
                if(array[i]!=-1 && array[i]<min_)
                {
                        //printf("Ia=%d %d",i,array[i]);
                        min_=array[i];
                }
        return(min_);
}
solve()
{
        int i,j,k,s,idx,newval;
        for(i=1;i<=N;i++)
        {
                for(j=0;j<=255;j++)
                {
                        s=old_sum_sqr_dif[j];
                        //printf("IJS=%d:%d:%d\n",i,j,s);

                        if(s!=-1)
                        {
                                for(k=0;k<M;k++)
                                {
                                        idx=j+C[k];
                                        if(idx>255)
                                                idx=255;
                                        else if(idx<0)
                                                idx=0;
                                        //printf("Idx=%d\n",idx);

                                                                                newval=s+(idx-x[i])*(idx-x[i]);
                                        if(new_sum_sqr_dif[idx]==-1)
                                                new_sum_sqr_dif[idx]=newval;
                                        else
                                                new_sum_sqr_dif[idx]=min(newval,new_sum_sqr_dif[idx]);

                                        //printf("%d::%d:%d\n",i,idx,newval);

                                }
                        }

                }
                memcpy(old_sum_sqr_dif,new_sum_sqr_dif,256*4);
                memset(new_sum_sqr_dif,-1,256*4);
        }
        return(find_min(old_sum_sqr_dif));
}

main()
{
        int i,ret;

        while(EOF!=scanf("%d %d",&N,&M) && (N||M))
        {

                for(i=0;i<M;i++)
                        scanf("%d",&C[i]);
                for(i=1;i<=N;i++)
                        scanf("%d",&x[i]);

                memset(old_sum_sqr_dif,-1,256*4);
                memset(new_sum_sqr_dif,-1,256*4);
                old_sum_sqr_dif[128]=0;  //開始点

                ret=solve();

                printf("%d\n",ret);

        }
        return(0);
}