N = input()

N = '0' + N
result = 0
carry = 0
for i in range(len(N) - 1, 0, -1):
    c = int(N[i]) + carry
    n = int(N[i - 1])
    if c < 5 or (c == 5 and n < 5):
        result += c
        carry = 0
    else:
        result += 10 - c
        carry = 1
result += carry
print(result) N = input() N input() input N = '0' + N N '0' + N '0' N result = 0 result 0 carry = 0 carry 0 for i in range(len(N) - 1, 0, -1):
    c = int(N[i]) + carry
    n = int(N[i - 1])
    if c < 5 or (c == 5 and n < 5):
        result += c
        carry = 0
    else:
        result += 10 - c
        carry = 1 i range(len(N) - 1, 0, -1) range len(N) - 1 len(N) len N 1 0 -1 c = int(N[i]) + carry c int(N[i]) + carry int(N[i]) int N[i] N i carry n = int(N[i - 1]) n int(N[i - 1]) int N[i - 1] N i - 1 i 1 if c < 5 or (c == 5 and n < 5):
        result += c
        carry = 0
    else:
        result += 10 - c
        carry = 1 c < 5 or (c == 5 and n < 5) c < 5 c 5 c == 5 and n < 5 c == 5 c 5 n < 5 n 5 result += c result c carry = 0 carry 0 result += 10 - c result 10 - c 10 c carry = 1 carry 1 result += carry result carry print(result) print result c = int(N[i]) + carry int(N[i]) + carry c n = int(N[i - 1]) int(N[i - 1]) n result += 10 - c 10 - c result carry = 1 1 carry result = 0 0 result result += c c result N = input() input() N result += carry carry result N = '0' + N '0' + N N carry = 0 0 carry carry = 0 0 carry
N = input()

N = '0' + N
result = 0
carry = 0
for i in range(len(N) - 1, 0, -1):
    c = int(N[i]) + carry
    n = int(N[i - 1])
    if c < 5 or (c == 5 and n < 5):
        result += c
        carry = 0
    else:
        result += 10 - c
        carry = 1
result += carry
print(result)
