n = input().rstrip()

total = 0
carry = 0
for i in range(len(n)-1, -1, -1):
    v = int(n[i]) + carry
    if v > 5:
        total += 10 - v
        carry = 1
    elif v == 5:
        if i == 0:
            total += 5
            carry = 0
        else:
            if int(n[i-1]) >= 5:
                total += 5
                carry = 1
            else:
                total += 5
                carry = 0
    else:
        total += v
        carry = 0

print(total + carry) n = input().rstrip() n input().rstrip() ().rstrip () input rstrip total = 0 total 0 carry = 0 carry 0 for i in range(len(n)-1, -1, -1):
    v = int(n[i]) + carry
    if v > 5:
        total += 10 - v
        carry = 1
    elif v == 5:
        if i == 0:
            total += 5
            carry = 0
        else:
            if int(n[i-1]) >= 5:
                total += 5
                carry = 1
            else:
                total += 5
                carry = 0
    else:
        total += v
        carry = 0 i range(len(n)-1, -1, -1) range len(n)-1 len(n) len n 1 -1 -1 v = int(n[i]) + carry v int(n[i]) + carry int(n[i]) int n[i] n i carry if v > 5:
        total += 10 - v
        carry = 1
    elif v == 5:
        if i == 0:
            total += 5
            carry = 0
        else:
            if int(n[i-1]) >= 5:
                total += 5
                carry = 1
            else:
                total += 5
                carry = 0
    else:
        total += v
        carry = 0 v > 5 v 5 total += 10 - v total 10 - v 10 v carry = 1 carry 1 elif v == 5:
        if i == 0:
            total += 5
            carry = 0
        else:
            if int(n[i-1]) >= 5:
                total += 5
                carry = 1
            else:
                total += 5
                carry = 0
     v == 5 v 5 if i == 0:
            total += 5
            carry = 0
        else:
            if int(n[i-1]) >= 5:
                total += 5
                carry = 1
            else:
                total += 5
                carry = 0
     i == 0 i 0 total += 5 total 5 carry = 0 carry 0 if int(n[i-1]) >= 5:
                total += 5
                carry = 1
            else:
                total += 5
                carry = 0
     int(n[i-1]) >= 5 int(n[i-1]) int n[i-1] n i-1 i 1 5 total += 5 total 5 carry = 1 carry 1 total += 5 total 5 carry = 0 carry 0 total += v total v carry = 0 carry 0 print(total + carry) print total + carry total carry total += 5 5 total n = input().rstrip() input().rstrip() n carry = 1 1 carry v = int(n[i]) + carry int(n[i]) + carry v total += v v total total += 5 5 total carry = 0 0 carry total = 0 0 total total += 5 5 total carry = 0 0 carry total += 10 - v 10 - v total carry = 1 1 carry carry = 0 0 carry carry = 0 0 carry
n = input().rstrip()

total = 0
carry = 0
for i in range(len(n)-1, -1, -1):
    v = int(n[i]) + carry
    if v > 5:
        total += 10 - v
        carry = 1
    elif v == 5:
        if i == 0:
            total += 5
            carry = 0
        else:
            if int(n[i-1]) >= 5:
                total += 5
                carry = 1
            else:
                total += 5
                carry = 0
    else:
        total += v
        carry = 0

print(total + carry)