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 foo {
    	
    	public static long one (long n) {
    		String num = String.valueOf(n);
    		String one = "";
    		long i = 0;
    		for(int j = 0; j < num.length() + 1; j++)
    			one = one + 1;
    		long n_one = Long.parseLong(one);
    		long k = 0;
    		long digit = 0;
    		boolean found = false;
    		while(!found) {
    			if(k < n) {
    				long a = Math.abs(n - k);
    				long b = Math.abs(k + n_one - n);
    				long c = 0;
    				if(a < b) c = b; else c = a;
    				double ratio = (b*1.0)/a;
    				if(a > b && (n_one == 1 || (Math.abs(a - b) >= Math.ceil((n_one*1.16)/11.0)) && !(i == 0 && ratio > 0.80))) {
    					k = k + n_one;
    					digit = digit + String.valueOf(n_one).length();
    					i++;
    				} else {
    					String t = String.valueOf(n_one);
    					n_one = Long.parseLong(t.substring(0, t.length() - 1));
    					i++;
    				}
    			} else if(k > n) {
    				long a = Math.abs(k - n);
    				long b = (Math.abs(k - n_one - n));
    				long c = 0;
    				if(a < b) c = b; else c = a;
    				if(a > b && (n_one == 1 || (Math.abs(a - b) >= Math.ceil((n_one*1.16)/11.0)))) {
    					k = k - n_one;
    					digit = digit + String.valueOf(n_one).length();
    					i++;
    				} else {
    					String t = String.valueOf(n_one);
    					n_one = Long.parseLong(t.substring(0, t.length() - 1));
    					i++;
    				}
    			} else {
    				found = true;
    			}
    		}
    		return digit;
    	}
    	
    	public static void main(String args[]) {
    		Scanner sc = new Scanner(System.in);
    	    long n = sc.nextLong();
    	    System.out.println(one(n));
    	}
    }

