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 OBADef
{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        long num = scan.nextLong();
        System.out.println(""+prog(num,(int)(Math.log10(Math.abs(num)))+1));
    }
    public static long genUnos(long n){
        return (long)(Math.pow(10,n)-1)/9;
    }
    public static long prog(long n, int tam){
        long unos=genUnos(1+tam);
        long veces=n/unos*(1+tam);
        n%=unos;
        if(n==0) return veces;
        return veces+Math.min((1+tam)+prog(unos-n,tam-1),prog(n,tam-1));
        }
}