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;
import java.lang.Math.*;
public class Main_C {
    static long n;
    private static Scanner enter;
    
    static long[] a;
    public static void main(String[] args) {
        enter=new Scanner(System.in);
        n=enter.nextLong();
        a=new long[20];
        a[1] = 1;
        for(int i = 2;i <= 16 ;i ++ ) {
            a[i] = a[i-1] *10 + 1;
        }
        System.out.println(sol(n));
    }
    static int sol (long l) {
        int i = 16;
        int r = 0;
        if(l <= 11) {
            return (int) Math.min(l,2+11-l);
        }
        while(a[i-1] > l)i--;
        r = (int) (l/a[i-1]) *(i-1) + sol(l%(a[i-1]));
        if(2*l<a[i])
            return r;
        return Math.min(r, i + sol(a[i] - l));
        
    }
}
