#include <bits/stdc++.h>
using namespace std;

int main()
{
vector<int> C,x;vector<vector<int>> dp;
int N,M,num,ans=1000000000,i,j,l;scanf("%d %d",&N,&M);
while(N!=0&&M!=0)
{
C.resize(0);x.resize(0);dp.resize(0);
C.resize(M),x.resize(N),dp.resize(N+1);for(i=0;i<N+1;i++)dp[i].resize(256,-1);dp[0][128]=0;
for(i=0;i<M;i++)scanf("%d",&C[i]);
for(i=0;i<N;i++)scanf("%d",&x[i]);
for(i=0;i<N;i++)for(j=0;j<256;j++)for(l=0;l<M;l++)
{
if(dp[i][j]==-1)break;
num=j+C[l];
if(num<0)num=0;else if(num>255)num=255;
if(dp[i+1][num]==-1)dp[i+1][num]=dp[i][j]+(x[i]-num)*(x[i]-num);
else dp[i+1][num]=min(dp[i][j]+(x[i]-num)*(x[i]-num),dp[i+1][num]);
}
for(i=0;i<256;i++){if(dp[N][i]==-1)continue;ans=min(ans,dp[N][i]);}
printf("%d\n",ans);ans=1000000000;
scanf("%d %d",&N,&M);
}
}




