#include <iostream>
#include <string>
#include <algorithm>
#include <set>
#include <vector>
#include <cstring>
#include <climits>
#include <queue>
#include <map>
#include <sstream>
using namespace std;

#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define EPS 1e-9

int pulse[20000];
int dp[2][256];

int main(){
	int n,m;
	int book[16];
	while(scanf("%d%d",&n,&m),n|m){
		rep(i,m)scanf("%d",&book[i]);
		rep(i,n)scanf("%d",&pulse[i]);
		rep(i,256)dp[0][i]=dp[1][i]=INT_MAX;
		
		dp[0][128] = 0;
		rep(i,n){
			int cur = i%2;
			int next = (i+1)%2;
			rep(j,256)if( dp[cur][j]!=INT_MAX ){
				rep(k,m){
					int d = j+book[k];
					if( d<0 )d=0;
					if( d>255 )d=255;
					dp[next][d] = min(dp[next][d], dp[cur][j]+(pulse[i]-d)*(pulse[i]-d));
				}
			}
			rep(j,256)dp[cur][j]=INT_MAX;
		}
		int ans = INT_MAX;
		rep(i,256)ans=min(ans,dp[n%2][i]);
		printf("%d\n",ans);
	}
	return 0;
}