import sys
R = lambda:map(int,raw_input().split())
yNum = 256
MAX_VAL = 10**6
while 1:
    n,m = R()
    if not (n or m): break
    c = []
    x = [128]
    for i in xrange(m):
        c.append(int(raw_input()))
    for i in xrange(n):
        x.append(int(raw_input()))
##    sub = [[0 for j in xrange(yNum)] for i in xrange(n+1)]
##    for i in xrange(1,n+1):
##        for j in xrange(yNum):
##            sub[i][j] = (j-x[i])**2
    values = [[MAX_VAL for j in xrange(yNum)] for i in xrange(n+1)]
    values[0][128] = 0
    for i in xrange(n):
        for j in xrange(yNum):
            for k in xrange(m):
                if 0 <= j+c[k] <= 255:
                    values[i+1][j+c[k]] = min(values[i+1][j+c[k]], values[i][j]+(j+c[k]-x[i+1])**2)
                elif 0 > j+c[k]:
                    values[i+1][0] = min(values[i+1][0], values[i][j]+(0-x[i+1])**2)
                else:
                    values[i+1][255] = min(values[i+1][255], values[i][j]+(255-x[i+1])**2)
                #if values[i][j] < 10**5: print values[i][j]
    print min(values[n])