m=str(raw_input())
n=[int(i) for i in m]
n.insert(0,0)
l=len(n)
ans=0
for i in range(l-1,-1,-1):
    current=n[i]
    if(current<5):
        ans+=current
    elif(current==5):
        ans+=current
        if(n[i-1]>=5):
            n[i-1]+=1
    else:
        ans+=10-current
        n[i-1]+=1
print ans m=str(raw_input()) m str(raw_input()) str raw_input() raw_input int(i) for i in m for i in m i m for i in m int(i) int i n=[int(i) for i in m] n [int(i) for i in m] n.insert(0,0) n.insert n insert 0 0 l=len(n) l len(n) len n ans=0 ans 0 for i in range(l-1,-1,-1):
    current=n[i]
    if(current<5):
        ans+=current
    elif(current==5):
        ans+=current
        if(n[i-1]>=5):
            n[i-1]+=1
    else:
        ans+=10-current
        n[i-1]+=1 i range(l-1,-1,-1) range l-1 l 1 -1 -1 current=n[i] current n[i] n i if(current<5):
        ans+=current
    elif(current==5):
        ans+=current
        if(n[i-1]>=5):
            n[i-1]+=1
    else:
        ans+=10-current
        n[i-1]+=1 current<5 current 5 ans+=current ans current elif(current==5):
        ans+=current
        if(n[i-1]>=5):
            n[i-1]+=1
     current==5 current 5 ans+=current ans current if(n[i-1]>=5):
            n[i-1]+=1
     n[i-1]>=5 n[i-1] n i-1 i 1 5 n[i-1]+=1 n[i-1] n i-1 i 1 1 ans+=10-current ans 10-current 10 current n[i-1]+=1 n[i-1] n i-1 i 1 1 print l=len(n) len(n) l m=str(raw_input()) str(raw_input()) m ans=0 0 ans current=n[i] n[i] current ans+=current current ans n=[int(i) for i in m] [int(i) for i in m] n ans+=10-current 10-current ans ans+=current current ans
m=str(raw_input())
n=[int(i) for i in m]
n.insert(0,0)
l=len(n)
ans=0
for i in range(l-1,-1,-1):
    current=n[i]
    if(current<5):
        ans+=current
    elif(current==5):
        ans+=current
        if(n[i-1]>=5):
            n[i-1]+=1
    else:
        ans+=10-current
        n[i-1]+=1
print ans
