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 Ones () {   //constructor
		this.arr[0]=0;
		int i = 1;
		while (i<17) {
			this.arr[i] = this.arr[i-1]*10+1;   // filling array of 0, 1 , 11, ... , 1111111111111111 because of constraint n < 10^15
			i++;
		}
	}
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		Ones p = new Ones();
		long x = sc.nextLong();
		System.out.println(process(x,16));  //if number satisfies constraints, find number of ones
	}
	
	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)
		long num = temp*index;                 //exact number of ones till now ex. temp = 2, index= 2 => num=4 (22=11+11)
		x= x%arr[index];                       //check if anything else left to be considered 
		return (x==0) ? num : (num+Math.min(index + process(arr[index]-x, index-1), process(x ,index-1)));  //if nothing left - return number, else recursively search
			
	}
	
	
}