def solve():
    N = input()
    
    INF = 10 ** 32
    
    dp = [[INF, INF] for i in range(len(N) + 1)]
    dp[0][0] = 0
    
    for i, x in enumerate(int(n) for n in N[::-1]):
        ni = i + 1
        dp[ni][0] = min(dp[i][0] + x, dp[i][1] + x)
        dp[ni][1] = min(dp[i][0] + 11 - x, dp[i][1] + 9 - x)
    
    ans = min(dp[-1])
    
    print(ans)

solve() def solve():
    N = input()
    
    INF = 10 ** 32
    
    dp = [[INF, INF] for i in range(len(N) + 1)]
    dp[0][0] = 0
    
    for i, x in enumerate(int(n) for n in N[::-1]):
        ni = i + 1
        dp[ni][0] = min(dp[i][0] + x, dp[i][1] + x)
        dp[ni][1] = min(dp[i][0] + 11 - x, dp[i][1] + 9 - x)
    
    ans = min(dp[-1])
    
    print(ans) solve N = input() N input() input INF = 10 ** 32 INF 10 ** 32 10 32 [INF, INF] for i in range(len(N) + 1) for i in range(len(N) + 1) i range(len(N) + 1) range len(N) + 1 len(N) len N 1 for i in range(len(N) + 1) [INF, INF] INF INF dp = [[INF, INF] for i in range(len(N) + 1)] dp [[INF, INF] for i in range(len(N) + 1)] dp[0][0] = 0 dp[0][0] [0] dp 0 0 0 for i, x in enumerate(int(n) for n in N[::-1]):
        ni = i + 1
        dp[ni][0] = min(dp[i][0] + x, dp[i][1] + x)
        dp[ni][1] = min(dp[i][0] + 11 - x, dp[i][1] + 9 - x)
    
     i x enumerate(int(n) for n in N[::-1]) enumerate int(n) int n ni = i + 1 ni i + 1 i 1 dp[ni][0] = min(dp[i][0] + x, dp[i][1] + x) dp[ni][0] [ni] dp ni 0 min(dp[i][0] + x, dp[i][1] + x) min dp[i][0] + x dp[i][0] [i] dp i 0 x dp[i][1] + x dp[i][1] [i] dp i 1 x dp[ni][1] = min(dp[i][0] + 11 - x, dp[i][1] + 9 - x) dp[ni][1] [ni] dp ni 1 min(dp[i][0] + 11 - x, dp[i][1] + 9 - x) min dp[i][0] + 11 - x dp[i][0] + 11 dp[i][0] [i] dp i 0 11 x dp[i][1] + 9 - x dp[i][1] + 9 dp[i][1] [i] dp i 1 9 x ans = min(dp[-1]) ans min(dp[-1]) min dp[-1] dp -1 print(ans) print ans solve() solve def solve():
    N = input()
    
    INF = 10 ** 32
    
    dp = [[INF, INF] for i in range(len(N) + 1)]
    dp[0][0] = 0
    
    for i, x in enumerate(int(n) for n in N[::-1]):
        ni = i + 1
        dp[ni][0] = min(dp[i][0] + x, dp[i][1] + x)
        dp[ni][1] = min(dp[i][0] + 11 - x, dp[i][1] + 9 - x)
    
    ans = min(dp[-1])
    
    print(ans) def solve():
    N = input()
    
    INF = 10 ** 32
    
    dp = [[INF, INF] for i in range(len(N) + 1)]
    dp[0][0] = 0
    
    for i, x in enumerate(int(n) for n in N[::-1]):
        ni = i + 1
        dp[ni][0] = min(dp[i][0] + x, dp[i][1] + x)
        dp[ni][1] = min(dp[i][0] + 11 - x, dp[i][1] + 9 - x)
    
    ans = min(dp[-1])
    
    print(ans) solve
def solve():
    N = input()
    
    INF = 10 ** 32
    
    dp = [[INF, INF] for i in range(len(N) + 1)]
    dp[0][0] = 0
    
    for i, x in enumerate(int(n) for n in N[::-1]):
        ni = i + 1
        dp[ni][0] = min(dp[i][0] + x, dp[i][1] + x)
        dp[ni][1] = min(dp[i][0] + 11 - x, dp[i][1] + 9 - x)
    
    ans = min(dp[-1])
    
    print(ans)

solve()