

def resolve():

    S = eval(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 = "0" + eval(input())

    N = len(S)



    dp = [[1<<60]*2 for _ in range(N+1)]

    dp[0][0] = 0

    for i in range(N):

        now = int(S[i])

        dp[i+1][0] = min(dp[i][0]+now, dp[i][1]+(10- now))

        dp[i + 1][1] = min(dp[i][0] + now + 1, dp[i][1] + (10 - now - 1))

    print((dp[N][0]))





if __name__ == "__main__":

    resolve()

def resolve():
    S = eval(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 = "0" + eval(input())
    N = len(S)
    dp = [[1 << 60] * 2 for _ in range(N + 1)]
    dp[0][0] = 0
    for i in range(N):
        now = int(S[i])
        dp[i + 1][0] = min(dp[i][0] + now, dp[i][1] + (10 - now))
        dp[i + 1][1] = min(dp[i][0] + now + 1, dp[i][1] + (10 - now - 1))
    print((dp[N][0]))


if __name__ == "__main__":
    resolve()

-    S = eval(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])) +    S = "0" + eval(input()) +    N = len(S) +    dp = [[1 << 60] * 2 for _ in range(N + 1)] +    dp[0][0] = 0 +    for i in range(N): +        now = int(S[i]) +        dp[i + 1][0] = min(dp[i][0] + now, dp[i][1] + (10 - now)) +        dp[i + 1][1] = min(dp[i][0] + now + 1, dp[i][1] + (10 - now - 1)) +    print((dp[N][0]))