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 oneBasedArithmetic {
    public static void main(String[] args){
        Scanner sc= new Scanner(System.in);  
        long n= sc.nextLong();                         //reading the input value
        sc.close();
        long smallestOnesNum= Long.parseLong("1".repeat(String.valueOf(n).length()+1)); 
        //step above gets the smallest # made of ones which may contribute to solution, call it 1#
        System.out.println(ones(n,smallestOnesNum));
    }

    public static int ones(long n, long onesNum){
        int len= String.valueOf(onesNum).length();     //checking on which 1# we currently are
        int timesClosest= (int) (n/onesNum);           //how many times does 1# appear in input
        long gap= n%onesNum;                           //what is the gap between input and 1# added to itself needed number of times
        int onesCount= len*timesClosest;               //counting number of ones needed to get close to input via 1#
        if(gap==0){ 
            return onesCount;                          //exit condition a.k.a. we can reach input solely by repeating 1# 
        }
        long jump= (long) Math.pow(10,len-1);          //need to decrease 1# for recusive step
        return onesCount+Math.min(ones(gap,onesNum-jump),len+ones(onesNum-gap,onesNum-jump)); 
        //recursion checks whether it is optimal to simply add until filling the gap or first add additional 1# and subtract from there
    }
}
