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.ArrayList;
import java.util.Scanner;

public class Main {

	static long n;
	static int length;
	static long[] numbers;
	static long result;
	static int countOnes = 0;
	static int step = 0;
	static ArrayList<Integer> doubts = new ArrayList<Integer>();
	static boolean flag = false;
	static boolean flag2 = true;

	public static void main(String[] args) {

		// MUST AT BEGINNING
		Scanner sc = new Scanner(System.in);
		n = Math.abs(sc.nextLong());
		sc.close();
		result = n;
		length = String.valueOf(Math.abs(n)).length();
		numbers = new long[length + 2];
		numbers[0] = 1;
		// numbers = [1, 11, 111, 1111, ...]
		for (int i = 1; i < numbers.length; i++) {
			numbers[i] = numbers[i - 1] * 10 + 1;
		}
		// MAIN THING

		int firstCount = doTask();

		if (doubts.size() == 0) {
			System.out.println(firstCount);
		} else {
			reset();
			int secondCount = doTask();
			System.out.println(Math.min(firstCount, secondCount));
		}

	}

	public static int IndexofClosest(long num) {
		step++;
		long differenceTop = numbers[length] - num;
		long differenceBottom = num - numbers[length - 1];
		long closest = Math.min(differenceBottom, differenceTop);

		if (doubt(differenceBottom, differenceTop)) {
			doubts.add(step);
		}

		if (differenceTop == differenceBottom) {
			return length - 1;
		}

		if (closest == differenceTop) {
			if (flag && doubts.size() != 0 && flag2) {
				if (doubts.get(0) == step) {
					doubts.remove(0);
					flag2=false;
					return length - 1;
					
				}
			}
			return length;

		} else {
			if (flag && doubts.size() != 0 && flag2) {
				if (doubts.get(0) == step) {
					doubts.remove(0);
					return length;
				}
			}
			return length - 1;
		}
	}

	public static boolean doubt(long differenceBottom, long differenceTop) {
		long max = Math.max(differenceBottom, differenceTop);
		long min = Math.min(differenceBottom, differenceTop);
		int lengthMax = String.valueOf(max).length() - 1;
		long tolerance = (long) Math.pow(10.0, lengthMax * 1.0);
		if (((max - tolerance) > min) || ((min + tolerance) < max)) {
			return false;
		} else {
			return true;
		}
	}

	public static void reset() {
		result = n;
		countOnes = 0;
		step = 0;
		flag = true;
	}

	public static int doTask() {
		if (flag) {

		}
		while (result != 0) {

			length = String.valueOf(Math.abs(result)).length();
			int indexofClosest = IndexofClosest(result);
			long closest = numbers[indexofClosest];
			countOnes = countOnes + (indexofClosest + 1);
			if (result >= closest) {
				result = result - closest;
			} else {
				result = closest - result;
			}
			if (result == 1) {
				countOnes++;
				break;
			}
		}
		return countOnes;
	}

}