s = input()

ans = 0
carry = 0
for i in range(len(s) - 1, -1, -1):
    c = int(s[i])
    if c + carry < 5:
        ans += c + carry
        carry = 0
    elif c + carry == 5:
        if i > 0 and int(s[i - 1]) >= 5:
            ans += 5
            carry = 1
        else:
            ans += 5
            carry = 0
    else:
        ans += 10 - c - carry
        carry = 1
if carry:
    ans += 1
print(ans) s = input() s input() input ans = 0 ans 0 carry = 0 carry 0 for i in range(len(s) - 1, -1, -1):
    c = int(s[i])
    if c + carry < 5:
        ans += c + carry
        carry = 0
    elif c + carry == 5:
        if i > 0 and int(s[i - 1]) >= 5:
            ans += 5
            carry = 1
        else:
            ans += 5
            carry = 0
    else:
        ans += 10 - c - carry
        carry = 1 i range(len(s) - 1, -1, -1) range len(s) - 1 len(s) len s 1 -1 -1 c = int(s[i]) c int(s[i]) int s[i] s i if c + carry < 5:
        ans += c + carry
        carry = 0
    elif c + carry == 5:
        if i > 0 and int(s[i - 1]) >= 5:
            ans += 5
            carry = 1
        else:
            ans += 5
            carry = 0
    else:
        ans += 10 - c - carry
        carry = 1 c + carry < 5 c + carry c carry 5 ans += c + carry ans c + carry c carry carry = 0 carry 0 elif c + carry == 5:
        if i > 0 and int(s[i - 1]) >= 5:
            ans += 5
            carry = 1
        else:
            ans += 5
            carry = 0
     c + carry == 5 c + carry c carry 5 if i > 0 and int(s[i - 1]) >= 5:
            ans += 5
            carry = 1
        else:
            ans += 5
            carry = 0
     i > 0 and int(s[i - 1]) >= 5 i > 0 i 0 int(s[i - 1]) >= 5 int(s[i - 1]) int s[i - 1] s i - 1 i 1 5 ans += 5 ans 5 carry = 1 carry 1 ans += 5 ans 5 carry = 0 carry 0 ans += 10 - c - carry ans 10 - c - carry 10 - c 10 c carry carry = 1 carry 1 if carry:
    ans += 1 carry ans += 1 ans 1 print(ans) print ans carry = 1 1 carry ans += 1 1 ans ans += 5 5 ans carry = 0 0 carry ans += 5 5 ans ans += 10 - c - carry 10 - c - carry ans s = input() input() s carry = 0 0 carry c = int(s[i]) int(s[i]) c ans = 0 0 ans carry = 0 0 carry ans += c + carry c + carry ans carry = 1 1 carry
s = input()

ans = 0
carry = 0
for i in range(len(s) - 1, -1, -1):
    c = int(s[i])
    if c + carry < 5:
        ans += c + carry
        carry = 0
    elif c + carry == 5:
        if i > 0 and int(s[i - 1]) >= 5:
            ans += 5
            carry = 1
        else:
            ans += 5
            carry = 0
    else:
        ans += 10 - c - carry
        carry = 1
if carry:
    ans += 1
print(ans)
