N = list(map(int, input()))

ans = 0
carry = 0   #0 or 1 or 2. when 2, it means that it's optional between 0 and 1.
for i in range(len(N) - 1, -1, -1):
    if carry == 2:
        if N[i] >= 5:
            digit = N[i] + 1
            carry = 1
            ans += (10 - digit)
        else:
            digit = N[i]
            carry = 0
            ans += digit
    else:
        digit = N[i] + carry
        if digit >= 6:
            carry = 1
            ans += (10 - digit)
        elif digit <= 4:
            carry = 0
            ans += digit
        else:
            carry = 2
            ans += digit
        

if carry == 1:
    ans += carry

print(ans) N = list(map(int, input())) N list(map(int, input())) list map(int, input()) map int input() input ans = 0 ans 0 carry = 0 carry 0 for i in range(len(N) - 1, -1, -1):
    if carry == 2:
        if N[i] >= 5:
            digit = N[i] + 1
            carry = 1
            ans += (10 - digit)
        else:
            digit = N[i]
            carry = 0
            ans += digit
    else:
        digit = N[i] + carry
        if digit >= 6:
            carry = 1
            ans += (10 - digit)
        elif digit <= 4:
            carry = 0
            ans += digit
        else:
            carry = 2
            ans += digit
         i range(len(N) - 1, -1, -1) range len(N) - 1 len(N) len N 1 -1 -1 if carry == 2:
        if N[i] >= 5:
            digit = N[i] + 1
            carry = 1
            ans += (10 - digit)
        else:
            digit = N[i]
            carry = 0
            ans += digit
    else:
        digit = N[i] + carry
        if digit >= 6:
            carry = 1
            ans += (10 - digit)
        elif digit <= 4:
            carry = 0
            ans += digit
        else:
            carry = 2
            ans += digit
         carry == 2 carry 2 if N[i] >= 5:
            digit = N[i] + 1
            carry = 1
            ans += (10 - digit)
        else:
            digit = N[i]
            carry = 0
            ans += digit
     N[i] >= 5 N[i] N i 5 digit = N[i] + 1 digit N[i] + 1 N[i] N i 1 carry = 1 carry 1 ans += (10 - digit) ans 10 - digit 10 digit digit = N[i] digit N[i] N i carry = 0 carry 0 ans += digit ans digit digit = N[i] + carry digit N[i] + carry N[i] N i carry if digit >= 6:
            carry = 1
            ans += (10 - digit)
        elif digit <= 4:
            carry = 0
            ans += digit
        else:
            carry = 2
            ans += digit
         digit >= 6 digit 6 carry = 1 carry 1 ans += (10 - digit) ans 10 - digit 10 digit elif digit <= 4:
            carry = 0
            ans += digit
         digit <= 4 digit 4 carry = 0 carry 0 ans += digit ans digit carry = 2 carry 2 ans += digit ans digit if carry == 1:
    ans += carry carry == 1 carry 1 ans += carry ans carry print(ans) print ans carry = 1 1 carry carry = 1 1 carry ans += (10 - digit) 10 - digit ans ans += carry carry ans digit = N[i] N[i] digit ans += digit digit ans digit = N[i] + 1 N[i] + 1 digit ans += digit digit ans carry = 0 0 carry carry = 0 0 carry carry = 2 2 carry carry = 0 0 carry ans += (10 - digit) 10 - digit ans N = list(map(int, input())) list(map(int, input())) N ans += digit digit ans ans = 0 0 ans digit = N[i] + carry N[i] + carry digit
N = list(map(int, input()))

ans = 0
carry = 0   #0 or 1 or 2. when 2, it means that it's optional between 0 and 1.
for i in range(len(N) - 1, -1, -1):
    if carry == 2:
        if N[i] >= 5:
            digit = N[i] + 1
            carry = 1
            ans += (10 - digit)
        else:
            digit = N[i]
            carry = 0
            ans += digit
    else:
        digit = N[i] + carry
        if digit >= 6:
            carry = 1
            ans += (10 - digit)
        elif digit <= 4:
            carry = 0
            ans += digit
        else:
            carry = 2
            ans += digit
        

if carry == 1:
    ans += carry

print(ans)