You are given a positive (greater than zero) integer n.

You have to represent n as the sum of integers (possibly negative) consisting only of ones (digits '1'). For example, 24 = 11 + 11 + 1 + 1 and 102 = 111 - 11 + 1 + 1. 

Among all possible representations, you have to find the one that uses the minimum number of ones in total.

Input

The single line contains one integer n (1 ≤ n < 10^{50}).

Output

Print one integer x — the minimum number of ones, such that there exist a representation of n as the sum of integers (possibly negative) that uses x ones in total.

Examples

Input


24


Output


6


Input


102


Output


7
memo = {} 

def solve (n):
    if n<=6 :
        return n
    elif n<=11:
        return 2+(11-n)
    elif n in memo:
        return memo[n]
    else:
        digits = [int(c) for c in str(n)]
        s = len(digits) 
        Ln = (10**s)//9
        if (n==Ln):
            return s
        
        if n<Ln:
            Ln = Ln//10
            s -=1
        Un = 10*Ln +1

        ## Ln < n < Un and Ln and Un digits are ones
        # s is the number of ones in the lower bound Ln
        # possibility 1: n = q Ln + r ==> q s + number for (r)  
        # possibility 2: n = Un - (Un - n) 
        # and Ln <= Un - n < Un ==>  solve for (Un - n)   
        len1 = solve (n%Ln) + s*(n//Ln) 
        n2 = Un-n
        len2 = solve (n2%Ln) + s*(n2//Ln)+ s+1
        ans = min(len1,len2) 

        memo[n]=ans
        return ans 

n = int(input())
print(solve(n))