n=[int(x) for x in list(input()[::-1])]+[0]
ans = 0
step = 0
for i in range(len(n)-1):
  x = n[i]+step
  if x < 5 or (x==5 and n[i+1]<5):
    ans += x
    step = 0
  else:
    ans += 10 - x
    step = 1
print(ans+step) int(x) for x in list(input()[::-1]) for x in list(input()[::-1]) x list(input()[::-1]) list input()[::-1] () input ::-1 -1 for x in list(input()[::-1]) int(x) int x n=[int(x) for x in list(input()[::-1])]+[0] n [int(x) for x in list(input()[::-1])]+[0] [int(x) for x in list(input()[::-1])] [0] 0 ans = 0 ans 0 step = 0 step 0 for i in range(len(n)-1):
  x = n[i]+step
  if x < 5 or (x==5 and n[i+1]<5):
    ans += x
    step = 0
  else:
    ans += 10 - x
    step = 1 i range(len(n)-1) range len(n)-1 len(n) len n 1 x = n[i]+step x n[i]+step n[i] n i step if x < 5 or (x==5 and n[i+1]<5):
    ans += x
    step = 0
  else:
    ans += 10 - x
    step = 1 x < 5 or (x==5 and n[i+1]<5) x < 5 x 5 x==5 and n[i+1]<5 x==5 x 5 n[i+1]<5 n[i+1] n i+1 i 1 5 ans += x ans x step = 0 step 0 ans += 10 - x ans 10 - x 10 x step = 1 step 1 print(ans+step) print ans+step ans step ans += x x ans ans += 10 - x 10 - x ans step = 1 1 step step = 0 0 step step = 0 0 step n=[int(x) for x in list(input()[::-1])]+[0] [int(x) for x in list(input()[::-1])]+[0] n ans = 0 0 ans x = n[i]+step n[i]+step x
n=[int(x) for x in list(input()[::-1])]+[0]
ans = 0
step = 0
for i in range(len(n)-1):
  x = n[i]+step
  if x < 5 or (x==5 and n[i+1]<5):
    ans += x
    step = 0
  else:
    ans += 10 - x
    step = 1
print(ans+step)