s=input()[::-1]+'0'
d=[0,1]
for c in s:
    x=int(c)
    d=[x+min(d[0],1+d[1]),min(1+d[0],d[1])+9-x]
print(min(d)) s=input()[::-1]+'0' s input()[::-1]+'0' input()[::-1] () input ::-1 -1 '0' d=[0,1] d [0,1] 0 1 for c in s:
    x=int(c)
    d=[x+min(d[0],1+d[1]),min(1+d[0],d[1])+9-x] c s x=int(c) x int(c) int c d=[x+min(d[0],1+d[1]),min(1+d[0],d[1])+9-x] d [x+min(d[0],1+d[1]),min(1+d[0],d[1])+9-x] x+min(d[0],1+d[1]) x min(d[0],1+d[1]) min d[0] d 0 1+d[1] 1 d[1] d 1 min(1+d[0],d[1])+9-x min(1+d[0],d[1])+9 min(1+d[0],d[1]) min 1+d[0] 1 d[0] d 0 d[1] d 1 9 x print(min(d)) print min(d) min d d=[0,1] [0,1] d s=input()[::-1]+'0' input()[::-1]+'0' s x=int(c) int(c) x d=[x+min(d[0],1+d[1]),min(1+d[0],d[1])+9-x] [x+min(d[0],1+d[1]),min(1+d[0],d[1])+9-x] d
s=input()[::-1]+'0'
d=[0,1]
for c in s:
    x=int(c)
    d=[x+min(d[0],1+d[1]),min(1+d[0],d[1])+9-x]
print(min(d))
