# E - Payment

N = input().strip()

s = 0
carry = 0
prev = None
for c in reversed(N):
    x = int(c) + carry
    if x > 5 or x == prev == 5:
        if prev == 5:
            x += 1
        s += 10 - x
        carry = 1
    else:
        s += x
        carry = 0
    prev = x
s += carry

print(s) N = input().strip() N input().strip() ().strip () input strip s = 0 s 0 carry = 0 carry 0 prev = None prev None for c in reversed(N):
    x = int(c) + carry
    if x > 5 or x == prev == 5:
        if prev == 5:
            x += 1
        s += 10 - x
        carry = 1
    else:
        s += x
        carry = 0
    prev = x c reversed(N) reversed N x = int(c) + carry x int(c) + carry int(c) int c carry if x > 5 or x == prev == 5:
        if prev == 5:
            x += 1
        s += 10 - x
        carry = 1
    else:
        s += x
        carry = 0
     x > 5 or x == prev == 5 x > 5 x 5 x == prev == 5 x == prev x prev 5 if prev == 5:
            x += 1
         prev == 5 prev 5 x += 1 x 1 s += 10 - x s 10 - x 10 x carry = 1 carry 1 s += x s x carry = 0 carry 0 prev = x prev x s += carry s carry print(s) print s carry = 0 0 carry N = input().strip() input().strip() N x = int(c) + carry int(c) + carry x prev = None None prev s += 10 - x 10 - x s prev = x x prev carry = 0 0 carry carry = 1 1 carry s += carry carry s s = 0 0 s x += 1 1 x s += x x s
# E - Payment

N = input().strip()

s = 0
carry = 0
prev = None
for c in reversed(N):
    x = int(c) + carry
    if x > 5 or x == prev == 5:
        if prev == 5:
            x += 1
        s += 10 - x
        carry = 1
    else:
        s += x
        carry = 0
    prev = x
s += carry

print(s)
