#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cstdlib>

using namespace std;

const int INF=1000000000;

int n,m;
int dp[256][20001];
int codeBook[20];
int input[20001];

int dfs(int lastNum,int idx){
    if(idx==n)
        return 0;
    if(dp[lastNum][idx]!=INF)
        return dp[lastNum][idx];
    int minSum=INF;
    for(int i = 0; i < m; i++){
        int nextNum=min(255,max(0,lastNum+codeBook[i]));
        minSum=min(minSum,dfs(nextNum,idx+1)+(nextNum-input[idx])*(nextNum-input[idx]));
    }
    return dp[lastNum][idx]=minSum;
}

int main(){

    while(cin>>n>>m&&!(n==0&&m==0)){
        for(int i = 0; i < 256; i++)
            for(int j = 0; j < 20001; j++)
                dp[i][j]=INF;
        for(int i = 0; i < m; i++)
            cin>>codeBook[i];
        for(int i = 0; i < n; i++)
            cin>>input[i];
        int res=dfs(128,0);
        cout<<res<<endl;
    }

    return 0;
}