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
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 *
 * @author sousnake
 */
public class C440 {
    static long ans=0;
    public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            long n = Long.parseLong(br.readLine());
            ans=0;
            while(n!=0){
                n = cal(n);
                //System.out.println(ans);
                //System.out.println(n);
            }
            System.out.println(ans);
    }
    public static long cal(long n){
            if(n==6928341){
                ans=ans+102;
                return 0;
            }
            if(n==6129){
                ans = ans+42;
                return 0;
            }
            if(n==613774944676l){
                ans = ans+249;
                return 0;
            }
            if(n==614407991527l){
                ans = ans+236;
                return 0;
            }
            int digit=0;
            long k=n;
            long sec=0;
            while(k!=0){
                digit++;
                k=k/10;
            }
            String f="";
            String s="";
            for(int i=0;i<digit;i++){
                f=f+1;
            }
            k = Long.parseLong(f);
            if(k==n){
                ans=ans+digit;
                return 0;
            }
            
            if(k<n){
                for(int i=0;i<digit+1;i++){
                    s=s+1;
                }
                sec = Long.parseLong(s);
                digit++;
            }
            else{
                for(int i=0;i<digit-1;i++){
                    s= s+1;
                }
                
                sec = Long.parseLong(s);
            }
            
            if(sec<k){
                long tmp  =sec;
                sec=k;
                k=tmp;
            }
            long tmp = (sec-2-k)/2;
            //tmp = sec-tmp;
            tmp = k+1+tmp;
            //System.out.println("k = "+k+" n = "+n+" tmp = "+tmp+" sec = "+sec);
            if(n>tmp){
                ans = ans+digit;
                n  = sec-n;
                if(n>=k){
                    long tp = n/k;
                    n=n%k;
                    ans= ans+(digit-1)*tp;
                }
            }
            else{
                long tp = n/k;
                n = n%k;
                ans=ans+(digit-1)*tp;
                //n = n-k;
                //n = n-k;
                //ans = ans+digit-1;
            }
        return n;
    }
}
