def main():
    n = input()
    a, b = 0, 1
    for i in n:
        x = int(i)
        a, b = min(a + x, b + 10 - x), min(a + x + 1, b + 10 - x - 1)
    print(a)

main() def main():
    n = input()
    a, b = 0, 1
    for i in n:
        x = int(i)
        a, b = min(a + x, b + 10 - x), min(a + x + 1, b + 10 - x - 1)
    print(a) main n = input() n input() input a, b = 0, 1 a 0 b 1 for i in n:
        x = int(i)
        a, b = min(a + x, b + 10 - x), min(a + x + 1, b + 10 - x - 1)
     i n x = int(i) x int(i) int i a, b = min(a + x, b + 10 - x), min(a + x + 1, b + 10 - x - 1) a min(a + x, b + 10 - x) min a + x a x b + 10 - x b + 10 b 10 x b min(a + x + 1, b + 10 - x - 1) min a + x + 1 a + x a x 1 b + 10 - x - 1 b + 10 - x b + 10 b 10 x 1 print(a) print a main() main def main():
    n = input()
    a, b = 0, 1
    for i in n:
        x = int(i)
        a, b = min(a + x, b + 10 - x), min(a + x + 1, b + 10 - x - 1)
    print(a) def main():
    n = input()
    a, b = 0, 1
    for i in n:
        x = int(i)
        a, b = min(a + x, b + 10 - x), min(a + x + 1, b + 10 - x - 1)
    print(a) main
def main():
    n = input()
    a, b = 0, 1
    for i in n:
        x = int(i)
        a, b = min(a + x, b + 10 - x), min(a + x + 1, b + 10 - x - 1)
    print(a)

main()
