
def resolve():
    S = input()
    n = len(S)
    dp = [[0] * 2 for _ in range(n + 1)]
    dp[0][1] = 1
    for i in range(1, n + 1):
        d = int(S[i - 1])
        dp[i][0] = min(dp[i - 1][0] + d, dp[i - 1][1] + (10 - d))
        dp[i][1] = min(dp[i - 1][0] + (d + 1), dp[i - 1][1] + (10 - 1 - d))
    print(dp[n][0])


if __name__ == "__main__":
    resolve() def resolve():
    S = input()
    n = len(S)
    dp = [[0] * 2 for _ in range(n + 1)]
    dp[0][1] = 1
    for i in range(1, n + 1):
        d = int(S[i - 1])
        dp[i][0] = min(dp[i - 1][0] + d, dp[i - 1][1] + (10 - d))
        dp[i][1] = min(dp[i - 1][0] + (d + 1), dp[i - 1][1] + (10 - 1 - d))
    print(dp[n][0]) resolve S = input() S input() input n = len(S) n len(S) len S [0] * 2 for _ in range(n + 1) for _ in range(n + 1) _ range(n + 1) range n + 1 n 1 for _ in range(n + 1) [0] * 2 [0] 0 2 dp = [[0] * 2 for _ in range(n + 1)] dp [[0] * 2 for _ in range(n + 1)] dp[0][1] = 1 dp[0][1] [0] dp 0 1 1 for i in range(1, n + 1):
        d = int(S[i - 1])
        dp[i][0] = min(dp[i - 1][0] + d, dp[i - 1][1] + (10 - d))
        dp[i][1] = min(dp[i - 1][0] + (d + 1), dp[i - 1][1] + (10 - 1 - d))
     i range(1, n + 1) range 1 n + 1 n 1 d = int(S[i - 1]) d int(S[i - 1]) int S[i - 1] S i - 1 i 1 dp[i][0] = min(dp[i - 1][0] + d, dp[i - 1][1] + (10 - d)) dp[i][0] [i] dp i 0 min(dp[i - 1][0] + d, dp[i - 1][1] + (10 - d)) min dp[i - 1][0] + d dp[i - 1][0] [i - 1] dp i - 1 i 1 0 d dp[i - 1][1] + (10 - d) dp[i - 1][1] [i - 1] dp i - 1 i 1 1 10 - d 10 d dp[i][1] = min(dp[i - 1][0] + (d + 1), dp[i - 1][1] + (10 - 1 - d)) dp[i][1] [i] dp i 1 min(dp[i - 1][0] + (d + 1), dp[i - 1][1] + (10 - 1 - d)) min dp[i - 1][0] + (d + 1) dp[i - 1][0] [i - 1] dp i - 1 i 1 0 d + 1 d 1 dp[i - 1][1] + (10 - 1 - d) dp[i - 1][1] [i - 1] dp i - 1 i 1 1 10 - 1 - d 10 - 1 10 1 d print(dp[n][0]) print dp[n][0] [n] dp n 0 if __name__ == "__main__":
    resolve() __name__ == "__main__" __name__ "__main__" resolve() resolve def resolve():
    S = input()
    n = len(S)
    dp = [[0] * 2 for _ in range(n + 1)]
    dp[0][1] = 1
    for i in range(1, n + 1):
        d = int(S[i - 1])
        dp[i][0] = min(dp[i - 1][0] + d, dp[i - 1][1] + (10 - d))
        dp[i][1] = min(dp[i - 1][0] + (d + 1), dp[i - 1][1] + (10 - 1 - d))
    print(dp[n][0]) def resolve():
    S = input()
    n = len(S)
    dp = [[0] * 2 for _ in range(n + 1)]
    dp[0][1] = 1
    for i in range(1, n + 1):
        d = int(S[i - 1])
        dp[i][0] = min(dp[i - 1][0] + d, dp[i - 1][1] + (10 - d))
        dp[i][1] = min(dp[i - 1][0] + (d + 1), dp[i - 1][1] + (10 - 1 - d))
    print(dp[n][0]) resolve

def resolve():
    S = input()
    n = len(S)
    dp = [[0] * 2 for _ in range(n + 1)]
    dp[0][1] = 1
    for i in range(1, n + 1):
        d = int(S[i - 1])
        dp[i][0] = min(dp[i - 1][0] + d, dp[i - 1][1] + (10 - d))
        dp[i][1] = min(dp[i - 1][0] + (d + 1), dp[i - 1][1] + (10 - 1 - d))
    print(dp[n][0])


if __name__ == "__main__":
    resolve()
