digits = [0] + [int(c) for c in input()]

dp = [0, 1]
for digit in reversed(digits):
    dp0, dp1 = dp
    dp[0] = min(dp0, dp1 + 1) + digit
    dp[1] = min(dp0, dp1 - 1) + 10 - digit

print(min(dp)) int(c) for c in input() for c in input() c input() input for c in input() int(c) int c digits = [0] + [int(c) for c in input()] digits [0] + [int(c) for c in input()] [0] 0 [int(c) for c in input()] dp = [0, 1] dp [0, 1] 0 1 for digit in reversed(digits):
    dp0, dp1 = dp
    dp[0] = min(dp0, dp1 + 1) + digit
    dp[1] = min(dp0, dp1 - 1) + 10 - digit digit reversed(digits) reversed digits dp0, dp1 = dp dp0 dp dp1 dp[0] = min(dp0, dp1 + 1) + digit dp[0] dp 0 min(dp0, dp1 + 1) + digit min(dp0, dp1 + 1) min dp0 dp1 + 1 dp1 1 digit dp[1] = min(dp0, dp1 - 1) + 10 - digit dp[1] dp 1 min(dp0, dp1 - 1) + 10 - digit min(dp0, dp1 - 1) + 10 min(dp0, dp1 - 1) min dp0 dp1 - 1 dp1 1 10 digit print(min(dp)) print min(dp) min dp dp = [0, 1] [0, 1] dp dp0, dp1 = dp dp dp0 digits = [0] + [int(c) for c in input()] [0] + [int(c) for c in input()] digits dp1 = dp dp dp1
digits = [0] + [int(c) for c in input()]

dp = [0, 1]
for digit in reversed(digits):
    dp0, dp1 = dp
    dp[0] = min(dp0, dp1 + 1) + digit
    dp[1] = min(dp0, dp1 - 1) + 10 - digit

print(min(dp))