import java.util.*;
 
import java.io.*;
 
public class Main{

    public static long Combination(long n, long r){
        long A = 1;
        long B = 1;
        for(long i = 0; i < r; i++){
            A = A*(n-i);
            B = B*(r-i);
        }
        if(A == 0) return 0;
        return A/B;
    }

    public static long solve(int i, int j, boolean check, int N, String S){
        if(i == N){
            if(j == 0){
                return 1;
            }else{
                return 0;
            }
        }

        if(j == 0) return 1;

        if(check){
            return Combination(N-i, j)*(long)Math.pow(9, j);
        }else{
            if(S.charAt(i) == '0') return solve(i+1, j, false, N, S);
            long Z = solve(i+1, j, true, N, S);
            long A = solve(i+1, j-1, true, N, S)*(S.charAt(i)-'1');
            long P = solve(i+1, j-1, false, N, S);
            return Z+A+P;
        }


    }
    
    public static long Dflag(long a){
        if(a == 1) return 1;
        long h = Dflag(a/2);
        return h + h + 1;
    }
	public static void main(String[] $){
		Scanner s = new Scanner(System.in);
        long H = s.nextLong();
        

        System.out.println(Dflag(H));
	}
}  import java.util.*; util.* java import java.io.*; io.* java public class Main{

    public static long Combination(long n, long r){
        long A = 1;
        long B = 1;
        for(long i = 0; i < r; i++){
            A = A*(n-i);
            B = B*(r-i);
        }
        if(A == 0) return 0;
        return A/B;
    }

    public static long solve(int i, int j, boolean check, int N, String S){
        if(i == N){
            if(j == 0){
                return 1;
            }else{
                return 0;
            }
        }

        if(j == 0) return 1;

        if(check){
            return Combination(N-i, j)*(long)Math.pow(9, j);
        }else{
            if(S.charAt(i) == '0') return solve(i+1, j, false, N, S);
            long Z = solve(i+1, j, true, N, S);
            long A = solve(i+1, j-1, true, N, S)*(S.charAt(i)-'1');
            long P = solve(i+1, j-1, false, N, S);
            return Z+A+P;
        }


    }
    
    public static long Dflag(long a){
        if(a == 1) return 1;
        long h = Dflag(a/2);
        return h + h + 1;
    }
	public static void main(String[] $){
		Scanner s = new Scanner(System.in);
        long H = s.nextLong();
        

        System.out.println(Dflag(H));
	}
} Main public static long Combination(long n, long r){
        long A = 1;
        long B = 1;
        for(long i = 0; i < r; i++){
            A = A*(n-i);
            B = B*(r-i);
        }
        if(A == 0) return 0;
        return A/B;
    } Combination {
        long A = 1;
        long B = 1;
        for(long i = 0; i < r; i++){
            A = A*(n-i);
            B = B*(r-i);
        }
        if(A == 0) return 0;
        return A/B;
    } long A = 1; A 1 long B = 1; B 1 for(long i = 0; i < r; i++){
            A = A*(n-i);
            B = B*(r-i);
        } long i = 0; long i = 0; i 0 i < r i r i++ i++ i {
            A = A*(n-i);
            B = B*(r-i);
        } {
            A = A*(n-i);
            B = B*(r-i);
        } A = A*(n-i) A A*(n-i) A (n-i) n i B = B*(r-i) B B*(r-i) B (r-i) r i if(A == 0) return 0; A == 0 A 0 return 0; 0 return A/B; A/B A B long n n long r r public static long solve(int i, int j, boolean check, int N, String S){
        if(i == N){
            if(j == 0){
                return 1;
            }else{
                return 0;
            }
        }

        if(j == 0) return 1;

        if(check){
            return Combination(N-i, j)*(long)Math.pow(9, j);
        }else{
            if(S.charAt(i) == '0') return solve(i+1, j, false, N, S);
            long Z = solve(i+1, j, true, N, S);
            long A = solve(i+1, j-1, true, N, S)*(S.charAt(i)-'1');
            long P = solve(i+1, j-1, false, N, S);
            return Z+A+P;
        }


    } solve {
        if(i == N){
            if(j == 0){
                return 1;
            }else{
                return 0;
            }
        }

        if(j == 0) return 1;

        if(check){
            return Combination(N-i, j)*(long)Math.pow(9, j);
        }else{
            if(S.charAt(i) == '0') return solve(i+1, j, false, N, S);
            long Z = solve(i+1, j, true, N, S);
            long A = solve(i+1, j-1, true, N, S)*(S.charAt(i)-'1');
            long P = solve(i+1, j-1, false, N, S);
            return Z+A+P;
        }


    } if(i == N){
            if(j == 0){
                return 1;
            }else{
                return 0;
            }
        } i == N i N {
            if(j == 0){
                return 1;
            }else{
                return 0;
            }
        } if(j == 0){
                return 1;
            }else{
                return 0;
            } j == 0 j 0 {
                return 1;
            } return 1; 1 {
                return 0;
            } return 0; 0 if(j == 0) return 1; j == 0 j 0 return 1; 1 if(check){
            return Combination(N-i, j)*(long)Math.pow(9, j);
        }else{
            if(S.charAt(i) == '0') return solve(i+1, j, false, N, S);
            long Z = solve(i+1, j, true, N, S);
            long A = solve(i+1, j-1, true, N, S)*(S.charAt(i)-'1');
            long P = solve(i+1, j-1, false, N, S);
            return Z+A+P;
        } check {
            return Combination(N-i, j)*(long)Math.pow(9, j);
        } return Combination(N-i, j)*(long)Math.pow(9, j); Combination(N-i, j)*(long)Math.pow(9, j) Combination(N-i, j) Combination N-i N i j (long)Math.pow(9, j) Math.pow Math 9 j {
            if(S.charAt(i) == '0') return solve(i+1, j, false, N, S);
            long Z = solve(i+1, j, true, N, S);
            long A = solve(i+1, j-1, true, N, S)*(S.charAt(i)-'1');
            long P = solve(i+1, j-1, false, N, S);
            return Z+A+P;
        } if(S.charAt(i) == '0') return solve(i+1, j, false, N, S); S.charAt(i) == '0' S.charAt(i) S.charAt S i '0' return solve(i+1, j, false, N, S); solve(i+1, j, false, N, S) solve i+1 i 1 j false N S long Z = solve(i+1, j, true, N, S); Z solve(i+1, j, true, N, S) solve i+1 i 1 j true N S long A = solve(i+1, j-1, true, N, S)*(S.charAt(i)-'1'); A solve(i+1, j-1, true, N, S)*(S.charAt(i)-'1') solve(i+1, j-1, true, N, S) solve i+1 i 1 j-1 j 1 true N S (S.charAt(i)-'1') S.charAt(i) S.charAt S i '1' long P = solve(i+1, j-1, false, N, S); P solve(i+1, j-1, false, N, S) solve i+1 i 1 j-1 j 1 false N S return Z+A+P; Z+A+P Z+A+P Z A P int i i int j j boolean check check int N N String S S public static long Dflag(long a){
        if(a == 1) return 1;
        long h = Dflag(a/2);
        return h + h + 1;
    } Dflag {
        if(a == 1) return 1;
        long h = Dflag(a/2);
        return h + h + 1;
    } if(a == 1) return 1; a == 1 a 1 return 1; 1 long h = Dflag(a/2); h Dflag(a/2) Dflag a/2 a 2 return h + h + 1; h + h + 1 h + h + 1 h h 1 long a a public static void main(String[] $){
		Scanner s = new Scanner(System.in);
        long H = s.nextLong();
        

        System.out.println(Dflag(H));
	} main {
		Scanner s = new Scanner(System.in);
        long H = s.nextLong();
        

        System.out.println(Dflag(H));
	} Scanner s = new Scanner(System.in); s new Scanner(System.in) long H = s.nextLong(); H s.nextLong() s.nextLong s System.out.println(Dflag(H)) System.out.println System.out System System.out Dflag(H) Dflag H String[] $ $ public class Main{

    public static long Combination(long n, long r){
        long A = 1;
        long B = 1;
        for(long i = 0; i < r; i++){
            A = A*(n-i);
            B = B*(r-i);
        }
        if(A == 0) return 0;
        return A/B;
    }

    public static long solve(int i, int j, boolean check, int N, String S){
        if(i == N){
            if(j == 0){
                return 1;
            }else{
                return 0;
            }
        }

        if(j == 0) return 1;

        if(check){
            return Combination(N-i, j)*(long)Math.pow(9, j);
        }else{
            if(S.charAt(i) == '0') return solve(i+1, j, false, N, S);
            long Z = solve(i+1, j, true, N, S);
            long A = solve(i+1, j-1, true, N, S)*(S.charAt(i)-'1');
            long P = solve(i+1, j-1, false, N, S);
            return Z+A+P;
        }


    }
    
    public static long Dflag(long a){
        if(a == 1) return 1;
        long h = Dflag(a/2);
        return h + h + 1;
    }
	public static void main(String[] $){
		Scanner s = new Scanner(System.in);
        long H = s.nextLong();
        

        System.out.println(Dflag(H));
	}
} public class Main{

    public static long Combination(long n, long r){
        long A = 1;
        long B = 1;
        for(long i = 0; i < r; i++){
            A = A*(n-i);
            B = B*(r-i);
        }
        if(A == 0) return 0;
        return A/B;
    }

    public static long solve(int i, int j, boolean check, int N, String S){
        if(i == N){
            if(j == 0){
                return 1;
            }else{
                return 0;
            }
        }

        if(j == 0) return 1;

        if(check){
            return Combination(N-i, j)*(long)Math.pow(9, j);
        }else{
            if(S.charAt(i) == '0') return solve(i+1, j, false, N, S);
            long Z = solve(i+1, j, true, N, S);
            long A = solve(i+1, j-1, true, N, S)*(S.charAt(i)-'1');
            long P = solve(i+1, j-1, false, N, S);
            return Z+A+P;
        }


    }
    
    public static long Dflag(long a){
        if(a == 1) return 1;
        long h = Dflag(a/2);
        return h + h + 1;
    }
	public static void main(String[] $){
		Scanner s = new Scanner(System.in);
        long H = s.nextLong();
        

        System.out.println(Dflag(H));
	}
} Main
import java.util.*;
 
import java.io.*;
 
public class Main{

    public static long Combination(long n, long r){
        long A = 1;
        long B = 1;
        for(long i = 0; i < r; i++){
            A = A*(n-i);
            B = B*(r-i);
        }
        if(A == 0) return 0;
        return A/B;
    }

    public static long solve(int i, int j, boolean check, int N, String S){
        if(i == N){
            if(j == 0){
                return 1;
            }else{
                return 0;
            }
        }

        if(j == 0) return 1;

        if(check){
            return Combination(N-i, j)*(long)Math.pow(9, j);
        }else{
            if(S.charAt(i) == '0') return solve(i+1, j, false, N, S);
            long Z = solve(i+1, j, true, N, S);
            long A = solve(i+1, j-1, true, N, S)*(S.charAt(i)-'1');
            long P = solve(i+1, j-1, false, N, S);
            return Z+A+P;
        }


    }
    
    public static long Dflag(long a){
        if(a == 1) return 1;
        long h = Dflag(a/2);
        return h + h + 1;
    }
	public static void main(String[] $){
		Scanner s = new Scanner(System.in);
        long H = s.nextLong();
        

        System.out.println(Dflag(H));
	}
} 