n = 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 = input() n input() input dp = [0, 10000] dp [0, 10000] 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])] d map(int, reversed(n)) map int reversed(n) reversed n dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])] dp [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])] min(d + dp[0], d + 1 + dp[1]) min d + dp[0] d dp[0] dp 0 d + 1 + dp[1] d + 1 d 1 dp[1] dp 1 min(10 - d + dp[0], 9 - d + dp[1]) min 10 - d + dp[0] 10 - d 10 d dp[0] dp 0 9 - d + dp[1] 9 - d 9 d dp[1] dp 1 print(min(dp[0], 1 + dp[1])) print min(dp[0], 1 + dp[1]) min dp[0] dp 0 1 + dp[1] 1 dp[1] dp 1 dp = [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])] [min(d + dp[0], d + 1 + dp[1]), min(10 - d + dp[0], 9 - d + dp[1])] dp n = input() input() n dp = [0, 10000] [0, 10000] dp
n = 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]))
