u = [] for i in range(3): u += list(map(int, input().strip().split()))[:3] l = [] l.append([4, 9, 2, 3, 5, 7, 8, 1, 6]) l.append([8, 1, 6, 3, 5, 7, 4, 9, 2]) l.append([2, 9, 4, 7, 5, 3, 6, 1, 8]) l.append([4, 3, 8, 9, 5, 1, 2, 7, 6]) l.append([6, 7, 2, 1, 5, 9, 8, 3, 4]) l.append([8, 3, 4, 1, 5, 9, 6, 7, 2]) l.append([6, 1, 8, 7, 5, 3, 2, 9, 4]) l.append([2, 7, 6, 9, 5, 1, 4, 3, 8]) m = None for i in l: s = 0 for j in range(9): s += abs(i[j]-u[j]) if m == None: m = s elif s < m: m = s print(m)