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

public class C {

    static HashMap<Long, Long> memo=new HashMap<Long, Long>();
    
    static long solve(long l) {
        if(l==0) {
            return 0;
        } else if(l==1) {
            return 1;
        }
        if(memo.containsKey(l)) {
            return memo.get(l);
        }
        StringBuilder s=new StringBuilder(); s.append("1");
        while(Long.parseLong(s.toString())<l) {
            s.append("1");
        }
        
        
        long l1=Math.abs(Long.parseLong(s.toString().substring(0, s.length()-1))-l);
        long l2=Math.abs(Long.parseLong(s.toString())-l);
        long ret=solve(l1)+s.length()-1;
        if(l2<l) {
            ret=Math.min(ret, solve(l2)+s.length());
        }
        memo.put(l, ret);
        return ret;
    }
    
    static StringBuilder sb=new StringBuilder();
    static void parse(String l, int[] here) {
        sb.setLength(0);
        int len=l.length(), j=0;
        for (int i=0; i<len; i++) {
            char c=l.charAt(i);
            if(c==' ') {
                here[j++]=Integer.parseInt(sb.toString());
                sb.setLength(0);
            } else {
                sb.append(c);
            }
        }
        if(sb.length()>0) {
            here[j++]=Integer.parseInt(sb.toString());
        }
    }
    
    static void run_stream(InputStream ins) throws IOException {
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        long n=Long.parseLong(br.readLine());
        System.out.println(solve(n));
    }

    public static void main(String[] args) throws IOException {
        run_stream(System.in);
    }

}
