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.*;
public class Main {

	static long RecFun(long number, int startIndex){
		
		long one_num = create_one_num(startIndex);
		int k=(int) (number/one_num);
		number%=one_num;
		long count=k*startIndex;
		if(number==0) return count;
		else
			return count+Math.min(startIndex + RecFun((one_num-number),startIndex -1),RecFun(number,startIndex -1));
	}
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		long number =scan.nextLong();
		int startIndex = CountDigit(number) + 1;
		System.out.println(RecFun(number,startIndex));
	}
	
	static int CountDigit(long n ) {
		int count = 0;
		while(n != 0) {
			n /= 10;
			count++;
		}
		return (count);
	}
	static long create_one_num(long digit_num) {
		double c=1;
		int i ;
		for ( i=1; i< digit_num ; i++) {
			c = c + (Math.pow(10, i));
		}
		return ((long)c);
	}
 
}