#include <bits/stdc++.h>
using namespace std;
const int MAX_N=2e4+10,MAX_M=20,MAX=256,INF=1e9;

int N,M,nxt,ans;
int C[MAX_M],x[MAX_N];
int dp[MAX_N][MAX];

void solve(){
    for (int i=0;i<MAX_N;++i)
        for (int j=0;j<MAX;++j)
            dp[i][j]=INF;
    dp[0][128]=0;
    for (int i=0;i<N;++i){
        for (int j=0;j<MAX;++j){
            for (int k=0;k<M;++k){
                nxt=max(0,min(MAX-1,j+C[k]));
                dp[i+1][nxt]=min(dp[i+1][nxt],dp[i][j]+(x[i]-nxt)*(x[i]-nxt));
            }
        }
    }
    ans=INF;
    for (int j=0;j<MAX;++j) ans=min(ans,dp[N][j]);
    cout << ans << '\n';
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> N >> M,N){
        for (int i=0;i<M;++i) cin >> C[i];
        for (int i=0;i<N;++i) cin >> x[i];
        solve();
    }
}
