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 {

    public static long n;

    // contains ones (0, 1, 11, ..., 1111111111111111)
    public static long[] container = new long[17]; 
    
    public static long sum(long n, int i) {

        long x = n / container[i];        // highest possible number
        long r = n %= container[i];       // remainder 
        long counter = x * i;             // store number of 1s

        // return the counter if there is nothing else,
        // otherwise enter recursion
        return (n == 0) ? counter : Math.min(i + sum(container[i] - r, i - 1), sum(r, i - 1)) + counter;

    }
    public static void main(String[] args) { 
        
        Scanner sc = new Scanner(System.in);
        n = sc.nextLong();
        sc.close();
        container[0] = 0; 
        container[1] = 1;

        // fill the array
        for (int i = 2; i <= 16; ++i) {
            container[i] = container[i - 1] * 10 + 1;
        }

        System.out.println(sum(n, 16));
    }
}