# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))

def main():
    N, M = inpl()
    INF = 10**9
    while N:
        C = [int(input()) for _ in range(M)]
        X = [int(input()) for _ in range(N)]
        Y = [[INF]*256 for i in range(N+1)]
    
        for i in range(256):
            Y[0][i] = 0
    
        for i, x in enumerate(X):
            for j in range(256):
                if Y[i][j] == INF:
                    continue
                for c in C:
                    k = min(255, max(0, j+c))
                    Y[i+1][k] = min(Y[i+1][k], Y[i][j] + (x - k)**2)
    
        print(min(Y[-1]))
        N, M = inpl()

if __name__ == "__main__":
    main()
