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


public class c12 {

    public static void main(String[] args)throws Exception {
        long n=ll();
        System.out.println(solve(n));
    }

    private static long solve(long n) {
        if (n<=10){
            return Math.min(n,2+(11-n));
        }
        long h=geth(n);
        long l=getl(n);
        long c1=((h+"").length())+Math.abs(((h-n)/l)*(l+"").length())+solve(Math.abs((h-n))%l);
        long c2=Math.abs(((n)/l)*(l+"").length())+solve(Math.abs((n))%l);
        return Math.min(c1,c2);
    }

    private static long geth(long n) {
        String s=n+"";
        long t1=get(s.length());
        if (t1>=n)return t1;
        else return get(s.length()+1);
    }

    private static long getl(long n) {
        String s=n+"";
        long t1=get(s.length());
        if (t1<n)return t1;
        return get(s.length()-1);
    }
    static long get(int l){
        switch (l){
        case 1:return 1;case 4:return 1111;
        case 2:return 11;case 5:return 11111;
        case 3:return 111;case 6:return 111111;
        case 7:return 1111111;case 10:return 1111111111;
        case 8:return 11111111;case 11:return 11111111111l;
        case 9:return 111111111;case 12:return 111111111111l;
        case 13:return 1111111111111l;
        case 14:return 11111111111111l;
        case 15:return 111111111111111l;
        case 16:return 1111111111111111l;
        default :return 11111111111111111l;
        
        }
    }

    static BufferedReader buf=new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static int in() throws IOException{
        if (st==null || !st.hasMoreTokens()){
            st=new StringTokenizer(buf.readLine());
        }
        return Integer.parseInt(st.nextToken());
    }
    static long ll() throws IOException{
        if (st==null || !st.hasMoreTokens()){
            st=new StringTokenizer(buf.readLine());
        }
        return Long.parseLong(st.nextToken());
    }
    static String str() throws IOException{
        if (st==null || !st.hasMoreTokens()){
            st=new StringTokenizer(buf.readLine());
        }
        return (st.nextToken());
    }
        static double dub() throws IOException{
            if (st==null || !st.hasMoreTokens()){
                st=new StringTokenizer(buf.readLine());
            }
            return Double.parseDouble(st.nextToken());
    }
}
