N = reversed("0" + input())
digit = next(N)
total = 0
carry = 0
for nextdigit in N:
    digit = int(digit) + carry
    if digit < 5:
        total += digit
        carry = 0
    elif digit == 5:
        if int(nextdigit) < 5:
            total += digit
            carry = 0
        else:
            total += 10 - digit
            carry = 1
    else:
        total += 10 - digit
        carry = 1
    digit = nextdigit

print(total + carry) N = reversed("0" + input()) N reversed("0" + input()) reversed "0" + input() "0" input() input digit = next(N) digit next(N) next N total = 0 total 0 carry = 0 carry 0 for nextdigit in N:
    digit = int(digit) + carry
    if digit < 5:
        total += digit
        carry = 0
    elif digit == 5:
        if int(nextdigit) < 5:
            total += digit
            carry = 0
        else:
            total += 10 - digit
            carry = 1
    else:
        total += 10 - digit
        carry = 1
    digit = nextdigit nextdigit N digit = int(digit) + carry digit int(digit) + carry int(digit) int digit carry if digit < 5:
        total += digit
        carry = 0
    elif digit == 5:
        if int(nextdigit) < 5:
            total += digit
            carry = 0
        else:
            total += 10 - digit
            carry = 1
    else:
        total += 10 - digit
        carry = 1
     digit < 5 digit 5 total += digit total digit carry = 0 carry 0 elif digit == 5:
        if int(nextdigit) < 5:
            total += digit
            carry = 0
        else:
            total += 10 - digit
            carry = 1
     digit == 5 digit 5 if int(nextdigit) < 5:
            total += digit
            carry = 0
        else:
            total += 10 - digit
            carry = 1
     int(nextdigit) < 5 int(nextdigit) int nextdigit 5 total += digit total digit carry = 0 carry 0 total += 10 - digit total 10 - digit 10 digit carry = 1 carry 1 total += 10 - digit total 10 - digit 10 digit carry = 1 carry 1 digit = nextdigit digit nextdigit print(total + carry) print total + carry total carry total += digit digit total total = 0 0 total total += 10 - digit 10 - digit total digit = nextdigit nextdigit digit digit = int(digit) + carry int(digit) + carry digit carry = 0 0 carry total += 10 - digit 10 - digit total N = reversed("0" + input()) reversed("0" + input()) N carry = 0 0 carry carry = 1 1 carry total += digit digit total carry = 1 1 carry digit = next(N) next(N) digit carry = 0 0 carry
N = reversed("0" + input())
digit = next(N)
total = 0
carry = 0
for nextdigit in N:
    digit = int(digit) + carry
    if digit < 5:
        total += digit
        carry = 0
    elif digit == 5:
        if int(nextdigit) < 5:
            total += digit
            carry = 0
        else:
            total += 10 - digit
            carry = 1
    else:
        total += 10 - digit
        carry = 1
    digit = nextdigit

print(total + carry)
