s = input()
s = s[::-1]
s += '0'
ans = 0
dp = [0, 10**9]
for c in s:
    i = int(c)
    a = dp[0]+i
    if i<9 and a>dp[1]+i+1:
        a = dp[1]+i+1
    b = dp[1]+9-i
    if i>0 and b>dp[0]+10-i:
        b = dp[0]+10-i
    dp = [a, b]
print(dp[0]) s = input() s input() input s = s[::-1] s s[::-1] s ::-1 -1 s += '0' s '0' ans = 0 ans 0 dp = [0, 10**9] dp [0, 10**9] 0 10**9 10 9 for c in s:
    i = int(c)
    a = dp[0]+i
    if i<9 and a>dp[1]+i+1:
        a = dp[1]+i+1
    b = dp[1]+9-i
    if i>0 and b>dp[0]+10-i:
        b = dp[0]+10-i
    dp = [a, b] c s i = int(c) i int(c) int c a = dp[0]+i a dp[0]+i dp[0] dp 0 i if i<9 and a>dp[1]+i+1:
        a = dp[1]+i+1
     i<9 and a>dp[1]+i+1 i<9 i 9 a>dp[1]+i+1 a dp[1]+i+1 dp[1]+i dp[1] dp 1 i 1 a = dp[1]+i+1 a dp[1]+i+1 dp[1]+i dp[1] dp 1 i 1 b = dp[1]+9-i b dp[1]+9-i dp[1]+9 dp[1] dp 1 9 i if i>0 and b>dp[0]+10-i:
        b = dp[0]+10-i
     i>0 and b>dp[0]+10-i i>0 i 0 b>dp[0]+10-i b dp[0]+10-i dp[0]+10 dp[0] dp 0 10 i b = dp[0]+10-i b dp[0]+10-i dp[0]+10 dp[0] dp 0 10 i dp = [a, b] dp [a, b] a b print(dp[0]) print dp[0] dp 0 s = input() input() s s += '0' '0' s a = dp[0]+i dp[0]+i a dp = [0, 10**9] [0, 10**9] dp ans = 0 0 ans s = s[::-1] s[::-1] s a = dp[1]+i+1 dp[1]+i+1 a dp = [a, b] [a, b] dp b = dp[1]+9-i dp[1]+9-i b i = int(c) int(c) i b = dp[0]+10-i dp[0]+10-i b
s = input()
s = s[::-1]
s += '0'
ans = 0
dp = [0, 10**9]
for c in s:
    i = int(c)
    a = dp[0]+i
    if i<9 and a>dp[1]+i+1:
        a = dp[1]+i+1
    b = dp[1]+9-i
    if i>0 and b>dp[0]+10-i:
        b = dp[0]+10-i
    dp = [a, b]
print(dp[0])