#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <climits>
using namespace std;
#define FOR(i,a,n) for(int i=a;i<n;i++)
#define REP(i,n) FOR(i,0,n)
typedef vector<int> vi;
typedef vector<vi> vii;

int main(){
	int n,m;
	while(cin>>n>>m,n&&m){
		vi c(m),x(n);
		REP(i,m){
			cin>>c[i];
		}
		REP(i,n){
			cin>>x[i];
		}

		vii dp(n+1,vi(256,INT_MAX));
		dp[0][128]=0;
		REP(i,n){
			REP(j,256){
				if(dp[i][j]==INT_MAX)continue;
				REP(k,m){
					int y=j+c[k];
					if(y<0)y=0;
					if(y>255)y=255;
					dp[i+1][y]=min(dp[i+1][y],dp[i][j]+(int)pow((double)y-x[i],2));
				}
			}
		}
		int ans=INT_MAX;
		REP(i,256){
			ans=min(ans,dp[n][i]);
		}
		cout<<ans<<endl;
	}
}