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

	static long arr[] = new long[17];
	
	public static long process(long x, int index) {
		int temp = (int) (x/arr[index]);       //temporary num of ones ex. 22/11 = 2 for index 2 ( but will start from index 16 going back so we get the biggest first)
		x= x%arr[index];                       //check if anything else left to be considered 
		long num = temp*index;                 //exact number of ones till now ex. temp = 2, index= 2 => num=4 (22=11+11)
		if(x==0) return num;
		else return num+Math.min(index + process(arr[index]-x, index-1), process(x ,index-1));  //if nothing left - return number, else recursively search
			
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long x = sc.nextLong();
		arr[0]=0;
		for(int i=1; i<=16; i++) arr[i]=arr[i-1]*10+1; // filling array of 0, 1 , 11, ... , 1111111111111111 because of constraint n < 10^15
		System.out.println(process(x,16));  //if number satisfies constraints, find number of ones
	}
	
	

	
	
}
