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.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
  static class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    static void init(InputStream input) {
      reader = new BufferedReader(new InputStreamReader(input));
      tokenizer = new StringTokenizer("");
    }

    static String next() throws IOException {
      while (!tokenizer.hasMoreTokens()) {
        tokenizer = new StringTokenizer(reader.readLine());
      }
      return tokenizer.nextToken();
    }

    static int nextInt() throws IOException {
      return Integer.parseInt(next());
    }

    static long nextLong() throws IOException {
      return Long.parseLong(next());
    }

    static double nextDouble() throws IOException {
      return Double.parseDouble(next());
    }

    static String nextLine() {
      String str = "";
      try {
        str = reader.readLine();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return str;
    }
  }

  static long[] ones = {0, 1, 11, 111, 1111, 11111, 111111, 1111111, 11111111, 111111111, 1111111111, 11111111111L, 111111111111L, 1111111111111L, 11111111111111L, 111111111111111L, 1111111111111111L};

  public static void main(String[] args) throws Exception {
    Reader.init(System.in);
    long n = Reader.nextLong();
    long res = decompose(n);
    System.out.println(res);
  }

  public static long decompose(long n) {
    if (n <= 6) {
      return n;
    }
    long numOfDigits = getNumOfDigits(n);
    long lower, upper;
    lower = ones[(int) numOfDigits];
    if (n < lower) {
      upper = lower;
      lower = ones[(int) (numOfDigits - 1)];
    } else if (n > lower) {
      upper = ones[(int) (numOfDigits + 1)];
    } else {
      return numOfDigits;
    }
    long resLower = decompose(n % lower) + (n / lower) * getNumOfDigits(lower);
    long resUpper = Long.MAX_VALUE;
    if (upper - n < n) {
      resUpper = decompose(upper - n) + getNumOfDigits(upper);
    }
    return Math.min(resLower, resUpper);
  }

  public static long getNumOfDigits(long n) {
    return (long) Math.floor(Math.log10(n)) + 1;
  }
}
