s = input()
s = '0' + s[::-1]
dp = [[0, 0] for _ in range(len(s) + 1)]
for i in range(len(s)):
    for j in range(2):
        if j == 0: #ぴったり
            dp[i + 1][j] = min(dp[i]) + int(s[i])
        else: #1枚多めに払う
            dp[i + 1][j] = min(dp[i][1] + 9 - int(s[i]), dp[i][0] + 11 - int(s[i]))

print(min(dp[-1])) s = input() s input() input s = '0' + s[::-1] s '0' + s[::-1] '0' s[::-1] s ::-1 -1 [0, 0] for _ in range(len(s) + 1) for _ in range(len(s) + 1) _ range(len(s) + 1) range len(s) + 1 len(s) len s 1 for _ in range(len(s) + 1) [0, 0] 0 0 dp = [[0, 0] for _ in range(len(s) + 1)] dp [[0, 0] for _ in range(len(s) + 1)] for i in range(len(s)):
    for j in range(2):
        if j == 0: #ぴったり
            dp[i + 1][j] = min(dp[i]) + int(s[i])
        else: #1枚多めに払う
            dp[i + 1][j] = min(dp[i][1] + 9 - int(s[i]), dp[i][0] + 11 - int(s[i])) i range(len(s)) range len(s) len s for j in range(2):
        if j == 0: #ぴったり
            dp[i + 1][j] = min(dp[i]) + int(s[i])
        else: #1枚多めに払う
            dp[i + 1][j] = min(dp[i][1] + 9 - int(s[i]), dp[i][0] + 11 - int(s[i])) j range(2) range 2 if j == 0: #ぴったり
            dp[i + 1][j] = min(dp[i]) + int(s[i])
        else: #1枚多めに払う
            dp[i + 1][j] = min(dp[i][1] + 9 - int(s[i]), dp[i][0] + 11 - int(s[i])) j == 0 j 0 dp[i + 1][j] = min(dp[i]) + int(s[i]) dp[i + 1][j] [i + 1] dp i + 1 i 1 j min(dp[i]) + int(s[i]) min(dp[i]) min dp[i] dp i int(s[i]) int s[i] s i dp[i + 1][j] = min(dp[i][1] + 9 - int(s[i]), dp[i][0] + 11 - int(s[i])) dp[i + 1][j] [i + 1] dp i + 1 i 1 j min(dp[i][1] + 9 - int(s[i]), dp[i][0] + 11 - int(s[i])) min dp[i][1] + 9 - int(s[i]) dp[i][1] + 9 dp[i][1] [i] dp i 1 9 int(s[i]) int s[i] s i dp[i][0] + 11 - int(s[i]) dp[i][0] + 11 dp[i][0] [i] dp i 0 11 int(s[i]) int s[i] s i print(min(dp[-1])) print min(dp[-1]) min dp[-1] dp -1 s = input() input() s s = '0' + s[::-1] '0' + s[::-1] s dp = [[0, 0] for _ in range(len(s) + 1)] [[0, 0] for _ in range(len(s) + 1)] dp
s = input()
s = '0' + s[::-1]
dp = [[0, 0] for _ in range(len(s) + 1)]
for i in range(len(s)):
    for j in range(2):
        if j == 0: #ぴったり
            dp[i + 1][j] = min(dp[i]) + int(s[i])
        else: #1枚多めに払う
            dp[i + 1][j] = min(dp[i][1] + 9 - int(s[i]), dp[i][0] + 11 - int(s[i]))

print(min(dp[-1]))