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.io.*;

public class Main {
	public static void main(String arg[])throws Exception{
		BufferedReader bff = new BufferedReader(new InputStreamReader(System.in));
		long n = Long.parseLong(bff.readLine());
		System.out.println(ones(n));
	}
	private static int digs(long n){
		return (int) Math.log10(n) + 1;
	}
	private static long fillOne(int n){
		long res = 1;
		while(n-- > 1)
			res = res * 10 + 1;
		return res;
	}
	
	private static long ones (long n){
		if(n <= 6) return n;
		int dig = digs(n);
		long aux = fillOne(dig), one = aux, diff = n;
		int c = 0;
		//System.out.println(aux);
		while(aux < n){
			c++;
			diff = n - aux;
			aux += one;
		}
		//System.out.println(diff + " " + aux);
		long res;
		if(c > 0)
			res = Math.min(c*dig + ones(diff), ++c * dig + ones(aux - n));
		else
			res = ++c * dig + ones(aux - n);
		aux = fillOne(dig + 1);
		if(aux - n < n)
			res = Math.min(res, dig + 1 + ones(aux - n));
		return res;
	}
}
