# coding: utf-8
import sys
sysread = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10**7)

def run():
    N = list(map(int, list(input())))
    N = N[::-1] + [0]
    count = 0
    seq = 0
    for idx, n in enumerate(N[:]):
        if seq == 1 and (n >= 5 or (n == 4 and N[idx+1] >= 5)):
            count += 9 - n
        elif seq == 0 and (n >= 6 or (n == 5 and N[idx+1] >= 5)):
            count += 10 - n
            seq = 1
        else:
            count += n + seq
            seq = 0
    print(count)

if __name__ == "__main__":
    run() import sys sys sysread = sys.stdin.readline sysread sys.stdin.readline sys.stdin sys stdin readline read = sys.stdin.read read sys.stdin.read sys.stdin sys stdin read sys.setrecursionlimit(10**7) sys.setrecursionlimit sys setrecursionlimit 10**7 10 7 def run():
    N = list(map(int, list(input())))
    N = N[::-1] + [0]
    count = 0
    seq = 0
    for idx, n in enumerate(N[:]):
        if seq == 1 and (n >= 5 or (n == 4 and N[idx+1] >= 5)):
            count += 9 - n
        elif seq == 0 and (n >= 6 or (n == 5 and N[idx+1] >= 5)):
            count += 10 - n
            seq = 1
        else:
            count += n + seq
            seq = 0
    print(count) run N = list(map(int, list(input()))) N list(map(int, list(input()))) list map(int, list(input())) map int list(input()) list input() input N = N[::-1] + [0] N N[::-1] + [0] N[::-1] N ::-1 -1 [0] 0 count = 0 count 0 seq = 0 seq 0 for idx, n in enumerate(N[:]):
        if seq == 1 and (n >= 5 or (n == 4 and N[idx+1] >= 5)):
            count += 9 - n
        elif seq == 0 and (n >= 6 or (n == 5 and N[idx+1] >= 5)):
            count += 10 - n
            seq = 1
        else:
            count += n + seq
            seq = 0
     idx n enumerate(N[:]) enumerate N[:] N : if seq == 1 and (n >= 5 or (n == 4 and N[idx+1] >= 5)):
            count += 9 - n
        elif seq == 0 and (n >= 6 or (n == 5 and N[idx+1] >= 5)):
            count += 10 - n
            seq = 1
        else:
            count += n + seq
            seq = 0
     seq == 1 and (n >= 5 or (n == 4 and N[idx+1] >= 5)) seq == 1 seq 1 n >= 5 or (n == 4 and N[idx+1] >= 5) n >= 5 n 5 n == 4 and N[idx+1] >= 5 n == 4 n 4 N[idx+1] >= 5 N[idx+1] N idx+1 idx 1 5 count += 9 - n count 9 - n 9 n elif seq == 0 and (n >= 6 or (n == 5 and N[idx+1] >= 5)):
            count += 10 - n
            seq = 1
         seq == 0 and (n >= 6 or (n == 5 and N[idx+1] >= 5)) seq == 0 seq 0 n >= 6 or (n == 5 and N[idx+1] >= 5) n >= 6 n 6 n == 5 and N[idx+1] >= 5 n == 5 n 5 N[idx+1] >= 5 N[idx+1] N idx+1 idx 1 5 count += 10 - n count 10 - n 10 n seq = 1 seq 1 count += n + seq count n + seq n seq seq = 0 seq 0 print(count) print count if __name__ == "__main__":
    run() __name__ == "__main__" __name__ "__main__" run() run read = sys.stdin.read sys.stdin.read read sysread = sys.stdin.readline sys.stdin.readline sysread def run():
    N = list(map(int, list(input())))
    N = N[::-1] + [0]
    count = 0
    seq = 0
    for idx, n in enumerate(N[:]):
        if seq == 1 and (n >= 5 or (n == 4 and N[idx+1] >= 5)):
            count += 9 - n
        elif seq == 0 and (n >= 6 or (n == 5 and N[idx+1] >= 5)):
            count += 10 - n
            seq = 1
        else:
            count += n + seq
            seq = 0
    print(count) def run():
    N = list(map(int, list(input())))
    N = N[::-1] + [0]
    count = 0
    seq = 0
    for idx, n in enumerate(N[:]):
        if seq == 1 and (n >= 5 or (n == 4 and N[idx+1] >= 5)):
            count += 9 - n
        elif seq == 0 and (n >= 6 or (n == 5 and N[idx+1] >= 5)):
            count += 10 - n
            seq = 1
        else:
            count += n + seq
            seq = 0
    print(count) run
# coding: utf-8
import sys
sysread = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10**7)

def run():
    N = list(map(int, list(input())))
    N = N[::-1] + [0]
    count = 0
    seq = 0
    for idx, n in enumerate(N[:]):
        if seq == 1 and (n >= 5 or (n == 4 and N[idx+1] >= 5)):
            count += 9 - n
        elif seq == 0 and (n >= 6 or (n == 5 and N[idx+1] >= 5)):
            count += 10 - n
            seq = 1
        else:
            count += n + seq
            seq = 0
    print(count)

if __name__ == "__main__":
    run()