function Main(input){
    input = input.split("\n");
    var l = 0;
    var dp = new Array(20001);
    for(var i = 0;i <= 20000;i++)
        dp[i] = new Array(256);
    while(1){
        input[l] = input[l].split(" ");
        var n = parseInt(input[l][0],10);
        var m = parseInt(input[l++][1],10);
        if(n == 0 && m == 0)
            break;

        var c = [],x = [];

        for(var i = 1;i <= m;i++)
            c[i] = parseInt(input[l++],10);

        for(var i = 1;i <= n;i++)
            x[i] = parseInt(input[l++],10);
        for(var i = 0;i <= 20000;i++)
            for(var j = 0;j < 256;j++)
                dp[i][j] = 1000000000000;
        dp[0][128] = 0;

        for(var i = 0;i < n;i++){
            for(var j = 0;j < 256;j++){
                for(var k = 1;k <= m;k++){
                    var a = j + c[k];
                    if(a < 0)
                        a = 0;
                    if(a > 255)
                        a = 255;
                    var b = dp[i][j] + (a - x[i + 1]) * (a - x[i + 1]);
                    if(dp[i + 1][a] > b)
                        dp[i + 1][a] = b;
                }
            }
        }

        var min = 1000000000000;

        for(var i = 0;i < 256;i++)
            if(min > dp[n][i])
                min = dp[n][i];

        console.log(min);
    }
}
Main(require("fs").readFileSync("/dev/stdin","utf8"));