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
import java.util.Scanner;

public class OneBasedArithmetic {

    public static long[] ones;

    public static void main(String[] args) {
    
        Scanner input = new Scanner(System.in);
        
        long x = input.nextLong();
        int xDigits = numOfDigits(x);

        ones = new long[xDigits+2];
        
        ones[0] = 0;
        for (int i = 1; i < xDigits+2; i++) {
            ones[i] = ones[i - 1] * 10 + 1;
        }
        
        long result = solve(x, xDigits+1);
        System.out.println(result);
    }
    
    private static int numOfDigits(long x) {

        int cnt = 0;

        while (x > 0) {
            x /= 10;
            cnt++;
        }

        return cnt;
    }

    private static long solve(long x, int cnt1) {
    
		/*  x = q * ones[cnt1] + r */
		long q = x / ones[cnt1];
        long r = x % ones[cnt1];
        
        long numOfOnes = cnt1 * q;
        
        if (r == 0)
            return numOfOnes;
		  
		long result1 = cnt1 + solve(ones[cnt1] - r, cnt1 - 1);
		long result2 = solve(r, cnt1 - 1);
		
        return numOfOnes + Math.min(result1, result2);
    }
}
