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 oneBased {
    static int DP[];
    static int size = Integer.MAX_VALUE / 100;

    public static int rec(long x) {

        if (x == 0)
            return 0;
        if (x <= size && DP[((int) x) - 1] != 0)
            return DP[((int) x) - 1];
        int res = 0;
        long counter = x;
        int numOfBits = 0;
        String onesString = "";
        while (counter > 0) {
            onesString += "1";
            counter /= 10;
            numOfBits++;
        }

        long ones = Long.parseLong(onesString);
        long ones2 = Long.parseLong((onesString + "1"));
        long reminder = (ones > x) ? ones - x : x - ones;
        long reminder2 = ones2 - x;
        int res1 = rec(reminder);
        res1 += numOfBits;
        int res2 = Integer.MAX_VALUE;
        if (reminder2 < x) {
            res2 = rec(reminder2);
            res2 += numOfBits + 1;
        }
        res = Math.min(res1, res2);
        if (x <= size)
            DP[((int) x) - 1] = res;
        return res;
    }

    

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long x = sc.nextLong();
        DP = new int[size];
        // System.out.println(rec(x));
        long x1 = Long.parseLong("81924761239462");
        long x2 = Long.parseLong("11111111111111");
        System.out.println(rec(x));

    }
    // 81924761239462

}
