N = input()

over = 1
eq = 0

for n in map(int, N):
    o = min(eq + n + 1, over + (10 - n) - 1)
    e = min(eq + n, over + (10 - n))

    over = o
    eq = e

print(eq) N = input() N input() input over = 1 over 1 eq = 0 eq 0 for n in map(int, N):
    o = min(eq + n + 1, over + (10 - n) - 1)
    e = min(eq + n, over + (10 - n))

    over = o
    eq = e n map(int, N) map int N o = min(eq + n + 1, over + (10 - n) - 1) o min(eq + n + 1, over + (10 - n) - 1) min eq + n + 1 eq + n eq n 1 over + (10 - n) - 1 over + (10 - n) over 10 - n 10 n 1 e = min(eq + n, over + (10 - n)) e min(eq + n, over + (10 - n)) min eq + n eq n over + (10 - n) over 10 - n 10 n over = o over o eq = e eq e print(eq) print eq o = min(eq + n + 1, over + (10 - n) - 1) min(eq + n + 1, over + (10 - n) - 1) o over = o o over N = input() input() N eq = e e eq e = min(eq + n, over + (10 - n)) min(eq + n, over + (10 - n)) e over = 1 1 over eq = 0 0 eq
N = input()

over = 1
eq = 0

for n in map(int, N):
    o = min(eq + n + 1, over + (10 - n) - 1)
    e = min(eq + n, over + (10 - n))

    over = o
    eq = e

print(eq)
