#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: str):
    # 下の桁から貪欲？
    N = "0" + N
    ans = 0
    kuriagari = 0
    for i in reversed(range(len(N))):
        n = int(N[i])
        if kuriagari == 1:
            kuriagari = 0
            n += 1
        if 0 <= n <= 4:
            ans += n
        elif n == 5:
            # print("上の桁を見て決める")
            k = i-1
            if 0 <= int(N[k]) <= 4:
                # print("桁上がらない")
                ans += 5
            else:
                # print("桁上がる")
                ans += 5
                kuriagari = 1
        else:
            ans += 10 - n
            kuriagari = 1
        # print("現状", ans, kuriagari)
    print(ans)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    a = next(tokens)
    N = str(a)  # type: str
    solve(N)


if __name__ == '__main__':
    main() import sys sys INF = float("inf") INF float("inf") float "inf" def solve(N: str):
    # 下の桁から貪欲？
    N = "0" + N
    ans = 0
    kuriagari = 0
    for i in reversed(range(len(N))):
        n = int(N[i])
        if kuriagari == 1:
            kuriagari = 0
            n += 1
        if 0 <= n <= 4:
            ans += n
        elif n == 5:
            # print("上の桁を見て決める")
            k = i-1
            if 0 <= int(N[k]) <= 4:
                # print("桁上がらない")
                ans += 5
            else:
                # print("桁上がる")
                ans += 5
                kuriagari = 1
        else:
            ans += 10 - n
            kuriagari = 1
        # print("現状", ans, kuriagari)
    print(ans)

    return solve N = "0" + N N "0" + N "0" N ans = 0 ans 0 kuriagari = 0 kuriagari 0 for i in reversed(range(len(N))):
        n = int(N[i])
        if kuriagari == 1:
            kuriagari = 0
            n += 1
        if 0 <= n <= 4:
            ans += n
        elif n == 5:
            # print("上の桁を見て決める")
            k = i-1
            if 0 <= int(N[k]) <= 4:
                # print("桁上がらない")
                ans += 5
            else:
                # print("桁上がる")
                ans += 5
                kuriagari = 1
        else:
            ans += 10 - n
            kuriagari = 1
        # print("現状", ans, kuriagari)
     i reversed(range(len(N))) reversed range(len(N)) range len(N) len N n = int(N[i]) n int(N[i]) int N[i] N i if kuriagari == 1:
            kuriagari = 0
            n += 1
         kuriagari == 1 kuriagari 1 kuriagari = 0 kuriagari 0 n += 1 n 1 if 0 <= n <= 4:
            ans += n
        elif n == 5:
            # print("上の桁を見て決める")
            k = i-1
            if 0 <= int(N[k]) <= 4:
                # print("桁上がらない")
                ans += 5
            else:
                # print("桁上がる")
                ans += 5
                kuriagari = 1
        else:
            ans += 10 - n
            kuriagari = 1
        # print("現状", ans, kuriagari)
     0 <= n <= 4 0 <= n 0 n 4 ans += n ans n elif n == 5:
            # print("上の桁を見て決める")
            k = i-1
            if 0 <= int(N[k]) <= 4:
                # print("桁上がらない")
                ans += 5
            else:
                # print("桁上がる")
                ans += 5
                kuriagari = 1
         n == 5 n 5 k = i-1 k i-1 i 1 if 0 <= int(N[k]) <= 4:
                # print("桁上がらない")
                ans += 5
            else:
                # print("桁上がる")
                ans += 5
                kuriagari = 1
         0 <= int(N[k]) <= 4 0 <= int(N[k]) 0 int(N[k]) int N[k] N k 4 ans += 5 ans 5 ans += 5 ans 5 kuriagari = 1 kuriagari 1 ans += 10 - n ans 10 - n 10 n kuriagari = 1 kuriagari 1 print(ans) print ans return N: str N def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    a = next(tokens)
    N = str(a)  # type: str
    solve(N) main def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
     iterate_tokens for line in sys.stdin:
            for word in line.split():
                yield word
     line sys.stdin sys stdin for word in line.split():
                yield word
     word line.split() line.split line split yield word word tokens = iterate_tokens() tokens iterate_tokens() iterate_tokens a = next(tokens) a next(tokens) next tokens N = str(a) N str(a) str a solve(N) solve N if __name__ == '__main__':
    main() __name__ == '__main__' __name__ '__main__' main() main def solve(N: str):
    # 下の桁から貪欲？
    N = "0" + N
    ans = 0
    kuriagari = 0
    for i in reversed(range(len(N))):
        n = int(N[i])
        if kuriagari == 1:
            kuriagari = 0
            n += 1
        if 0 <= n <= 4:
            ans += n
        elif n == 5:
            # print("上の桁を見て決める")
            k = i-1
            if 0 <= int(N[k]) <= 4:
                # print("桁上がらない")
                ans += 5
            else:
                # print("桁上がる")
                ans += 5
                kuriagari = 1
        else:
            ans += 10 - n
            kuriagari = 1
        # print("現状", ans, kuriagari)
    print(ans)

    return def solve(N: str):
    # 下の桁から貪欲？
    N = "0" + N
    ans = 0
    kuriagari = 0
    for i in reversed(range(len(N))):
        n = int(N[i])
        if kuriagari == 1:
            kuriagari = 0
            n += 1
        if 0 <= n <= 4:
            ans += n
        elif n == 5:
            # print("上の桁を見て決める")
            k = i-1
            if 0 <= int(N[k]) <= 4:
                # print("桁上がらない")
                ans += 5
            else:
                # print("桁上がる")
                ans += 5
                kuriagari = 1
        else:
            ans += 10 - n
            kuriagari = 1
        # print("現状", ans, kuriagari)
    print(ans)

    return solve INF = float("inf") float("inf") INF def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    a = next(tokens)
    N = str(a)  # type: str
    solve(N) def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    a = next(tokens)
    N = str(a)  # type: str
    solve(N) main
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: str):
    # 下の桁から貪欲？
    N = "0" + N
    ans = 0
    kuriagari = 0
    for i in reversed(range(len(N))):
        n = int(N[i])
        if kuriagari == 1:
            kuriagari = 0
            n += 1
        if 0 <= n <= 4:
            ans += n
        elif n == 5:
            # print("上の桁を見て決める")
            k = i-1
            if 0 <= int(N[k]) <= 4:
                # print("桁上がらない")
                ans += 5
            else:
                # print("桁上がる")
                ans += 5
                kuriagari = 1
        else:
            ans += 10 - n
            kuriagari = 1
        # print("現状", ans, kuriagari)
    print(ans)

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    a = next(tokens)
    N = str(a)  # type: str
    solve(N)


if __name__ == '__main__':
    main()
