import sys
input=sys.stdin.readline
ns=input().rstrip()
ans=0
l=[]
ls=len(ns)
for i in range(ls):
    l.append(int(ns[ls-1-i]))
for i in range(10**6+1-ls):
    l.append(0)
for i in range(10**6+1):
    if 0<=l[i]<=4:
        ans+=l[i]
    elif 6<=l[i]<=9:
        ans+=10-l[i]
        l[i+1]+=1
    elif l[i]==10:
        l[i+1]+=1
    elif l[i]==5:
        ans+=5
        if 0<=l[i+1]<=4:
            pass
        else:
            l[i+1]+=1
print(ans)





 import sys sys input=sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline ns=input().rstrip() ns input().rstrip() ().rstrip () input rstrip ans=0 ans 0 l=[] l [] ls=len(ns) ls len(ns) len ns for i in range(ls):
    l.append(int(ns[ls-1-i])) i range(ls) range ls l.append(int(ns[ls-1-i])) l.append l append int(ns[ls-1-i]) int ns[ls-1-i] ns ls-1-i ls-1 ls 1 i for i in range(10**6+1-ls):
    l.append(0) i range(10**6+1-ls) range 10**6+1-ls 10**6+1 10**6 10 6 1 ls l.append(0) l.append l append 0 for i in range(10**6+1):
    if 0<=l[i]<=4:
        ans+=l[i]
    elif 6<=l[i]<=9:
        ans+=10-l[i]
        l[i+1]+=1
    elif l[i]==10:
        l[i+1]+=1
    elif l[i]==5:
        ans+=5
        if 0<=l[i+1]<=4:
            pass
        else:
            l[i+1]+=1 i range(10**6+1) range 10**6+1 10**6 10 6 1 if 0<=l[i]<=4:
        ans+=l[i]
    elif 6<=l[i]<=9:
        ans+=10-l[i]
        l[i+1]+=1
    elif l[i]==10:
        l[i+1]+=1
    elif l[i]==5:
        ans+=5
        if 0<=l[i+1]<=4:
            pass
        else:
            l[i+1]+=1 0<=l[i]<=4 0<=l[i] 0 l[i] l i 4 ans+=l[i] ans l[i] l i elif 6<=l[i]<=9:
        ans+=10-l[i]
        l[i+1]+=1
     6<=l[i]<=9 6<=l[i] 6 l[i] l i 9 ans+=10-l[i] ans 10-l[i] 10 l[i] l i l[i+1]+=1 l[i+1] l i+1 i 1 1 elif l[i]==10:
        l[i+1]+=1
     l[i]==10 l[i] l i 10 l[i+1]+=1 l[i+1] l i+1 i 1 1 elif l[i]==5:
        ans+=5
        if 0<=l[i+1]<=4:
            pass
        else:
            l[i+1]+=1 l[i]==5 l[i] l i 5 ans+=5 ans 5 if 0<=l[i+1]<=4:
            pass
        else:
            l[i+1]+=1 0<=l[i+1]<=4 0<=l[i+1] 0 l[i+1] l i+1 i 1 4 l[i+1]+=1 l[i+1] l i+1 i 1 1 print(ans) print ans ans+=10-l[i] 10-l[i] ans l=[] [] l ans+=5 5 ans ns=input().rstrip() input().rstrip() ns ans+=l[i] l[i] ans ls=len(ns) len(ns) ls input=sys.stdin.readline sys.stdin.readline input ans=0 0 ans
import sys
input=sys.stdin.readline
ns=input().rstrip()
ans=0
l=[]
ls=len(ns)
for i in range(ls):
    l.append(int(ns[ls-1-i]))
for i in range(10**6+1-ls):
    l.append(0)
for i in range(10**6+1):
    if 0<=l[i]<=4:
        ans+=l[i]
    elif 6<=l[i]<=9:
        ans+=10-l[i]
        l[i+1]+=1
    elif l[i]==10:
        l[i+1]+=1
    elif l[i]==5:
        ans+=5
        if 0<=l[i+1]<=4:
            pass
        else:
            l[i+1]+=1
print(ans)






