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 Main {
    public static long divide(long[] ones, long number, int id) {
        long k = number / ones[id];
        number = number % ones[id];
        long numberOfOnes = k * id;
        if (number != 0) {
            return numberOfOnes + Math.min(divide(ones, number, id - 1), id + divide(ones, ones[id] - number, id - 1));
        } else {
            return numberOfOnes;
        }
    }

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        long number = input.nextLong();
        long[] ones = new long[17];
        for (int i = 1; i < 17; i++) {
            ones[i] = 10 * ones[i - 1] + 1;
        }
        System.out.println(divide(ones, number, 16));
    }
}
