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;
import java.util.*;
 
public class OneBasedArithmetic {
    public static void main(String[] args) {
         Scanner myObj = new Scanner(System.in);
        long n = myObj.nextLong();
        long[] ones = new long[17];
        for(int i = 1; i < 17; ++i){
            ones[i] = 10 * ones[i - 1] + 1;
        }
 
  
        long ans = dfs(ones, n, 16);
        System.out.println(ans);
    }
 
    private static long dfs(long[] ones, long n, int idx){
 
        long quot = n / ones[idx];
        n %= ones[idx];
        long onesUsedHere = quot * idx;
        if(n == 0) return onesUsedHere;
        else
            return onesUsedHere + Math.min(dfs(ones, n, idx - 1),
                    idx + dfs(ones, ones[idx] - n, idx - 1));
    }
   
}