def main():

    a = list(map(int, list(eval(input()))))

    a.reverse()

    n = len(a)

    a += [0]

    b = [i for i in a]

    cnt = 0

    for i in range(len(a)):

        if b[i] > 5 or (b[i] == 5 and b[i+1] >= 5):

            b[i] = 0

            b[i+1] += 1

    cnt = sum(b)

    for i in range(n):

        if a[i] <= b[i]:

            cnt += b[i]-a[i]

        else:

            b[i] += 10

            b[i+1] -= 1

            cnt += b[i]-a[i]

    print(cnt)





main()
def main():

    a = list(map(int, list(eval(input()))))

    n = len(a)

    dp = [[0, 0] for _ in [0]*(n+1)]

    dp[0][1] = 1

    for i in range(n):

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

        dp[i+1][1] = min(dp[i][0]+a[i]+1, dp[i][1]+9-a[i])

    print((dp[n][0]))



main()

def main():
    a = list(map(int, list(eval(input()))))
    a.reverse()
    n = len(a)
    a += [0]
    b = [i for i in a]
    cnt = 0
    for i in range(len(a)):
        if b[i] > 5 or (b[i] == 5 and b[i + 1] >= 5):
            b[i] = 0
            b[i + 1] += 1
    cnt = sum(b)
    for i in range(n):
        if a[i] <= b[i]:
            cnt += b[i] - a[i]
        else:
            b[i] += 10
            b[i + 1] -= 1
            cnt += b[i] - a[i]
    print(cnt)


main()

def main():
    a = list(map(int, list(eval(input()))))
    n = len(a)
    dp = [[0, 0] for _ in [0] * (n + 1)]
    dp[0][1] = 1
    for i in range(n):
        dp[i + 1][0] = min(dp[i][0] + a[i], dp[i][1] + 10 - a[i])
        dp[i + 1][1] = min(dp[i][0] + a[i] + 1, dp[i][1] + 9 - a[i])
    print((dp[n][0]))


main()

-    a.reverse() -    a += [0] -    b = [i for i in a] -    cnt = 0 -    for i in range(len(a)): -        if b[i] > 5 or (b[i] == 5 and b[i + 1] >= 5): -            b[i] = 0 -            b[i + 1] += 1 -    cnt = sum(b) +    dp = [[0, 0] for _ in [0] * (n + 1)] +    dp[0][1] = 1 -        if a[i] <= b[i]: -            cnt += b[i] - a[i] -        else: -            b[i] += 10 -            b[i + 1] -= 1 -            cnt += b[i] - a[i] -    print(cnt) +        dp[i + 1][0] = min(dp[i][0] + a[i], dp[i][1] + 10 - a[i]) +        dp[i + 1][1] = min(dp[i][0] + a[i] + 1, dp[i][1] + 9 - a[i]) +    print((dp[n][0]))