import math, sys a_ms = [ [8,1,6,3,5,7,4,9,2], [6,1,8,7,5,3,2,9,4], [4,3,8,9,5,1,2,7,6], [2,7,6,9,5,1,4,3,8], [2,9,4,7,5,3,6,1,8], [4,9,2,3,5,7,8,1,6], [6,7,2,1,5,9,8,3,4], [8,3,4,1,5,9,6,7,2] ] def func(A,B): return [abs(a-b) for a,b in zip(A,B) if abs(a-b)>=0] t=[] for line in sys.stdin: t += [i for i in line if i in [str(i) for i in range(10)]] t = [int(i) for i in t] l=[] min_= 100 for i in a_ms: if min_ > sum(func(t,i)): min_ = sum(func(t,i)) print(min_)