#include<bits/stdc++.h>
using namespace std;
int dp[2][256];
const int INF=1e9;
int N,M;
int val[20000],c[16];

inline int conv(int v){
    return max(min(v,255),0);
}

void solve(){
    fill_n(*dp,2*256,INF);
    dp[0][128]=0;
    int *prev=dp[0],*next=dp[1];

    for(int i=0;i<N;i++){
        for(int j=0;j<256;j++){
            if(prev[j]==INF)continue;
            for(int k=0;k<M;k++){
                int t=conv(j+c[k]);
                int v=(t-val[i])*(t-val[i]);

                next[t]=min(next[t],prev[j]+v);
            }
            prev[j]=INF;
        }
        swap(prev,next);
    }

    printf("%d\n",*min_element(dp[N&1],dp[N&1]+256));

}

int main(){
    while(scanf("%d%d",&N,&M),N||M){
        for(int i=0;i<M;i++)scanf("%d",&c[i]);
        for(int i=0;i<N;i++)scanf("%d",&val[i]);
        solve();
    }
    return 0;
}