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
/*
If you want to aim high, aim high
Don't let that studying and grades consume you
Just live life young
******************************
If I'm the sun, you're the moon
Because when I go up, you go down
*******************************
I'm working for the day I will surpass you
https://www.a2oj.com/Ladder16.html
*/
import java.util.*;
import java.io.*;
import java.math.*;

   public class x440C
   {
      static long[] pow;
      public static void main(String omkar[]) throws Exception
      {
         BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));  
         StringTokenizer st = new StringTokenizer(infile.readLine());
         String input = st.nextToken();
         int N = input.length();
         int[] arr = new int[N];
         for(int i=0; i < N; i++)
            arr[i] = Integer.parseInt(input.charAt(i)+"");
         //brute force
         pow = new long[16];
         pow[0] = 1L;
         for(int i=1; i <= 15; i++)
            pow[i] = pow[i-1]*10;
         long start = Long.parseLong(input);
         System.out.println(solve(start));
      }
      public static int solve(long x)
      {
         if(x <= 11)
         {
            if(x == 11)
               return 2;
            else if(x == 10)
               return 3;
            else if(x == 9)
               return 4;
            else if(x == 8)
               return 5;
            else if(x == 7)
               return 6;
            else
               return (int)x;
         }
         int len = (x+"").length();
         long boof = 0L;
         for(int i=0; i <= len; i++)
            boof += pow[i];
         int res = Integer.MAX_VALUE;
         if(x > boof-x)
            res = Math.min(res, len+1+solve(boof-x));
         boof -= pow[len];
         for(int d=1; d <= 9; d++)
         {
            boof *= d;
            if(d == x/pow[len-1] || d+1 == x/pow[len-1])//(x > boof-x)
               res = Math.min(res, len*d+solve(Math.abs(boof-x)));
            boof /= d;
         }
         return res;
      }
   }