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.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class OnBasedArit {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        long n= sc.nextLong();
        long[] ena= new long[17];
        for(int i= 1; i< 17; i++){
        	ena[i]= 10* ena[i-1]+ 1;
        }
        long odg = dfs(ena, n, 16);
        System.out.println(odg);
    }

    private static long dfs(long[] ena, long n, int id){
        long del= n / ena[id];
        n %= ena[id];
        long uporabljene= del * id;
        if(n == 0) return uporabljene;
        else
        return uporabljene+ Math.min(dfs(ena, n, id- 1), id+ dfs(ena, ena[id]- n, id- 1));
    }
    private static class Beri {
        private final BufferedReader br;
        StringTokenizer st;
        public Beri() {
            br= new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st== null || !st.hasMoreElements()) {
                try{
                    st= new StringTokenizer(br.readLine());
                }catch (IOException e) {
                    e.printStackTrace();
                }
               }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String vrstica() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] polje(int n) {
            int[] polje= new int[n];
            for (int i= 0; i < n; ++i)
            	polje[i] = nextInt();
            return polje;
        }
        List<Integer> seznam(int n) {
            List<Integer> sez= new ArrayList<>();
            for (int i= 0; i< n; ++i)
                sez.add(nextInt());
            return sez;
        }
        long[] beriPolje(int n) {
            long[] bp= new long[n];
            for (int i= 0; i< n; ++i)
                bp[i]= nextLong();
            return bp;
        }
        double[] beriPolje2(int n) {
            double[] bp2= new double[n];
            for (int i= 0; i < n; ++i)
            	bp2[i]= nextDouble();
            return bp2;
    }
   }
}