D = int(input()) ok = pow(10,18)*2 ng = 0 def check(x): ret = x while x > 0: x = x//2 ret += x return ret cnt = 0 while abs(ok-ng) > 1: #and cnt < 100: mid = (ok+ng)//2 #print(ok,mid,ng) if check(mid) >= D: ok = mid else: ng = mid cnt += 1 ans = ok #print(ans) def verify(x,y): ret = x while x > 0: if ret == y: return True else: x = x//2 ret += x if ret == y: return True else: return False a = ans + 0 b = ans - 0 while True: if verify(a,D): print(a) break elif verify(b,D): print(b) break else: a += 1 b -= 1