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

import static java.lang.Math.min;
import static java.lang.Math.pow;

public class practice {

    public static void main(String[] args) throws NumberFormatException, IOException {

        Scanner scan = new Scanner(System.in);
        //System.out.println("Input your number: ");
        Long number = scan.nextLong();

        scan.close();

        System.out.println(calculate(number));
    }

    /**
     * Метод получения единиц от степени
     *
     * @param number степень
     * @return набор единиц
     */
    public static Long powOnes(Long number) {
        return (long) ((pow(10, number) - 1) / 9);
    }

    public static Long calculate(Long number) {
        if (number <= 6) {
            return number;
        }

        if (number <= 11) {
            return 13 - number;
        }

        long onesCount = 1L;

        while (powOnes(onesCount) < number) {
            onesCount += 1;
        }

        onesCount -= 1;
        Long onesSet = powOnes(onesCount);
        Long a = number;
        Long firstResult = (a / onesSet) * onesCount;
        a %= onesSet;
        firstResult += calculate(a);
        Long b = powOnes(onesCount + 1) - number;

        Long secondResult = onesCount + 1;
        secondResult += (b / onesSet) * onesCount;
        b %= onesSet;
        secondResult += calculate(b);

        return min(firstResult, secondResult);
    }
}
