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;
import java.lang.Math;

public class arithmetic {
	static long [] valuesOnes = new long[17]; //17 positions needed for the array of values of ones

	public static void main(String[] args) {
		valuesOnes[0] = 0;
		for(int i = 1; i <= 16; i++) {
			valuesOnes[i]=valuesOnes[i-1]*10 +1; //pos. 0 is 0, pos. 1 is 1, pos. 2 is 11 ... - position gives number of ones for the value
		}
		
		Scanner scan = new Scanner(System.in);
		//System.out.println("Enter an integer: ");
		long n = scan.nextLong();
		scan.close();
		long sum = sumOnes(16, n); //beginning with value containing most ones and continuing to find the minimum sum
		System.out.println(sum);
	}
	
	public static long sumOnes(int pos, long n) {
		int times = (int) (n/valuesOnes[pos]); //how many times the value of ones occurs in the number
		
		long remains = n % valuesOnes[pos]; //how much there remains between our input and the current value of ones
		
		int sumOfOnes = times * pos; //getting the number of ones by using its position num from the array
		
		if(remains == 0) {
			return sumOfOnes; //and we are done
		} else { //otherwise we continue to see if we should add another value of ones or it is more efficient if we subtract to get the minimum sum of ones
			
			long case1 = sumOnes(pos - 1, remains);
			long case2 = pos + sumOnes(pos - 1, valuesOnes[pos]-remains);
			
			return sumOfOnes + Math.min(case1, case2); 
		}
	}

}