#!python3



iim = lambda: list(map(int, input().rstrip().split()))



def resolve():

    n = [int(c) for c in "0" + eval(input())]

    I = len(n)

    ans = 0

    for i in range(I-1, -1, -1):

        b = n[i]

        if b < 5:

            ans += b

        elif b == 5:

            for j in range(2):

                c = n[i-j-1]

                cn = 5 - j

                if c < cn:

                    ans += b

                    break

                elif c > cn:

                    ans += b

                    n[i-1] += 1

                    break

            else:

                ans += b

                n[i-1] += 1

        else:

            ans += 10 - b

            n[i-1] += 1

    print(ans)



if __name__ == "__main__":

    resolve()

#!python3



iim = lambda: list(map(int, input().rstrip().split()))



def resolve():

    n = [int(c) for c in "0" + eval(input())]

    I = len(n)

    ans = 0

    for i in range(I-1, -1, -1):

        b = n[i]

        if b < 5:

            ans += b

        elif b == 5:

            c = n[i-1]

            ans += b

            if c >= 5:

                n[i-1] += 1

        else:

            ans += 10 - b

            n[i-1] += 1

    print(ans)



if __name__ == "__main__":

    resolve()

#!python3
iim = lambda: list(map(int, input().rstrip().split()))


def resolve():
    n = [int(c) for c in "0" + eval(input())]
    I = len(n)
    ans = 0
    for i in range(I - 1, -1, -1):
        b = n[i]
        if b < 5:
            ans += b
        elif b == 5:
            for j in range(2):
                c = n[i - j - 1]
                cn = 5 - j
                if c < cn:
                    ans += b
                    break
                elif c > cn:
                    ans += b
                    n[i - 1] += 1
                    break
            else:
                ans += b
                n[i - 1] += 1
        else:
            ans += 10 - b
            n[i - 1] += 1
    print(ans)


if __name__ == "__main__":
    resolve()

#!python3
iim = lambda: list(map(int, input().rstrip().split()))


def resolve():
    n = [int(c) for c in "0" + eval(input())]
    I = len(n)
    ans = 0
    for i in range(I - 1, -1, -1):
        b = n[i]
        if b < 5:
            ans += b
        elif b == 5:
            c = n[i - 1]
            ans += b
            if c >= 5:
                n[i - 1] += 1
        else:
            ans += 10 - b
            n[i - 1] += 1
    print(ans)


if __name__ == "__main__":
    resolve()

-            for j in range(2): -                c = n[i - j - 1] -                cn = 5 - j -                if c < cn: -                    ans += b -                    break -                elif c > cn: -                    ans += b -                    n[i - 1] += 1 -                    break -            else: -                ans += b +            c = n[i - 1] +            ans += b +            if c >= 5: