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 oneBased {

	static long onesArray[] = new long[17];

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);

		onesArray[0] = 0;

		int i = 1;

		while (i < 17) {													// Initialisation of the array of elements made from ones
																			//[1,11,111,1111,11111,...]			
			onesArray[i] = (onesArray[i - 1] * 10) + 1;							

			i++;
		}

		System.out.println(calculate(scanner.nextLong(), 16));			//passing the input number and the position of the highest ones number

	}

	public static long calculate(long num, int ones) {

		long countOnes = ((int) (num / onesArray[ones])) * ones;       // dividing the input number with the highest ones number, and getting value of used 1s

		long remainder = num % onesArray[ones];						   // the remainder of the division 

		if (remainder == 0) {		

			return countOnes;										// if the remainder of the division is 0 we return the number of used 1s

		} else {
																	// else we return the sum of 1s so far with the mininum number between the recursive calls of the calculate function
			return countOnes
					+ Math.min(calculate(onesArray[ones] - remainder, ones - 1) + ones, calculate(remainder, ones - 1));

		}

	}

}
