def resolve():
    N = input().strip()
    carry = 0
    a = 0 # 99 -> 099
    b = 1 # 99 -> 100
    for ch in N:
        n = ord(ch) - ord('0')
        a2 = min(a+n, b+10-n)
        b = min(a+n+1, b+10-n-1)
        a=a2
    print(a)

resolve() def resolve():
    N = input().strip()
    carry = 0
    a = 0 # 99 -> 099
    b = 1 # 99 -> 100
    for ch in N:
        n = ord(ch) - ord('0')
        a2 = min(a+n, b+10-n)
        b = min(a+n+1, b+10-n-1)
        a=a2
    print(a) resolve N = input().strip() N input().strip() ().strip () input strip carry = 0 carry 0 a = 0 a 0 b = 1 b 1 for ch in N:
        n = ord(ch) - ord('0')
        a2 = min(a+n, b+10-n)
        b = min(a+n+1, b+10-n-1)
        a=a2
     ch N n = ord(ch) - ord('0') n ord(ch) - ord('0') ord(ch) ord ch ord('0') ord '0' a2 = min(a+n, b+10-n) a2 min(a+n, b+10-n) min a+n a n b+10-n b+10 b 10 n b = min(a+n+1, b+10-n-1) b min(a+n+1, b+10-n-1) min a+n+1 a+n a n 1 b+10-n-1 b+10-n b+10 b 10 n 1 a=a2 a a2 print(a) print a resolve() resolve def resolve():
    N = input().strip()
    carry = 0
    a = 0 # 99 -> 099
    b = 1 # 99 -> 100
    for ch in N:
        n = ord(ch) - ord('0')
        a2 = min(a+n, b+10-n)
        b = min(a+n+1, b+10-n-1)
        a=a2
    print(a) def resolve():
    N = input().strip()
    carry = 0
    a = 0 # 99 -> 099
    b = 1 # 99 -> 100
    for ch in N:
        n = ord(ch) - ord('0')
        a2 = min(a+n, b+10-n)
        b = min(a+n+1, b+10-n-1)
        a=a2
    print(a) resolve
def resolve():
    N = input().strip()
    carry = 0
    a = 0 # 99 -> 099
    b = 1 # 99 -> 100
    for ch in N:
        n = ord(ch) - ord('0')
        a2 = min(a+n, b+10-n)
        b = min(a+n+1, b+10-n-1)
        a=a2
    print(a)

resolve()