N = [int(s) for s in input()]
M = len(N)

eq = 0
ov = 1

for n in N:
    a = min(eq + n, ov + 10 - n)
    b = min(eq + (n + 1), ov + 10 - (n + 1))
    eq, ov = a, b

print(eq) int(s) for s in input() for s in input() s input() input for s in input() int(s) int s N = [int(s) for s in input()] N [int(s) for s in input()] M = len(N) M len(N) len N eq = 0 eq 0 ov = 1 ov 1 for n in N:
    a = min(eq + n, ov + 10 - n)
    b = min(eq + (n + 1), ov + 10 - (n + 1))
    eq, ov = a, b n N a = min(eq + n, ov + 10 - n) a min(eq + n, ov + 10 - n) min eq + n eq n ov + 10 - n ov + 10 ov 10 n b = min(eq + (n + 1), ov + 10 - (n + 1)) b min(eq + (n + 1), ov + 10 - (n + 1)) min eq + (n + 1) eq n + 1 n 1 ov + 10 - (n + 1) ov + 10 ov 10 n + 1 n 1 eq, ov = a, b eq a ov b print(eq) print eq M = len(N) len(N) M b = min(eq + (n + 1), ov + 10 - (n + 1)) min(eq + (n + 1), ov + 10 - (n + 1)) b eq = 0 0 eq N = [int(s) for s in input()] [int(s) for s in input()] N ov = a, b b ov ov = 1 1 ov eq, ov = a, b a eq a = min(eq + n, ov + 10 - n) min(eq + n, ov + 10 - n) a
N = [int(s) for s in input()]
M = len(N)

eq = 0
ov = 1

for n in N:
    a = min(eq + n, ov + 10 - n)
    b = min(eq + (n + 1), ov + 10 - (n + 1))
    eq, ov = a, b

print(eq)
