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 OneBasedArithmetic {
    public static void main(String[] args) {
        FastReader sc = new FastReader();
        long n = sc.nextLong();
        long[] ones = new long[17];
        for(int i = 1; i < 17; ++i){
            ones[i] = 10 * ones[i - 1] + 1;
        }

        //System.out.println(Arrays.toString(ones));
        long ans = dfs(ones, n, 16);
        System.out.println(ans);
    }

    private static long dfs(long[] ones, long n, int idx){

        //System.out.println(n +", " + idx);
        long quot = n / ones[idx];
        n %= ones[idx];
        long onesUsedHere = quot * idx;
        //System.out.println("q : " + quot +", n : " + n);
        if(n == 0) return onesUsedHere;
        else
            return onesUsedHere + Math.min(dfs(ones, n, idx - 1),
                    idx + dfs(ones, ones[idx] - n, idx - 1));
    }
    private static class FastReader {
        private final BufferedReader br;
        StringTokenizer st;

        public FastReader() {
            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 nextLine() {
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }

        int[] readArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; ++i)
                arr[i] = nextInt();

            return arr;
        }

        List<Integer> readList(int n) {
            List<Integer> list = new ArrayList<>();
            for (int i = 0; i < n; ++i)
                list.add(nextInt());
            return list;
        }

        long[] readLongArray(int n) {
            long[] arr = new long[n];
            for (int i = 0; i < n; ++i)
                arr[i] = nextLong();

            return arr;
        }

        double[] readDoubleArray(int n) {
            double[] arr = new double[n];
            for (int i = 0; i < n; ++i)
                arr[i] = nextDouble();
            return arr;
        }
    }
}
