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.InputStreamReader;

public class C {

  public static void main(String[] args) throws Exception {
    try (BufferedReader in = new BufferedReader(new InputStreamReader(System.in))) {
      int m = 17;
      long[] ones = new long[m];
      for (int i = 1; i < m; ++i) ones[i] = ones[i - 1] * 10 + 1;
      long n = Long.parseLong(in.readLine());
      long res = fun(n, m - 1, ones);
      System.out.println(res);
    }
  }

  private static long fun(long n, int m, long[] ones) {
    long d = n / ones[m];
    n %= ones[m];
    long res = d * m;
    if (n > 0) {
      res += Math.min(fun(n, m - 1, ones), m + fun(ones[m] - n, m - 1, ones));
    }
    return res;
  }
}