n = list(map(int, reversed(list(eval(input())))))

a , b = 0, 9999

for x in n:

    a, b = min(a+x, b+x), min(a+10-x+1, b+10-x-1)

print((min(a, b)))
n = reversed(list(map(int, eval(input()))))

a, b = 0, 30

for x in n:

    a, b = min(a+x, b+x), min(a-x+11, b-x+9)

print((min(a, b)))
n = list(map(int, reversed(list(eval(input())))))
a, b = 0, 9999
for x in n:
    a, b = min(a + x, b + x), min(a + 10 - x + 1, b + 10 - x - 1)
print((min(a, b)))

n = reversed(list(map(int, eval(input()))))
a, b = 0, 30
for x in n:
    a, b = min(a + x, b + x), min(a - x + 11, b - x + 9)
print((min(a, b)))

-n = list(map(int, reversed(list(eval(input()))))) -a, b = 0, 9999 +n = reversed(list(map(int, eval(input())))) +a, b = 0, 30 -    a, b = min(a + x, b + x), min(a + 10 - x + 1, b + 10 - x - 1) +    a, b = min(a + x, b + x), min(a - x + 11, b - x + 9)