s = list(input())
s.reverse()
s.append('0')

dp = [[1000000000] * 2 for _ in range(1000005)]
dp[0][0] = 0
n = len(s)
for i in range(n):
    for j in range(2):
        x = int(s[i])
        x += j
        if x < 10:
            dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + x)
        if x > 0:
            dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + (10 - x))

ans = dp[n][0]
print(ans) s = list(input()) s list(input()) list input() input s.reverse() s.reverse s reverse s.append('0') s.append s append '0' [1000000000] * 2 for _ in range(1000005) for _ in range(1000005) _ range(1000005) range 1000005 for _ in range(1000005) [1000000000] * 2 [1000000000] 1000000000 2 dp = [[1000000000] * 2 for _ in range(1000005)] dp [[1000000000] * 2 for _ in range(1000005)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 n = len(s) n len(s) len s for i in range(n):
    for j in range(2):
        x = int(s[i])
        x += j
        if x < 10:
            dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + x)
        if x > 0:
            dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + (10 - x)) i range(n) range n for j in range(2):
        x = int(s[i])
        x += j
        if x < 10:
            dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + x)
        if x > 0:
            dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + (10 - x)) j range(2) range 2 x = int(s[i]) x int(s[i]) int s[i] s i x += j x j if x < 10:
            dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + x)
         x < 10 x 10 dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + x) dp[i + 1][0] [i + 1] dp i + 1 i 1 0 min(dp[i + 1][0], dp[i][j] + x) min dp[i + 1][0] [i + 1] dp i + 1 i 1 0 dp[i][j] + x dp[i][j] [i] dp i j x if x > 0:
            dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + (10 - x)) x > 0 x 0 dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + (10 - x)) dp[i + 1][1] [i + 1] dp i + 1 i 1 1 min(dp[i + 1][1], dp[i][j] + (10 - x)) min dp[i + 1][1] [i + 1] dp i + 1 i 1 1 dp[i][j] + (10 - x) dp[i][j] [i] dp i j 10 - x 10 x ans = dp[n][0] ans dp[n][0] [n] dp n 0 print(ans) print ans s = list(input()) list(input()) s x += j j x ans = dp[n][0] dp[n][0] ans dp = [[1000000000] * 2 for _ in range(1000005)] [[1000000000] * 2 for _ in range(1000005)] dp x = int(s[i]) int(s[i]) x n = len(s) len(s) n
s = list(input())
s.reverse()
s.append('0')

dp = [[1000000000] * 2 for _ in range(1000005)]
dp[0][0] = 0
n = len(s)
for i in range(n):
    for j in range(2):
        x = int(s[i])
        x += j
        if x < 10:
            dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + x)
        if x > 0:
            dp[i + 1][1] = min(dp[i + 1][1], dp[i][j] + (10 - x))

ans = dp[n][0]
print(ans)