import sys

n=sys.stdin.readline().rstrip()[::-1]
to=0

m=0
n+="0"
num=len(n)

for i in range(num):
    k=int(n[i])
    k+=m
    if k==5:
        if int(n[i+1])>=5:
            to+=10-k
            m=1
        else:
            to+=k
            m=0
    if k<5:
        to+=k
        m=0
    elif k>5:
        to+=10-k
        m=1
        
print(to+m) import sys sys n=sys.stdin.readline().rstrip()[::-1] n sys.stdin.readline().rstrip()[::-1] sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip ::-1 -1 to=0 to 0 m=0 m 0 n+="0" n "0" num=len(n) num len(n) len n for i in range(num):
    k=int(n[i])
    k+=m
    if k==5:
        if int(n[i+1])>=5:
            to+=10-k
            m=1
        else:
            to+=k
            m=0
    if k<5:
        to+=k
        m=0
    elif k>5:
        to+=10-k
        m=1
         i range(num) range num k=int(n[i]) k int(n[i]) int n[i] n i k+=m k m if k==5:
        if int(n[i+1])>=5:
            to+=10-k
            m=1
        else:
            to+=k
            m=0
     k==5 k 5 if int(n[i+1])>=5:
            to+=10-k
            m=1
        else:
            to+=k
            m=0
     int(n[i+1])>=5 int(n[i+1]) int n[i+1] n i+1 i 1 5 to+=10-k to 10-k 10 k m=1 m 1 to+=k to k m=0 m 0 if k<5:
        to+=k
        m=0
    elif k>5:
        to+=10-k
        m=1
         k<5 k 5 to+=k to k m=0 m 0 elif k>5:
        to+=10-k
        m=1
         k>5 k 5 to+=10-k to 10-k 10 k m=1 m 1 print(to+m) print to+m to m k+=m m k m=1 1 m m=0 0 m to=0 0 to k=int(n[i]) int(n[i]) k m=1 1 m to+=10-k 10-k to n+="0" "0" n to+=k k to n=sys.stdin.readline().rstrip()[::-1] sys.stdin.readline().rstrip()[::-1] n to+=10-k 10-k to to+=k k to m=0 0 m num=len(n) len(n) num m=0 0 m
import sys

n=sys.stdin.readline().rstrip()[::-1]
to=0

m=0
n+="0"
num=len(n)

for i in range(num):
    k=int(n[i])
    k+=m
    if k==5:
        if int(n[i+1])>=5:
            to+=10-k
            m=1
        else:
            to+=k
            m=0
    if k<5:
        to+=k
        m=0
    elif k>5:
        to+=10-k
        m=1
        
print(to+m)
