# Enter your code here. Read input from STDIN. Print output to STDOUT magic = {1: 4, 2: 9, 3: 2, 4: 3, 5: 5, 6: 7, 7: 8, 8: 1, 9: 6} lst = [] for _ in range(3): lst += map(int, raw_input().strip().split(' ')) arr = {i + 1: x for i, x in enumerate(lst)} def cost(arr, magic): c = 0 for k, v in magic.iteritems(): c += abs(arr[k] - v) return c def rotate(arr): key_map = [(1, 7), (2, 4), (3, 1), (6, 2), (9, 3), (8, 6), (7, 9), (4, 8), (5, 5)] new_arr = {} for i, j in key_map: new_arr[j] = arr[i] return new_arr def mirror(arr): key_map = [(1, 3), (2, 2), (3, 1), (4, 6), (5, 5), (6, 4), (7, 9), (8, 8), (9, 7)] new_arr = {} for i, j in key_map: new_arr[j] = arr[i] return new_arr costs = [] costs.append(cost(arr, magic)) for _ in range(4): arr = rotate(arr) costs.append(cost(arr, magic)) mirr = mirror(arr) costs.append(cost(mirr, magic)) print min(costs)