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

k = len(n)

dp1, dp2 = [0] * k, [0] * k

dp1[0], dp2[0] = n[0], 11-n[0]

for i, x in enumerate(n[1:], 1):

    dp1[i] = min(x+dp1[i-1], x+dp2[i-1])

    dp2[i] = min(11-x+dp1[i-1], 9-x+dp2[i-1])

print((min(dp1[-1], dp2[-1])))
N = list(map(int, eval(input())))[::-1]

a, b = N[0], 11-N[0]

for n in N[1:]:

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

print((min(a, b)))
n = list(reversed(list(map(int, eval(input())))))
k = len(n)
dp1, dp2 = [0] * k, [0] * k
dp1[0], dp2[0] = n[0], 11 - n[0]
for i, x in enumerate(n[1:], 1):
    dp1[i] = min(x + dp1[i - 1], x + dp2[i - 1])
    dp2[i] = min(11 - x + dp1[i - 1], 9 - x + dp2[i - 1])
print((min(dp1[-1], dp2[-1])))

N = list(map(int, eval(input())))[::-1]
a, b = N[0], 11 - N[0]
for n in N[1:]:
    a, b = min(n + a, n + b), min(11 - n + a, 9 - n + b)
print((min(a, b)))

-n = list(reversed(list(map(int, eval(input()))))) -k = len(n) -dp1, dp2 = [0] * k, [0] * k -dp1[0], dp2[0] = n[0], 11 - n[0] -for i, x in enumerate(n[1:], 1): -    dp1[i] = min(x + dp1[i - 1], x + dp2[i - 1]) -    dp2[i] = min(11 - x + dp1[i - 1], 9 - x + dp2[i - 1]) -print((min(dp1[-1], dp2[-1]))) +N = list(map(int, eval(input())))[::-1] +a, b = N[0], 11 - N[0] +for n in N[1:]: +    a, b = min(n + a, n + b), min(11 - n + a, 9 - n + b) +print((min(a, b)))