# E - Payment

N = input().strip()

s = 0
carry = 0
five = False
for c in reversed(N):
    x = int(c) + carry
    if x == 5 and not five:
        s += x
        carry = 0
        five = True
    elif x >= 5:
        if five:
            x += 1
        s += 10 - x
        carry = 1
        five = False
    else:
        s += x
        carry = 0
        five = False
s += carry

print(s) N = input().strip() N input().strip() ().strip () input strip s = 0 s 0 carry = 0 carry 0 five = False five False for c in reversed(N):
    x = int(c) + carry
    if x == 5 and not five:
        s += x
        carry = 0
        five = True
    elif x >= 5:
        if five:
            x += 1
        s += 10 - x
        carry = 1
        five = False
    else:
        s += x
        carry = 0
        five = False c reversed(N) reversed N x = int(c) + carry x int(c) + carry int(c) int c carry if x == 5 and not five:
        s += x
        carry = 0
        five = True
    elif x >= 5:
        if five:
            x += 1
        s += 10 - x
        carry = 1
        five = False
    else:
        s += x
        carry = 0
        five = False x == 5 and not five x == 5 x 5 not five five s += x s x carry = 0 carry 0 five = True five True elif x >= 5:
        if five:
            x += 1
        s += 10 - x
        carry = 1
        five = False
     x >= 5 x 5 if five:
            x += 1
         five x += 1 x 1 s += 10 - x s 10 - x 10 x carry = 1 carry 1 five = False five False s += x s x carry = 0 carry 0 five = False five False s += carry s carry print(s) print s s += x x s five = False False five s = 0 0 s carry = 0 0 carry x = int(c) + carry int(c) + carry x carry = 0 0 carry carry = 1 1 carry s += 10 - x 10 - x s N = input().strip() input().strip() N carry = 0 0 carry s += x x s five = False False five five = False False five five = True True five s += carry carry s x += 1 1 x
# E - Payment

N = input().strip()

s = 0
carry = 0
five = False
for c in reversed(N):
    x = int(c) + carry
    if x == 5 and not five:
        s += x
        carry = 0
        five = True
    elif x >= 5:
        if five:
            x += 1
        s += 10 - x
        carry = 1
        five = False
    else:
        s += x
        carry = 0
        five = False
s += carry

print(s)
