# import copy
# answers = []

from sys import stdin
input = stdin
def solve():
    while True:
        n, m = map(int,input.readline().split())
        if n == 0 and m == 0:
            break
        # c = []
        # x = [128]
        INF = float('inf')
        # for i in range(m):
        #     c.append(int(input()))
        c = tuple(int(input.readline()) for i in range(m))
        tb1 = tuple(tuple((i-j)**2 for i in range(256)) for j in range(256))
        tb2 = tuple(tuple(255 if i+ci>255 else 0 if i+ci<0 else i+ci for ci in c) for i in range(256))
        # for j in range(n):
        #     x.append(int(input()))
        # dp = [[INF]*(256) for _ in range(n+1)] #dp[i][j]は信号の大きさj入力した時のi番目の信号との二乗誤差の合計
        dp1 = [INF]*256
        dp2 = [INF]*256
        # prev = set([128])
        # dp[0][128] = 0
        dp1[128] = 0
        for i in range(n):
            x = int(input.readline())
            # nexts = set([])
            # print(prev)
            tb1_x = tb1[x]
            # for j in range(256):
            for j,precost in enumerate(dp1):
                for a in tb2[j]:
                    # a = j + c[k]
                    # a = tb2[j][k]
                    # nexts.add(a)
                    # print(dp2[a],dp1[j] + tb1[a][x])
                    # dp[i+1][a] = min(dp[i+1][a],dp[i][j] + tb1[a][x])
                    newcost = precost+tb1_x[a]
                    if newcost<dp2[a]:
                        dp2[a] = newcost
                    # dp2[a] = min(dp2[a], dp1[j] + tb1_x[a] )
            # prev = nexts
            dp1 = dp2[:]
            dp2 = [INF]*256
        # for i in range(n+1):
        #     print(x[i],"→",*dp[i][120:256])
        print(min(dp1))
solve()
# for ans in answers:
#     print(ans)
