import sys
input = sys.stdin.readline

n = list(input().strip())
ans = 0

carry = False
for i in range(len(n) - 1, -1, -1):
    p = int(n[i])
    if carry:
        p += 1
        carry = False
    if p == 10:
        carry = True
    elif p >= 6:
        carry = True
        ans += 10 - p
    elif p == 5:
        if i != 0 and int(n[i - 1]) >= 5:
            carry = True
            ans += 5
        else:
            carry = False
            ans += 5
    else:
        ans += p
if carry:
    ans += 1
print(ans) import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline n = list(input().strip()) n list(input().strip()) list input().strip() ().strip () input strip ans = 0 ans 0 carry = False carry False for i in range(len(n) - 1, -1, -1):
    p = int(n[i])
    if carry:
        p += 1
        carry = False
    if p == 10:
        carry = True
    elif p >= 6:
        carry = True
        ans += 10 - p
    elif p == 5:
        if i != 0 and int(n[i - 1]) >= 5:
            carry = True
            ans += 5
        else:
            carry = False
            ans += 5
    else:
        ans += p i range(len(n) - 1, -1, -1) range len(n) - 1 len(n) len n 1 -1 -1 p = int(n[i]) p int(n[i]) int n[i] n i if carry:
        p += 1
        carry = False
     carry p += 1 p 1 carry = False carry False if p == 10:
        carry = True
    elif p >= 6:
        carry = True
        ans += 10 - p
    elif p == 5:
        if i != 0 and int(n[i - 1]) >= 5:
            carry = True
            ans += 5
        else:
            carry = False
            ans += 5
    else:
        ans += p p == 10 p 10 carry = True carry True elif p >= 6:
        carry = True
        ans += 10 - p
     p >= 6 p 6 carry = True carry True ans += 10 - p ans 10 - p 10 p elif p == 5:
        if i != 0 and int(n[i - 1]) >= 5:
            carry = True
            ans += 5
        else:
            carry = False
            ans += 5
     p == 5 p 5 if i != 0 and int(n[i - 1]) >= 5:
            carry = True
            ans += 5
        else:
            carry = False
            ans += 5
     i != 0 and int(n[i - 1]) >= 5 i != 0 i 0 int(n[i - 1]) >= 5 int(n[i - 1]) int n[i - 1] n i - 1 i 1 5 carry = True carry True ans += 5 ans 5 carry = False carry False ans += 5 ans 5 ans += p ans p if carry:
    ans += 1 carry ans += 1 ans 1 print(ans) print ans ans += 5 5 ans carry = False False carry carry = True True carry ans += 5 5 ans carry = False False carry carry = True True carry input = sys.stdin.readline sys.stdin.readline input p += 1 1 p carry = True True carry n = list(input().strip()) list(input().strip()) n ans = 0 0 ans carry = False False carry ans += p p ans ans += 1 1 ans ans += 10 - p 10 - p ans p = int(n[i]) int(n[i]) p
import sys
input = sys.stdin.readline

n = list(input().strip())
ans = 0

carry = False
for i in range(len(n) - 1, -1, -1):
    p = int(n[i])
    if carry:
        p += 1
        carry = False
    if p == 10:
        carry = True
    elif p >= 6:
        carry = True
        ans += 10 - p
    elif p == 5:
        if i != 0 and int(n[i - 1]) >= 5:
            carry = True
            ans += 5
        else:
            carry = False
            ans += 5
    else:
        ans += p
if carry:
    ans += 1
print(ans)
