program BegD
  implicit none
  integer(8) :: H, cnt
  integer(8) ::i!,amr,Hnew                                                                    

  read(*,*) H
  i=0
  cnt=0

  do
     if(mod(H,1024).eq.0)then
        i=i+10
        H=H/1024
        cnt =cnt+2046
     else
        i=i+1
        H=H/2
        cnt=cnt+2**(i-1)
     endif
     if(H.eq.1) goto 500
  enddo
500 continue

  print*, cnt+2**i


end program BegD