# coding: utf-8


def solve(*args: str) -> str:
    N = list(map(int, args[0]))

    a, b = 0, 1
    for n in N:
        a, b = min(b+(10-n), a+n), min(b+(9-n), a+n+1)
    return str(a)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines()))) def solve(*args: str) -> str:
    N = list(map(int, args[0]))

    a, b = 0, 1
    for n in N:
        a, b = min(b+(10-n), a+n), min(b+(9-n), a+n+1)
    return str(a) solve N = list(map(int, args[0])) N list(map(int, args[0])) list map(int, args[0]) map int args[0] args 0 a, b = 0, 1 a 0 b 1 for n in N:
        a, b = min(b+(10-n), a+n), min(b+(9-n), a+n+1)
     n N a, b = min(b+(10-n), a+n), min(b+(9-n), a+n+1) a min(b+(10-n), a+n) min b+(10-n) b 10-n 10 n a+n a n b min(b+(9-n), a+n+1) min b+(9-n) b 9-n 9 n a+n+1 a+n a n 1 return str(a) str(a) str a *args: str if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines()))) __name__ == "__main__" __name__ "__main__" print(solve(*(open(0).read().splitlines()))) print solve(*(open(0).read().splitlines())) solve *(open(0).read().splitlines()) open(0).read().splitlines() (0).read().splitlines (0).read() (0).read (0) open 0 read splitlines def solve(*args: str) -> str:
    N = list(map(int, args[0]))

    a, b = 0, 1
    for n in N:
        a, b = min(b+(10-n), a+n), min(b+(9-n), a+n+1)
    return str(a) def solve(*args: str) -> str:
    N = list(map(int, args[0]))

    a, b = 0, 1
    for n in N:
        a, b = min(b+(10-n), a+n), min(b+(9-n), a+n+1)
    return str(a) solve
# coding: utf-8


def solve(*args: str) -> str:
    N = list(map(int, args[0]))

    a, b = 0, 1
    for n in N:
        a, b = min(b+(10-n), a+n), min(b+(9-n), a+n+1)
    return str(a)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))
