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 = {y0:0}
	for i in range(N):
		tmpdp = dict()
		for k,v in dp.iteritems():
			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 not(y in tmpdp and v + sub_sq >= tmpdp[y]):
					tmpdp[y] = v + sub_sq
		dp = tmpdp
	print sorted(dp.values())[0]