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

def resolve():
    S = "0" + 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()
