N = list(map(int, input()))
N = N[::-1] + [0]

count = 0

for i, n in enumerate(N):
  if n < 5:
    count += n
  elif n > 5:
    count += 10 - n
    N[i+1] += 1
  elif n == 5:
    if N[i+1] >= 5:
      N[i+1] += 1
    count += 5
    
print(count) N = list(map(int, input())) N list(map(int, input())) list map(int, input()) map int input() input N = N[::-1] + [0] N N[::-1] + [0] N[::-1] N ::-1 -1 [0] 0 count = 0 count 0 for i, n in enumerate(N):
  if n < 5:
    count += n
  elif n > 5:
    count += 10 - n
    N[i+1] += 1
  elif n == 5:
    if N[i+1] >= 5:
      N[i+1] += 1
    count += 5
     i n enumerate(N) enumerate N if n < 5:
    count += n
  elif n > 5:
    count += 10 - n
    N[i+1] += 1
  elif n == 5:
    if N[i+1] >= 5:
      N[i+1] += 1
    count += 5
     n < 5 n 5 count += n count n elif n > 5:
    count += 10 - n
    N[i+1] += 1
   n > 5 n 5 count += 10 - n count 10 - n 10 n N[i+1] += 1 N[i+1] N i+1 i 1 1 elif n == 5:
    if N[i+1] >= 5:
      N[i+1] += 1
    count += 5
     n == 5 n 5 if N[i+1] >= 5:
      N[i+1] += 1
     N[i+1] >= 5 N[i+1] N i+1 i 1 5 N[i+1] += 1 N[i+1] N i+1 i 1 1 count += 5 count 5 print(count) print count N = list(map(int, input())) list(map(int, input())) N count += 5 5 count count += 10 - n 10 - n count N = N[::-1] + [0] N[::-1] + [0] N count += n n count count = 0 0 count
N = list(map(int, input()))
N = N[::-1] + [0]

count = 0

for i, n in enumerate(N):
  if n < 5:
    count += n
  elif n > 5:
    count += 10 - n
    N[i+1] += 1
  elif n == 5:
    if N[i+1] >= 5:
      N[i+1] += 1
    count += 5
    
print(count)