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 OneArithmetic {
	static long oneArr[] = new long[17]; // n<10^15

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long n = sc.nextLong();
		oneArr[0] = 0;
		for (int i = 1; i < 17; i++) {
			oneArr[i] = oneArr[i - 1] * 10 + 1; // [0,1,11,111,...] position reference value
		}
		System.out.println(minOnes(n, 16));
	}

	static long minOnes(long n, int i) {
		long counter = i * (n / oneArr[i]);
		n = n % oneArr[i];
		if (n == 0)
			return counter;
		return counter + Math.min(i + minOnes(oneArr[i] - n, i - 1), minOnes(n, i - 1)); // decreasing and increasing to
																							// n
	}

}