#include "bits/stdc++.h"
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
long long d;
bool f(long long x) {
	if (x == d)return true;
	long long now = x;
	while (x > 0) {
		now += x / 2;
		if (now == d)return true;
		x /= 2;
	}
	now += x;
	if (now == d)return true;
	return false;
}
int main() {
	ios::sync_with_stdio(false);
	std::cin.tie(nullptr);
	cin >> d;
	long long ans = d;
	long long tmp = 1;
	long long bunsi = 1, bunbo = 1;
	for (int i = 0; bunsi <= (1e18+100); i++) {
		double tmp2 = (double)bunbo / (double)bunsi;
		tmp2 *= (double)d;
		long long now = (long long)tmp2;
		for (long long j = now - 500; j <= now + 500; j++) {
			if (f(j)) {
				ans = min(ans, j);
			}
		}
		tmp *= 2;
		bunsi += tmp; bunbo = tmp;
	}
	cout << ans << endl;
	return 0;
}