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 {
	
	// 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 < 10^15).
	
	// Output: Print expected minimal number of digits 1.
	
	static long input;
	static int len;
	static long[] ones;				// array of ones like {}, 1, 11, 111, ...
	static long answer;
	
	public static void initializeOnesArray() {
		
		ones = new long[len+2];
		
		for (int i = 1; i < ones.length; i++)
			ones[i] = ones[i-1] * 10 + 1;
		
	}
	
	public static long FindMinOnesSum(long currentNumber, int pos) {
		
		long xTimes = currentNumber / ones[pos];		// additional number for division of our number
														// ( the amount of how many times our current
														// ones (ones[pos]) are present in our number )
		currentNumber = currentNumber % ones[pos];			// use currentNumber for another value to save space
															// as we do not need original currentNum anymore
														// ( the amount of how close we are to our number )
		
		if (currentNumber == 0)					// if it reaches the end, so we cannot 'split' it anymore
			return xTimes * pos;			// additional * current position
		
		else						// return the lowest nearest amount of ones up to now for the current (sub)number
			
									// ( return additional product for the lowest value between the quantity of 1
									// for the number and the previous position )
			
									// So we either do +ones or -ones depending on what is has smaller amount of ones
		{
			long possibleMin1 = FindMinOnesSum(currentNumber, pos-1);
			long possibleMin2 = pos + FindMinOnesSum(ones[pos] - currentNumber, pos - 1);
			return xTimes * pos + Math.min(possibleMin1, possibleMin2);						// return the closest possibility
		}
		
	}

	public static void main(String[] args) {
		
		Scanner scan = new Scanner(System.in);
		input = scan.nextLong();
		scan.close();
		
		len = (int) (Math.log10(input) + 1);		// the number of digits in our input
													// in order to optimize the program
		
		initializeOnesArray();
		
		answer = FindMinOnesSum(input, len+1);		// we insert ++len !
		System.out.println(answer);

	}

}
