from collections import defaultdict c = [list(map(int, input().split())) for _ in range(5)] s = sum([c[i][j] for i in range(5) for j in range(5)]) dx = [1, 0, -1, 0] dy = [0, 1, 0, -1] ans = abs(s - c[0][0] - c[0][0]) dq = [] dq.append((1, c[0][0])) used = defaultdict(bool) used[1] = True while len(dq) > 0: mask, score = dq.pop() q = [] for i in range(25): if (mask >> i) & 1 != 0: cx, cy = i // 5, i % 5 for i in range(4): nx = cx + dx[i] if nx < 0 or nx >= 5: continue ny = cy + dy[i] if ny < 0 or ny >= 5: continue nxt = mask | (1 << (nx * 5 + ny)) if (mask >> (nx * 5 + ny)) & 1 != 0 or used[nxt]: continue used[nxt] = True ans = min(ans, abs(s - 2 * (score + c[nx][ny]))) dq.append((nxt, score + c[nx][ny])) print(ans)