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 OneBasedArithmetic {
	static long arr[] = new long[17]; // 10^15

	public static void digits(long n) {
		long s = 0; // s for terms of series, i for n terms
		arr[0] = 0;
		for (int i = 0; i < n - 1; i++) { // generate 0, 1, 11, 111,...
			s = s * 10 + 1;
			arr[i + 1] = s; // i + 1 since 0 * 10 + 1 = 1
		}
	}

	public static long ones(int i, long number) {
		long wholeNumber = (int) (number / arr[i]);  	// number of times the current 111.. occurs *cast to int for a whole
														// value)
		long remainder = number % arr[i]; 				// the remainder of the division form the current state of the number and the
														// current 111..(how much more we have left)
		long counter = wholeNumber * i;				    // the whole number that can be divided with the number * i(number of 1 in the
														// arr[i]) to get the specific amount of ones
		if (remainder == 0)							    // stopping condition, if the number is zero
			return counter;                             // there are no ones to be added or subtracted
		else
			return counter + Math.min(i + ones(i - 1, arr[i] - remainder), ones(i - 1, remainder)); // check if a whole number(for division) is reached with adding or
																									// subtracting ones to find the minimum amount of 1's that need to be used
	}

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		long number = s.nextLong(); 		// number to be calculated
		s.close();
		digits(17);					    // initializing the array with ones
		long result = ones(16, number); 	// starting with the largest value of ones arr[16]
		System.out.println(result); 		// printing the result
	}
}