n = 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 = input() n input() input precise = 0 precise 0 over = 1 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 d n d = int(d) d int(d) int d new_over = min(precise + d + 1, over + d + 2, over + 9 - d) new_over min(precise + d + 1, over + d + 2, over + 9 - d) min precise + d + 1 precise + d precise d 1 over + d + 2 over + d over d 2 over + 9 - d over + 9 over 9 d precise = min(over + d + 1, precise + d) precise min(over + d + 1, precise + d) min over + d + 1 over + d over d 1 precise + d precise d over = new_over over new_over over += 1 over 1 print(min(precise, over)) print min(precise, over) min precise over d = int(d) int(d) d over += 1 1 over precise = min(over + d + 1, precise + d) min(over + d + 1, precise + d) precise over = new_over new_over over n = input() input() n new_over = min(precise + d + 1, over + d + 2, over + 9 - d) min(precise + d + 1, over + d + 2, over + 9 - d) new_over over = 1 1 over precise = 0 0 precise
n = 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))
