#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */
typedef struct TYPE_6__ TYPE_3__;
typedef struct TYPE_5__ TYPE_2__;
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_6__ {
  int member_0;
  int member_1;
  int member_2;
};
struct TYPE_5__ {
  int member_0;
  int member_1;
  int member_2;
};
struct TYPE_4__ {
  int member_0;
  int member_1;
  int member_2;
};

/* Variables and functions */
scalar_t__ abs(int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ scanf(char *, int *);
int main() {
  int magic_mat[8][3][3] = {
      {{8, 1, 6}, {3, 5, 7}, {4, 9, 2}}, {{6, 1, 8}, {7, 5, 3}, {2, 9, 4}},
      {{4, 9, 2}, {3, 5, 7}, {8, 1, 6}}, {{2, 9, 4}, {7, 5, 3}, {6, 1, 8}},
      {{8, 3, 4}, {1, 5, 9}, {6, 7, 2}}, {{4, 3, 8}, {9, 5, 1}, {2, 7, 6}},
      {{6, 7, 2}, {1, 5, 9}, {8, 3, 4}}, {{2, 7, 6}, {9, 5, 1}, {4, 3, 8}},
  };
  int A[3][3];
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 3; j++)
      scanf("%d", &A[i][j]);
  }
  int min_cost = 81;
  for (int k = 0; k < 8; k++) {
    int crt_cost = 0;
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++)
        crt_cost += abs(A[i][j] - magic_mat[k][i][j]);
    }
    if (crt_cost < min_cost)
      min_cost = crt_cost;
  }
  printf("%d", min_cost);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x180,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x130(%rbp),%rdi
lea    0xea0(%rip),%rsi
mov    $0x120,%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x164(%rbp)
cmpl   $0x3,-0x164(%rbp)
jge    11f4 <main+0xb4>
movl   $0x0,-0x168(%rbp)
cmpl   $0x3,-0x168(%rbp)
jge    11db <main+0x9b>
movslq -0x164(%rbp),%rax
lea    -0x160(%rbp),%rsi
imul   $0xc,%rax,%rax
add    %rax,%rsi
movslq -0x168(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
lea    0xf5e(%rip),%rdi
call   1060 <scanf@plt>
mov    -0x168(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x168(%rbp)
jmp    118b <main+0x4b>
jmp    11e0 <main+0xa0>
mov    -0x164(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x164(%rbp)
jmp    1174 <main+0x34>
movl   $0x51,-0x16c(%rbp)
movl   $0x0,-0x170(%rbp)
cmpl   $0x8,-0x170(%rbp)
jge    1315 <main+0x1d5>
movl   $0x0,-0x174(%rbp)
movl   $0x0,-0x178(%rbp)
cmpl   $0x3,-0x178(%rbp)
jge    12de <main+0x19e>
movl   $0x0,-0x17c(%rbp)
cmpl   $0x3,-0x17c(%rbp)
jge    12c5 <main+0x185>
movslq -0x178(%rbp),%rcx
lea    -0x160(%rbp),%rax
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
movslq -0x17c(%rbp),%rcx
mov    (%rax,%rcx,4),%edi
movslq -0x170(%rbp),%rcx
lea    -0x130(%rbp),%rax
imul   $0x24,%rcx,%rcx
add    %rcx,%rax
movslq -0x178(%rbp),%rcx
imul   $0xc,%rcx,%rcx
add    %rcx,%rax
movslq -0x17c(%rbp),%rcx
sub    (%rax,%rcx,4),%edi
call   1030 <abs@plt>
mov    %rax,%rcx
movslq -0x174(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x174(%rbp)
mov    -0x17c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x17c(%rbp)
jmp    1240 <main+0x100>
jmp    12ca <main+0x18a>
mov    -0x178(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x178(%rbp)
jmp    1229 <main+0xe9>
mov    -0x174(%rbp),%eax
cmp    -0x16c(%rbp),%eax
jge    12fc <main+0x1bc>
mov    -0x174(%rbp),%eax
mov    %eax,-0x16c(%rbp)
jmp    1301 <main+0x1c1>
mov    -0x170(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x170(%rbp)
jmp    1208 <main+0xc8>
mov    -0x16c(%rbp),%esi
lea    0xdfe(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x180,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
lea    0xfdd(%rip),%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1050 <scanf@plt>
lea    0x4(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <scanf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <scanf@plt>
lea    0xc(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <scanf@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <scanf@plt>
lea    0x14(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <scanf@plt>
lea    0x18(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <scanf@plt>
lea    0x1c(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <scanf@plt>
lea    0x20(%rsp),%rsi
mov    %rbx,%rdi
call   1050 <scanf@plt>
mov    $0x51,%eax
mov    $0x20,%ebx
lea    0xe39(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %eax,%ebp
mov    (%rsp),%edi
sub    -0x20(%rbx,%r12,1),%edi
call   1030 <abs@plt>
mov    %rax,%r14
mov    0x4(%rsp),%edi
sub    -0x1c(%rbx,%r12,1),%edi
call   1030 <abs@plt>
mov    %rax,%r15
add    %r14d,%r15d
mov    0x8(%rsp),%edi
sub    -0x18(%rbx,%r12,1),%edi
call   1030 <abs@plt>
mov    %rax,%r14
add    %r15d,%r14d
mov    0xc(%rsp),%edi
sub    -0x14(%rbx,%r12,1),%edi
call   1030 <abs@plt>
mov    %rax,%r15
add    %r14d,%r15d
mov    0x10(%rsp),%edi
sub    -0x10(%rbx,%r12,1),%edi
call   1030 <abs@plt>
mov    %rax,%r14
add    %r15d,%r14d
mov    0x14(%rsp),%edi
sub    -0xc(%rbx,%r12,1),%edi
call   1030 <abs@plt>
mov    %rax,%r15
add    %r14d,%r15d
mov    0x18(%rsp),%edi
sub    -0x8(%rbx,%r12,1),%edi
call   1030 <abs@plt>
mov    %rax,%r14
add    %r15d,%r14d
mov    0x1c(%rsp),%edi
sub    -0x4(%rbx,%r12,1),%edi
call   1030 <abs@plt>
mov    %rax,%r15
add    %r14d,%r15d
mov    0x20(%rsp),%edi
sub    (%rbx,%r12,1),%edi
call   1030 <abs@plt>
add    %r15d,%eax
cmp    %ebp,%eax
cmovge %ebp,%eax
add    $0x24,%rbx
cmp    $0x140,%rbx
jne    11d0 <main+0xa0>
lea    0xe85(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.magic_mat = [8 x [3 x [3 x i32]]] [[3 x [3 x i32]] [[3 x i32] [i32 8, i32 1, i32 6], [3 x i32] [i32 3, i32 5, i32 7], [3 x i32] [i32 4, i32 9, i32 2]], [3 x [3 x i32]] [[3 x i32] [i32 6, i32 1, i32 8], [3 x i32] [i32 7, i32 5, i32 3], [3 x i32] [i32 2, i32 9, i32 4]], [3 x [3 x i32]] [[3 x i32] [i32 4, i32 9, i32 2], [3 x i32] [i32 3, i32 5, i32 7], [3 x i32] [i32 8, i32 1, i32 6]], [3 x [3 x i32]] [[3 x i32] [i32 2, i32 9, i32 4], [3 x i32] [i32 7, i32 5, i32 3], [3 x i32] [i32 6, i32 1, i32 8]], [3 x [3 x i32]] [[3 x i32] [i32 8, i32 3, i32 4], [3 x i32] [i32 1, i32 5, i32 9], [3 x i32] [i32 6, i32 7, i32 2]], [3 x [3 x i32]] [[3 x i32] [i32 4, i32 3, i32 8], [3 x i32] [i32 9, i32 5, i32 1], [3 x i32] [i32 2, i32 7, i32 6]], [3 x [3 x i32]] [[3 x i32] [i32 6, i32 7, i32 2], [3 x i32] [i32 1, i32 5, i32 9], [3 x i32] [i32 8, i32 3, i32 4]], [3 x [3 x i32]] [[3 x i32] [i32 2, i32 7, i32 6], [3 x i32] [i32 9, i32 5, i32 1], [3 x i32] [i32 4, i32 3, i32 8]]]
@.str = [3 x i8] c"%d\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [8 x [3 x [3 x i32]]]
%3 = alloca [3 x [3 x i32]]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %1
%11 = bitcast [8 x [3 x [3 x i32]]]* %2 to i8*
store i32 0, i32* %4
br label %12
12: ; preds = %31, %0
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 3
br i1 %14, label %15, label %34
15: ; preds = %12
store i32 0, i32* %5
br label %16
16: ; preds = %27, %15
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 3
br i1 %18, label %19, label %30
19: ; preds = %16
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [3 x [3 x i32]], [3 x [3 x i32]]* %3, i64 0, i64 %21
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [3 x i32], [3 x i32]* %22, i64 0, i64 %24
%26 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %25)
br label %27
27: ; preds = %19
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
br label %16
30: ; preds = %16
br label %31
31: ; preds = %30
%32 = load i32, i32* %4
%33 = add nsw i32 %32, 1
store i32 %33, i32* %4
br label %12
34: ; preds = %12
store i32 81, i32* %6
store i32 0, i32* %7
br label %35
35: ; preds = %84, %34
%36 = load i32, i32* %7
%37 = icmp slt i32 %36, 8
br i1 %37, label %38, label %87
38: ; preds = %35
store i32 0, i32* %8
store i32 0, i32* %9
br label %39
39: ; preds = %74, %38
%40 = load i32, i32* %9
%41 = icmp slt i32 %40, 3
br i1 %41, label %42, label %77
42: ; preds = %39
store i32 0, i32* %10
br label %43
43: ; preds = %70, %42
%44 = load i32, i32* %10
%45 = icmp slt i32 %44, 3
br i1 %45, label %46, label %73
46: ; preds = %43
%47 = load i32, i32* %9
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [3 x [3 x i32]], [3 x [3 x i32]]* %3, i64 0, i64 %48
%50 = load i32, i32* %10
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [3 x i32], [3 x i32]* %49, i64 0, i64 %51
%53 = load i32, i32* %52
%54 = load i32, i32* %7
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [8 x [3 x [3 x i32]]], [8 x [3 x [3 x i32]]]* %2, i64 0, i64 %55
%57 = load i32, i32* %9
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [3 x [3 x i32]], [3 x [3 x i32]]* %56, i64 0, i64 %58
%60 = load i32, i32* %10
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds [3 x i32], [3 x i32]* %59, i64 0, i64 %61
%63 = load i32, i32* %62
%64 = sub nsw i32 %53, %63
%65 = call i64 @abs(i32 %64)
%66 = load i32, i32* %8
%67 = sext i32 %66 to i64
%68 = add nsw i64 %67, %65
%69 = trunc i64 %68 to i32
store i32 %69, i32* %8
br label %70
70: ; preds = %46
%71 = load i32, i32* %10
%72 = add nsw i32 %71, 1
store i32 %72, i32* %10
br label %43
73: ; preds = %43
br label %74
74: ; preds = %73
%75 = load i32, i32* %9
%76 = add nsw i32 %75, 1
store i32 %76, i32* %9
br label %39
77: ; preds = %39
%78 = load i32, i32* %8
%79 = load i32, i32* %6
%80 = icmp slt i32 %78, %79
br i1 %80, label %81, label %83
81: ; preds = %77
%82 = load i32, i32* %8
store i32 %82, i32* %6
br label %83
83: ; preds = %81, %77
br label %84
84: ; preds = %83
%85 = load i32, i32* %7
%86 = add nsw i32 %85, 1
store i32 %86, i32* %7
br label %35
87: ; preds = %35
%88 = load i32, i32* %6
%89 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %88)
ret i32 0
}

LLVM IR (-O2):
@__const.main.magic_mat = [8 x [3 x [3 x i32]]] [[3 x [3 x i32]] [[3 x i32] [i32 8, i32 1, i32 6], [3 x i32] [i32 3, i32 5, i32 7], [3 x i32] [i32 4, i32 9, i32 2]], [3 x [3 x i32]] [[3 x i32] [i32 6, i32 1, i32 8], [3 x i32] [i32 7, i32 5, i32 3], [3 x i32] [i32 2, i32 9, i32 4]], [3 x [3 x i32]] [[3 x i32] [i32 4, i32 9, i32 2], [3 x i32] [i32 3, i32 5, i32 7], [3 x i32] [i32 8, i32 1, i32 6]], [3 x [3 x i32]] [[3 x i32] [i32 2, i32 9, i32 4], [3 x i32] [i32 7, i32 5, i32 3], [3 x i32] [i32 6, i32 1, i32 8]], [3 x [3 x i32]] [[3 x i32] [i32 8, i32 3, i32 4], [3 x i32] [i32 1, i32 5, i32 9], [3 x i32] [i32 6, i32 7, i32 2]], [3 x [3 x i32]] [[3 x i32] [i32 4, i32 3, i32 8], [3 x i32] [i32 9, i32 5, i32 1], [3 x i32] [i32 2, i32 7, i32 6]], [3 x [3 x i32]] [[3 x i32] [i32 6, i32 7, i32 2], [3 x i32] [i32 1, i32 5, i32 9], [3 x i32] [i32 8, i32 3, i32 4]], [3 x [3 x i32]] [[3 x i32] [i32 2, i32 7, i32 6], [3 x i32] [i32 9, i32 5, i32 1], [3 x i32] [i32 4, i32 3, i32 8]]]
@.str = [3 x i8] c"%d\00"
define i32 @main() {
%1 = alloca [3 x [3 x i32]]
%2 = bitcast [3 x [3 x i32]]* %1 to i8*
%3 = getelementptr inbounds [3 x [3 x i32]], [3 x [3 x i32]]* %1, i64 0, i64 0, i64 0
%4 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
%5 = getelementptr inbounds [3 x [3 x i32]], [3 x [3 x i32]]* %1, i64 0, i64 0, i64 1
%6 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %5)
%7 = getelementptr inbounds [3 x [3 x i32]], [3 x [3 x i32]]* %1, i64 0, i64 0, i64 2
%8 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %7)
%9 = getelementptr inbounds [3 x [3 x i32]], [3 x [3 x i32]]* %1, i64 0, i64 1, i64 0
%10 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %9)
%11 = getelementptr inbounds [3 x [3 x i32]], [3 x [3 x i32]]* %1, i64 0, i64 1, i64 1
%12 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %11)
%13 = getelementptr inbounds [3 x [3 x i32]], [3 x [3 x i32]]* %1, i64 0, i64 1, i64 2
%14 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %13)
%15 = getelementptr inbounds [3 x [3 x i32]], [3 x [3 x i32]]* %1, i64 0, i64 2, i64 0
%16 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %15)
%17 = getelementptr inbounds [3 x [3 x i32]], [3 x [3 x i32]]* %1, i64 0, i64 2, i64 1
%18 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %17)
%19 = getelementptr inbounds [3 x [3 x i32]], [3 x [3 x i32]]* %1, i64 0, i64 2, i64 2
%20 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %19)
br label %21
21: ; preds = %0, %21
%22 = phi i64 [ 0, %0 ], [ %88, %21 ]
%23 = phi i32 [ 81, %0 ], [ %87, %21 ]
%24 = load i32, i32* %3
%25 = getelementptr inbounds [8 x [3 x [3 x i32]]], [8 x [3 x [3 x i32]]]* @__const.main.magic_mat, i64 0, i64 %22, i64 0, i64 0
%26 = load i32, i32* %25
%27 = sub nsw i32 %24, %26
%28 = call i64 @abs(i32 %27)
%29 = trunc i64 %28 to i32
%30 = load i32, i32* %5
%31 = getelementptr inbounds [8 x [3 x [3 x i32]]], [8 x [3 x [3 x i32]]]* @__const.main.magic_mat, i64 0, i64 %22, i64 0, i64 1
%32 = load i32, i32* %31
%33 = sub nsw i32 %30, %32
%34 = call i64 @abs(i32 %33)
%35 = trunc i64 %34 to i32
%36 = add i32 %29, %35
%37 = load i32, i32* %7
%38 = getelementptr inbounds [8 x [3 x [3 x i32]]], [8 x [3 x [3 x i32]]]* @__const.main.magic_mat, i64 0, i64 %22, i64 0, i64 2
%39 = load i32, i32* %38
%40 = sub nsw i32 %37, %39
%41 = call i64 @abs(i32 %40)
%42 = trunc i64 %41 to i32
%43 = add i32 %36, %42
%44 = load i32, i32* %9
%45 = getelementptr inbounds [8 x [3 x [3 x i32]]], [8 x [3 x [3 x i32]]]* @__const.main.magic_mat, i64 0, i64 %22, i64 1, i64 0
%46 = load i32, i32* %45
%47 = sub nsw i32 %44, %46
%48 = call i64 @abs(i32 %47)
%49 = trunc i64 %48 to i32
%50 = add i32 %43, %49
%51 = load i32, i32* %11
%52 = getelementptr inbounds [8 x [3 x [3 x i32]]], [8 x [3 x [3 x i32]]]* @__const.main.magic_mat, i64 0, i64 %22, i64 1, i64 1
%53 = load i32, i32* %52
%54 = sub nsw i32 %51, %53
%55 = call i64 @abs(i32 %54)
%56 = trunc i64 %55 to i32
%57 = add i32 %50, %56
%58 = load i32, i32* %13
%59 = getelementptr inbounds [8 x [3 x [3 x i32]]], [8 x [3 x [3 x i32]]]* @__const.main.magic_mat, i64 0, i64 %22, i64 1, i64 2
%60 = load i32, i32* %59
%61 = sub nsw i32 %58, %60
%62 = call i64 @abs(i32 %61)
%63 = trunc i64 %62 to i32
%64 = add i32 %57, %63
%65 = load i32, i32* %15
%66 = getelementptr inbounds [8 x [3 x [3 x i32]]], [8 x [3 x [3 x i32]]]* @__const.main.magic_mat, i64 0, i64 %22, i64 2, i64 0
%67 = load i32, i32* %66
%68 = sub nsw i32 %65, %67
%69 = call i64 @abs(i32 %68)
%70 = trunc i64 %69 to i32
%71 = add i32 %64, %70
%72 = load i32, i32* %17
%73 = getelementptr inbounds [8 x [3 x [3 x i32]]], [8 x [3 x [3 x i32]]]* @__const.main.magic_mat, i64 0, i64 %22, i64 2, i64 1
%74 = load i32, i32* %73
%75 = sub nsw i32 %72, %74
%76 = call i64 @abs(i32 %75)
%77 = trunc i64 %76 to i32
%78 = add i32 %71, %77
%79 = load i32, i32* %19
%80 = getelementptr inbounds [8 x [3 x [3 x i32]]], [8 x [3 x [3 x i32]]]* @__const.main.magic_mat, i64 0, i64 %22, i64 2, i64 2
%81 = load i32, i32* %80
%82 = sub nsw i32 %79, %81
%83 = call i64 @abs(i32 %82)
%84 = trunc i64 %83 to i32
%85 = add i32 %78, %84
%86 = icmp slt i32 %85, %23
%87 = select i1 %86, i32 %85, i32 %23
%88 = add nuw nsw i64 %22, 1
%89 = icmp eq i64 %88, 8
br i1 %89, label %90, label %21
90: ; preds = %21
%91 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %87)
ret i32 0
}