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 Main {

        private static long prestej(long[] arr, long num, int id) {

            //stevilo delimo z najvecjim stevilom sestavljenim iz enk
            long k = num / arr[id];
            //stevilo se spremeni v ostanek deljenja
            num = num % arr[id];
            //izracunamo stevilo enk
            long numOfOnes = k * id;
            //če je ostanek
            if (num != 0) {
                //izberemo manjso
                return numOfOnes + Math.min(prestej(arr, num, id - 1), id + prestej(arr, arr[id] - num, id - 1));
            } else {
                //vrne stevilo enic
                return numOfOnes;
            }
        }

        public static void main(String[] args) {

            Scanner vnos = new Scanner(System.in);

            long[] arr = new long[17];
            long stevilo = vnos.nextLong();
            //stevilo enk ba zadnjem mestu tabele
            int a = 16;

            //v tabelo vstavimo stevila 1, 11, 111, ... , 1111111111111111
            for (int i = 1; i < 17; i++) {
                arr[i] = 10 * arr[i - 1] + 1;
            }

            System.out.println(prestej(arr, stevilo, 16));

        }
}
