###綺麗になったよ！！！！！
def y_solver(tmp):
    l=len(tmp)
    rev=tmp[::-1]+"0"
    ans=0
    next_bit=0
    kuri_cal=0
    for i in range(l-1):
        num=int(rev[i])+next_bit
        next_num=int(rev[i+1])
        if (num<5) or (num==5 and next_num<5):
            ans+=(kuri_cal+num)
            kuri_cal=0
            next_bit=0
        else:
            kuri_cal+=10-num
            next_bit=1 
    
    last=int(tmp[0])+next_bit
    ans+=kuri_cal+min(last,11-last)
    return ans

n=input()
ans=y_solver(n)
print(ans) def y_solver(tmp):
    l=len(tmp)
    rev=tmp[::-1]+"0"
    ans=0
    next_bit=0
    kuri_cal=0
    for i in range(l-1):
        num=int(rev[i])+next_bit
        next_num=int(rev[i+1])
        if (num<5) or (num==5 and next_num<5):
            ans+=(kuri_cal+num)
            kuri_cal=0
            next_bit=0
        else:
            kuri_cal+=10-num
            next_bit=1 
    
    last=int(tmp[0])+next_bit
    ans+=kuri_cal+min(last,11-last)
    return ans y_solver l=len(tmp) l len(tmp) len tmp rev=tmp[::-1]+"0" rev tmp[::-1]+"0" tmp[::-1] tmp ::-1 -1 "0" ans=0 ans 0 next_bit=0 next_bit 0 kuri_cal=0 kuri_cal 0 for i in range(l-1):
        num=int(rev[i])+next_bit
        next_num=int(rev[i+1])
        if (num<5) or (num==5 and next_num<5):
            ans+=(kuri_cal+num)
            kuri_cal=0
            next_bit=0
        else:
            kuri_cal+=10-num
            next_bit=1 
    
     i range(l-1) range l-1 l 1 num=int(rev[i])+next_bit num int(rev[i])+next_bit int(rev[i]) int rev[i] rev i next_bit next_num=int(rev[i+1]) next_num int(rev[i+1]) int rev[i+1] rev i+1 i 1 if (num<5) or (num==5 and next_num<5):
            ans+=(kuri_cal+num)
            kuri_cal=0
            next_bit=0
        else:
            kuri_cal+=10-num
            next_bit=1 
    
     (num<5) or (num==5 and next_num<5) num<5 num 5 num==5 and next_num<5 num==5 num 5 next_num<5 next_num 5 ans+=(kuri_cal+num) ans kuri_cal+num kuri_cal num kuri_cal=0 kuri_cal 0 next_bit=0 next_bit 0 kuri_cal+=10-num kuri_cal 10-num 10 num next_bit=1 next_bit 1 last=int(tmp[0])+next_bit last int(tmp[0])+next_bit int(tmp[0]) int tmp[0] tmp 0 next_bit ans+=kuri_cal+min(last,11-last) ans kuri_cal+min(last,11-last) kuri_cal min(last,11-last) min last 11-last 11 last return ans ans tmp tmp n=input() n input() input ans=y_solver(n) ans y_solver(n) y_solver n print(ans) print ans ans=y_solver(n) y_solver(n) ans def y_solver(tmp):
    l=len(tmp)
    rev=tmp[::-1]+"0"
    ans=0
    next_bit=0
    kuri_cal=0
    for i in range(l-1):
        num=int(rev[i])+next_bit
        next_num=int(rev[i+1])
        if (num<5) or (num==5 and next_num<5):
            ans+=(kuri_cal+num)
            kuri_cal=0
            next_bit=0
        else:
            kuri_cal+=10-num
            next_bit=1 
    
    last=int(tmp[0])+next_bit
    ans+=kuri_cal+min(last,11-last)
    return ans def y_solver(tmp):
    l=len(tmp)
    rev=tmp[::-1]+"0"
    ans=0
    next_bit=0
    kuri_cal=0
    for i in range(l-1):
        num=int(rev[i])+next_bit
        next_num=int(rev[i+1])
        if (num<5) or (num==5 and next_num<5):
            ans+=(kuri_cal+num)
            kuri_cal=0
            next_bit=0
        else:
            kuri_cal+=10-num
            next_bit=1 
    
    last=int(tmp[0])+next_bit
    ans+=kuri_cal+min(last,11-last)
    return ans y_solver n=input() input() n
###綺麗になったよ！！！！！
def y_solver(tmp):
    l=len(tmp)
    rev=tmp[::-1]+"0"
    ans=0
    next_bit=0
    kuri_cal=0
    for i in range(l-1):
        num=int(rev[i])+next_bit
        next_num=int(rev[i+1])
        if (num<5) or (num==5 and next_num<5):
            ans+=(kuri_cal+num)
            kuri_cal=0
            next_bit=0
        else:
            kuri_cal+=10-num
            next_bit=1 
    
    last=int(tmp[0])+next_bit
    ans+=kuri_cal+min(last,11-last)
    return ans

n=input()
ans=y_solver(n)
print(ans)