#E
#greedy 
n = input()
n = n[::-1] + "0"
l = len(n)

ans = 0

kuriagari=0

for i,v in enumerate(n):
    v = int(v)
    if kuriagari:
        v += 1
    
    if v<5:
        ans += v
        kuriagari = 0
    if v>5:
        ans += 10 - v
        kuriagari = 1
    if v==5:
        if int(n[i+1])<5:
            #delete is better
            ans += v
            kuriagari = 0
        if int(n[i+1])>=5:
            #kuriagari is promising
            ans += 10 - v
            kuriagari = 1
        
print(ans) n = input() n input() input n = n[::-1] + "0" n n[::-1] + "0" n[::-1] n ::-1 -1 "0" l = len(n) l len(n) len n ans = 0 ans 0 kuriagari=0 kuriagari 0 for i,v in enumerate(n):
    v = int(v)
    if kuriagari:
        v += 1
    
    if v<5:
        ans += v
        kuriagari = 0
    if v>5:
        ans += 10 - v
        kuriagari = 1
    if v==5:
        if int(n[i+1])<5:
            #delete is better
            ans += v
            kuriagari = 0
        if int(n[i+1])>=5:
            #kuriagari is promising
            ans += 10 - v
            kuriagari = 1
         i v enumerate(n) enumerate n v = int(v) v int(v) int v if kuriagari:
        v += 1
    
     kuriagari v += 1 v 1 if v<5:
        ans += v
        kuriagari = 0
     v<5 v 5 ans += v ans v kuriagari = 0 kuriagari 0 if v>5:
        ans += 10 - v
        kuriagari = 1
     v>5 v 5 ans += 10 - v ans 10 - v 10 v kuriagari = 1 kuriagari 1 if v==5:
        if int(n[i+1])<5:
            #delete is better
            ans += v
            kuriagari = 0
        if int(n[i+1])>=5:
            #kuriagari is promising
            ans += 10 - v
            kuriagari = 1
         v==5 v 5 if int(n[i+1])<5:
            #delete is better
            ans += v
            kuriagari = 0
         int(n[i+1])<5 int(n[i+1]) int n[i+1] n i+1 i 1 5 ans += v ans v kuriagari = 0 kuriagari 0 if int(n[i+1])>=5:
            #kuriagari is promising
            ans += 10 - v
            kuriagari = 1
         int(n[i+1])>=5 int(n[i+1]) int n[i+1] n i+1 i 1 5 ans += 10 - v ans 10 - v 10 v kuriagari = 1 kuriagari 1 print(ans) print ans ans += v v ans kuriagari = 0 0 kuriagari kuriagari = 1 1 kuriagari ans += v v ans kuriagari = 0 0 kuriagari kuriagari=0 0 kuriagari n = input() input() n v = int(v) int(v) v kuriagari = 1 1 kuriagari n = n[::-1] + "0" n[::-1] + "0" n v += 1 1 v ans += 10 - v 10 - v ans l = len(n) len(n) l ans = 0 0 ans ans += 10 - v 10 - v ans
#E
#greedy 
n = input()
n = n[::-1] + "0"
l = len(n)

ans = 0

kuriagari=0

for i,v in enumerate(n):
    v = int(v)
    if kuriagari:
        v += 1
    
    if v<5:
        ans += v
        kuriagari = 0
    if v>5:
        ans += 10 - v
        kuriagari = 1
    if v==5:
        if int(n[i+1])<5:
            #delete is better
            ans += v
            kuriagari = 0
        if int(n[i+1])>=5:
            #kuriagari is promising
            ans += 10 - v
            kuriagari = 1
        
print(ans)