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.io.BufferedReader;
    import java.io.*;
    import java.util.StringTokenizer;
    
    
    /**
     * Author: Luis Manuel Díaz Barón
     * Problem:
     * Online Judge:
     * Idea:
     * Fecha: 20/02/2013
     */
    
    public class Main {
    
        static MyScanner sc;
        static MyWriter pw;
        static String filePathWin="d:\\lmo.in";
        static String filePathLinux="/media/luismo/Trabajo/lmo.in";
        public static void main(String[] args) throws IOException {
            sc = new MyScanner(System.in);
    
    //        sc = new MyScanner(new FileReader(filePathWin));
            // sc = new MyScanner(new FileReader(filePathLinux));
    
            pw = new MyWriter(System.out);
    
            while(sc.canRead())
                SolveSingleProblem();
    
            pw.close();
        }
        
        
        public static void SolveSingleProblem() throws IOException{
            
            long n = sc.nextLong();
            
            long dig = (long)Math.log10(n) + 1;
            
            long mirr = n;
            
            String s = "";
            for(int i =0; i < dig + 1; i++)
                s+="1";
            
            long k = Long.parseLong(s);
            
            long k2 = Long.parseLong(s.substring(1));
            
            long answ1 = best(n, k);
            
            long answ2 = best(n, k2);
            
            
            answ1 = min(answ1, answ2);
            
            pw.println(answ1);
        }
        
        static long best(long n, long k){
            
            if(k == 1) return n;
            
            int cumul = 0;
            int digits = (int)Math.log10(k) + 1;
            
            if(n == k){
                long ret = digits;
                return ret;
            }
            
            if(k < n){                
                
                long ret = digits + best(n - k , k);
                return ret;
            }
            
            long left = digits + best( k - n, k / 10);
            
            long right = best(n, k / 10);
            
            return min(left, right);
        }
        
        static long min(long a, long b){
            return (a < b)? a: b;
        }
        
    }
    
    class MyScanner{
        BufferedReader br;
        StringTokenizer stk;
        public MyScanner(InputStream stream){
            this.br = new BufferedReader(new InputStreamReader(stream),72357);
            this.stk = new StringTokenizer("");
        }
        public MyScanner(FileReader fr){
            this.br = new BufferedReader(fr);
            this.stk = new StringTokenizer("");
        }
        public String next() throws IOException {
            while(!stk.hasMoreElements()){
                String line = br.readLine();
                if(line == null)
                    return null;
                stk = new StringTokenizer(line);
            }
            return stk.nextToken();
        }
        public int nextInt()throws IOException{
            return Integer.parseInt(next());
        }
    
        public long nextLong()throws IOException{
            return Long.parseLong(next());
        }
    
        public double nextDouble()throws IOException{
            return Double.parseDouble(next());
        }
    
        public boolean canRead() throws IOException{
            return br.ready();
        }
    }
    
    class MyWriter{
        PrintWriter pw;
        public MyWriter(OutputStream out){
            this.pw = new PrintWriter(out);
        }
        public void print(Object obj){
            pw.print(obj.toString());
        }
        public void println(Object obj){
            pw.println(obj.toString());
        }
        public void close() {
            pw.close();
        }
    }