n = str(input())
n_list = list(reversed(n))
n_list.append('0')
sum = 0
kuriagari = False
for i in range(len(n_list)):
    num = int(n_list[i])
    if(kuriagari):
        num+=1
        kuriagari=False
    
    if(num==10):
        num=0
        kuriagari=True

    if(num<5):
        sum+=num
    else:
        if(num>5):
            sum+=10-num
            kuriagari=True
        else:
            sum+=5
            if(int(n_list[i+1])>=5):
                kuriagari=True

print(sum)

 n = str(input()) n str(input()) str input() input n_list = list(reversed(n)) n_list list(reversed(n)) list reversed(n) reversed n n_list.append('0') n_list.append n_list append '0' sum = 0 sum 0 kuriagari = False kuriagari False for i in range(len(n_list)):
    num = int(n_list[i])
    if(kuriagari):
        num+=1
        kuriagari=False
    
    if(num==10):
        num=0
        kuriagari=True

    if(num<5):
        sum+=num
    else:
        if(num>5):
            sum+=10-num
            kuriagari=True
        else:
            sum+=5
            if(int(n_list[i+1])>=5):
                kuriagari=True i range(len(n_list)) range len(n_list) len n_list num = int(n_list[i]) num int(n_list[i]) int n_list[i] n_list i if(kuriagari):
        num+=1
        kuriagari=False
    
     kuriagari num+=1 num 1 kuriagari=False kuriagari False if(num==10):
        num=0
        kuriagari=True

     num==10 num 10 num=0 num 0 kuriagari=True kuriagari True if(num<5):
        sum+=num
    else:
        if(num>5):
            sum+=10-num
            kuriagari=True
        else:
            sum+=5
            if(int(n_list[i+1])>=5):
                kuriagari=True num<5 num 5 sum+=num sum num if(num>5):
            sum+=10-num
            kuriagari=True
        else:
            sum+=5
            if(int(n_list[i+1])>=5):
                kuriagari=True num>5 num 5 sum+=10-num sum 10-num 10 num kuriagari=True kuriagari True sum+=5 sum 5 if(int(n_list[i+1])>=5):
                kuriagari=True int(n_list[i+1])>=5 int(n_list[i+1]) int n_list[i+1] n_list i+1 i 1 5 kuriagari=True kuriagari True print(sum) print sum n_list = list(reversed(n)) list(reversed(n)) n_list sum+=5 5 sum sum = 0 0 sum kuriagari = False False kuriagari kuriagari=True True kuriagari num+=1 1 num kuriagari=False False kuriagari n = str(input()) str(input()) n kuriagari=True True kuriagari kuriagari=True True kuriagari num = int(n_list[i]) int(n_list[i]) num sum+=num num sum sum+=10-num 10-num sum num=0 0 num
n = str(input())
n_list = list(reversed(n))
n_list.append('0')
sum = 0
kuriagari = False
for i in range(len(n_list)):
    num = int(n_list[i])
    if(kuriagari):
        num+=1
        kuriagari=False
    
    if(num==10):
        num=0
        kuriagari=True

    if(num<5):
        sum+=num
    else:
        if(num>5):
            sum+=10-num
            kuriagari=True
        else:
            sum+=5
            if(int(n_list[i+1])>=5):
                kuriagari=True

print(sum)


