// Problem   In Danger
// Algorithm Recursion
// Runtime   O(log(n))
// Author    Walter Guttmann
// Date      06.05.2001

#include <fstream>
#include <iostream>

using namespace std;

int joseph (int n)
{
  if (n == 1) return 1;
  if (n & 1) return joseph ((n-1)/2) * 2 + 1;
  return joseph (n/2) * 2 - 1;
}

int main ()
{
  while (1)
  {
    double d;
    cin >> d;
    int n = (int) d;
    if (n == 0) break;
    cout << joseph (n) << endl;
  }
  return 0;
}

