n = eval(input())

precise = 0

ok_over = 1

ok_prec = 0

for d in n:

    d = int(d)

    new_over = min(precise + d + 1, ok_over + d + 2, ok_over + 9 - d, ok_prec + d + 1)

    new_prec = min(ok_over + d + 1, ok_prec + d)

    precise += d

    ok_over = new_over

    ok_prec = new_prec

ok_over += 1

print((min(precise, ok_over, ok_prec)))

n = eval(input())

precise = 0

over = 1

for d in n:

    d = int(d)

    new_over = min(precise + d + 1, over + d + 2, over + 9 - d)

    precise = min(over + d + 1, precise + d)

    over = new_over

over += 1

print((min(precise, over)))

n = eval(input())
precise = 0
ok_over = 1
ok_prec = 0
for d in n:
    d = int(d)
    new_over = min(precise + d + 1, ok_over + d + 2, ok_over + 9 - d, ok_prec + d + 1)
    new_prec = min(ok_over + d + 1, ok_prec + d)
    precise += d
    ok_over = new_over
    ok_prec = new_prec
ok_over += 1
print((min(precise, ok_over, ok_prec)))

n = eval(input())
precise = 0
over = 1
for d in n:
    d = int(d)
    new_over = min(precise + d + 1, over + d + 2, over + 9 - d)
    precise = min(over + d + 1, precise + d)
    over = new_over
over += 1
print((min(precise, over)))

-ok_over = 1 -ok_prec = 0 +over = 1 -    new_over = min(precise + d + 1, ok_over + d + 2, ok_over + 9 - d, ok_prec + d + 1) -    new_prec = min(ok_over + d + 1, ok_prec + d) -    precise += d -    ok_over = new_over -    ok_prec = new_prec -ok_over += 1 -print((min(precise, ok_over, ok_prec))) +    new_over = min(precise + d + 1, over + d + 2, over + 9 - d) +    precise = min(over + d + 1, precise + d) +    over = new_over +over += 1 +print((min(precise, over)))