n = eval(input())

dp = [0, 10000]

for d in reversed('0' + n):

    newdp = []

    newdp.append(min(int(d) + dp[0], int(d) + 1 + dp[1]))

    newdp.append(min(10 - int(d) + dp[0], 10 - int(d) - 1 + dp[1]))

    dp = newdp

print((dp[0]))

n = eval(input())

dp = [0, 10000]

for d in map(int, reversed(n)):

    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]

print((min(dp[0], 1 + dp[1])))

n = eval(input())
dp = [0, 10000]
for d in reversed("0" + n):
    newdp = []
    newdp.append(min(int(d) + dp[0], int(d) + 1 + dp[1]))
    newdp.append(min(10 - int(d) + dp[0], 10 - int(d) - 1 + dp[1]))
    dp = newdp
print((dp[0]))

n = eval(input())
dp = [0, 10000]
for d in map(int, reversed(n)):
    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])]
print((min(dp[0], 1 + dp[1])))

-for d in reversed("0" + n): -    newdp = [] -    newdp.append(min(int(d) + dp[0], int(d) + 1 + dp[1])) -    newdp.append(min(10 - int(d) + dp[0], 10 - int(d) - 1 + dp[1])) -    dp = newdp -print((dp[0])) +for d in map(int, reversed(n)): +    dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])] +print((min(dp[0], 1 + dp[1])))