import scala.collection.mutable
import scala.io.StdIn.readLine

object Main extends App {

  var h = readLine().toLong
  val m = mutable.Map[Long, Long]()

  def go(hp: Long): Long = hp match {
    case 0 => 0
    case 1 => 1
    case _ => {
      val p = hp / 2
      val q = m.get(p) match {
        case Some(value) => value
        case None =>
          val y = go(p)
          m + (p -> y)
          y
      }
      q * 2 + 1
    }
  }

  println(go(h))
}
