s=[int(s) for s in input().strip().split()] s.extend([int(s) for s in input().strip().split()]) s.extend([int(s) for s in input().strip().split()]) magicsquares=[] costs=[] for a in [2,4,6,8]: for b in [2,4,6,8]: if a+b!=10 and a!=b: m=[a,15-a-b, b, 15-a-(10-b), 5, 15-b-(10-a), 10-b, 10-(15-a-b), 10-a] magicsquares.append(m) costs.append(sum([abs(x-y) for x,y in zip(s,m)])) print(min(costs))