dp = [0, 1]

s = input()
s = s[::-1]
s += "0"

for i in range(len(s)):
    dp0, dp1 = dp
    dp[0] = min(dp0, dp1 + 1) + int(s[i])
    dp[1] = min(dp0, dp1 - 1) + 10 - int(s[i])

print(min(dp)) dp = [0, 1] dp [0, 1] 0 1 s = input() s input() input s = s[::-1] s s[::-1] s ::-1 -1 s += "0" s "0" for i in range(len(s)):
    dp0, dp1 = dp
    dp[0] = min(dp0, dp1 + 1) + int(s[i])
    dp[1] = min(dp0, dp1 - 1) + 10 - int(s[i]) i range(len(s)) range len(s) len s dp0, dp1 = dp dp0 dp dp1 dp[0] = min(dp0, dp1 + 1) + int(s[i]) dp[0] dp 0 min(dp0, dp1 + 1) + int(s[i]) min(dp0, dp1 + 1) min dp0 dp1 + 1 dp1 1 int(s[i]) int s[i] s i dp[1] = min(dp0, dp1 - 1) + 10 - int(s[i]) dp[1] dp 1 min(dp0, dp1 - 1) + 10 - int(s[i]) min(dp0, dp1 - 1) + 10 min(dp0, dp1 - 1) min dp0 dp1 - 1 dp1 1 10 int(s[i]) int s[i] s i print(min(dp)) print min(dp) min dp dp0, dp1 = dp dp dp0 dp = [0, 1] [0, 1] dp s = s[::-1] s[::-1] s s += "0" "0" s s = input() input() s dp1 = dp dp dp1
dp = [0, 1]

s = input()
s = s[::-1]
s += "0"

for i in range(len(s)):
    dp0, dp1 = dp
    dp[0] = min(dp0, dp1 + 1) + int(s[i])
    dp[1] = min(dp0, dp1 - 1) + 10 - int(s[i])

print(min(dp))