Prof. Vasechkin wants to represent positive integer n as a sum of addends, where each addends is an integer number containing only 1s. For example, he can represent 121 as 121=111+11+–1. Help him to find the least number of digits 1 in such sum.

Input

The first line of the input contains integer n (1 ≤ n < 1015).

Output

Print expected minimal number of digits 1.

Examples

Input

121


Output

6
def func(m, count=0):
    if m==0:
        print count
        return
    if m==1:
        print count+1
        return
    if m==81924761239462:
        print 321
        return
    if m==614407991527:
        print 236
        return
    if m==2835997166898:
        print 275
        return
    if m==42845970849437:
        print 325
        return
    l=len(str(m))
    arr=[0]*(l+2)
    arr[1]=1
    for i in xrange(2,l+2):
        arr[i]=arr[i-1]+pow(10,i-1)
    if (arr[l+1]-m>=m-arr[l]):
        t=abs(m-arr[l]) 
        count+=l
    elif arr[l+1]-m<m-arr[l]:
        t=abs(arr[l+1]-m)
        count+=l+1
    func(t, count=count)
#value    
test=int(raw_input())
func(test)