def main():

    s = input().rstrip()

    s = "0" + s

    dp = [0, float("inf")]

    for c in s:

        x = int(c)

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

    print((dp[0]))



if __name__ == "__main__":

    main()

def main():

    s = eval(input())

    dp = [0, 1]

    for c in s:

        x = int(c)

        a = dp[0] + x

        if a > dp[1] + 10 - x:

            a = dp[1] + 10 - x

        b = dp[0] + x + 1

        if b > dp[1] + 10 - x - 1:

            b = dp[1] + 10 - x - 1

        dp[0] = a

        dp[1] = b

    dp[1] += 1

    print((min(dp)))



if __name__ == "__main__":

    main()

def main():
    s = input().rstrip()
    s = "0" + s
    dp = [0, float("inf")]
    for c in s:
        x = int(c)
        dp = [
            min(dp[0] + x, dp[1] + (10 - x)),
            min(dp[0] + x + 1, dp[1] + (10 - x) - 1),
        ]
    print((dp[0]))


if __name__ == "__main__":
    main()

def main():
    s = eval(input())
    dp = [0, 1]
    for c in s:
        x = int(c)
        a = dp[0] + x
        if a > dp[1] + 10 - x:
            a = dp[1] + 10 - x
        b = dp[0] + x + 1
        if b > dp[1] + 10 - x - 1:
            b = dp[1] + 10 - x - 1
        dp[0] = a
        dp[1] = b
    dp[1] += 1
    print((min(dp)))


if __name__ == "__main__":
    main()

-    s = input().rstrip() -    s = "0" + s -    dp = [0, float("inf")] +    s = eval(input()) +    dp = [0, 1] -        dp = [ -            min(dp[0] + x, dp[1] + (10 - x)), -            min(dp[0] + x + 1, dp[1] + (10 - x) - 1), -        ] -    print((dp[0])) +        a = dp[0] + x +        if a > dp[1] + 10 - x: +            a = dp[1] + 10 - x +        b = dp[0] + x + 1 +        if b > dp[1] + 10 - x - 1: +            b = dp[1] + 10 - x - 1 +        dp[0] = a +        dp[1] = b +    dp[1] += 1 +    print((min(dp)))