var sq1 = [8,1,6,3,5,7,4,9,2]; var sq2 = [6,1,8,7,5,3,2,9,4]; var sq3 = [4,3,8,9,5,1,2,7,6]; var sq4 = [2,7,6,9,5,1,4,3,8]; var sq5 = [2,9,4,7,5,3,6,1,8]; var sq6 = [4,9,2,3,5,7,8,1,6]; var sq7 = [6,7,2,1,5,9,8,3,4]; var sq8 = [8,3,4,1,5,9,6,7,2]; function processData(input) { var cost = 0; //console.log(input); input = input.split(/\s+/).map(Number); var sqs = [sq1,sq2,sq3,sq4,sq5,sq6,sq7,sq8]; var costs = [0,0,0,0,0,0,0,0]; for (var i = 0; i < 8; i++) { for (var j = 0; j < 9; j++) { costs[i] += Math.abs(sqs[i][j] - input[j]); } } console.log(Math.min.apply(Math, costs)); } process.stdin.resume(); process.stdin.setEncoding("ascii"); _input = ""; process.stdin.on("data", function (input) { _input += input; }); process.stdin.on("end", function () { processData(_input); });