INF = float("inf")
while 1:
	N, M = map(int,raw_input().split(" "))
	if M == N == 0 :
		break
	y0 = 128
	C = [int(raw_input()) for _ in range(M)]
	x = [int(raw_input()) for _ in range(N)]
	dp = [[INF for _ in range(256)] for _ in range(N+1)]
	dp[0][128] = 0
	for i in range(N):
		for k in range(256):
			v = dp[i][k] 
			if v == INF : continue
			for c in C:
				if k + c > 255: y = 255
				elif k + c < 0: y = 0
				else :          y = k + c
				sub_sq = (x[i]-y)**2
				if  v + sub_sq < dp[i+1][y]: 
					dp[i+1][y] = v + sub_sq
	print sorted(dp[N])[0]