#include #include #include #include #include #include #include #include #include #include #include #include namespace utility { templatestruct equality_comparable; templatestruct addable; templatestruct subtractable; templatestruct multipliable; templatestruct dividable; templatedecltype(auto) operator!=(equality_comparableconst& lhs, equality_comparableconst& rhs) { return !lhs.operator==(rhs); } templatedecltype(auto) operator+(addableconst& lhs, addableconst& rhs) { auto x = lhs; x += rhs; return x; } templatedecltype(auto) operator-(subtractableconst& lhs, subtractableconst& rhs) { auto x = lhs; x -= rhs; return x; } templatedecltype(auto) operator*(multipliableconst& lhs, multipliableconst& rhs) { auto x = lhs; x *= rhs; return x; } templatedecltype(auto) operator/(dividableconst& lhs, dividableconst& rhs) { auto x = lhs; x /= rhs; return x; } templatestruct equality_comparable { decltype(auto) operator==(equality_comparableconst& rhs)const { return static_cast(*this) == static_cast(rhs); } friend decltype(auto) operator!=(equality_comparableconst& lhs, equality_comparableconst& rhs); }; templatestruct addable { decltype(auto) operator+=(addableconst& rhs) { return static_cast(*this) += static_cast(rhs); } friend decltype(auto) operator+(addableconst& lhs, addableconst& rhs); }; templatestruct subtractable { decltype(auto) operator-=(subtractableconst& rhs) { return static_cast(*this) -= static_cast(rhs); } friend decltype(auto) operator-(subtractableconst& lhs, subtractableconst& rhs); }; templatestruct multipliable { decltype(auto) operator*=(multipliableconst& rhs) { return static_cast(*this) *= static_cast(rhs); } friend decltype(auto) operator*(multipliableconst& lhs, multipliableconst& rhs); }; templatestruct dividable { decltype(auto) operator/=(dividableconst& rhs) { return static_cast(*this) /= static_cast(rhs); } friend decltype(auto) operator/(dividableconst& lhs, dividableconst& rhs); }; } namespace math { templateconstexpr T pow(T val, std::uint64_t p) { return p == 0 ? T(1) : p == 1 ? val : p == 2 ? val*val : p % 2 == 0 ? pow(pow(val, p / 2), 2) : pow(pow(val, p / 2), 2)*val; } templateconstexpr int ilog(T val, U base) { T v(1); for (int i{};;++i) { if (val <= v) { return i; } v *= base; } } templateclass mod_number: private utility::equality_comparable>, private utility::addable>, private utility::subtractable>, private utility::multipliable>, private utility::dividable> { std::uint64_t value; public: mod_number(std::uint64_t v = 0) :value(v%Mod) { } mod_number(int v) :value(v%Mod) { } mod_number(std::int64_t v) :value(v%Mod) { } mod_number(std::uint32_t v) :value(v%Mod) { } bool operator==(mod_number const& rhs) { return value == rhs.value; } auto& operator+=(mod_number const& rhs) { value += rhs.value; value %= Mod; return *this; } auto& operator-=(mod_number const& rhs) { value += Mod - rhs.value; value %= Mod; return *this; } auto& operator*=(mod_number const& rhs) { value *= rhs.value; value %= Mod; return *this; } auto& operator/=(mod_number const& rhs) { return operator*=(pow(rhs, Mod - 2)); } }; constexpr int bitcount(unsigned int x) { x = (x & 0x55555555) + ((x & 0xAAAAAAAA) >> 1); x = (x & 0x33333333) + ((x & 0xCCCCCCCC) >> 2); x = (x & 0x0F0F0F0F) + ((x & 0xF0F0F0F0) >> 4); x = (x & 0x00FF00FF) + ((x & 0xFF00FF00) >> 8); x = (x & 0x0000FFFF) + ((x & 0xFFFF0000) >> 16); return static_cast(x); } constexpr int bitcount(unsigned long long x) { x = (x & 0b0101010101010101010101010101010101010101010101010101010101010101) + ((x & 0b1010101010101010101010101010101010101010101010101010101010101010) >> 1); x = (x & 0b0011001100110011001100110011001100110011001100110011001100110011) + ((x & 0b1100110011001100110011001100110011001100110011001100110011001100) >> 2); x = (x & 0b0000111100001111000011110000111100001111000011110000111100001111) + ((x & 0b1111000011110000111100001111000011110000111100001111000011110000) >> 4); x = (x & 0b0000000011111111000000001111111100000000111111110000000011111111) + ((x & 0b1111111100000000111111110000000011111111000000001111111100000000) >> 8); x = (x & 0b0000000000000000111111111111111100000000000000001111111111111111) + ((x & 0b1111111111111111000000000000000011111111111111110000000000000000) >> 16); x = (x & 0b0000000000000000000000000000000011111111111111111111111111111111) + ((x & 0b1111111111111111111111111111111100000000000000000000000000000000) >> 32); return static_cast(x); } constexpr int bitcount(int x) { return bitcount(static_cast(x)); } constexpr int bitcount(long long x) { return bitcount(static_cast(x)); } } namespace container { namespace detail { templatestruct tree_array { std::array ar; constexpr T& operator()(std::size_t index, std::size_t depth) { return ar[(1ull << depth) + index - 1]; } constexpr T const& operator()(std::size_t index, std::size_t depth)const { return ar[(1ull << depth) + index - 1]; } }; } templateclass addtree { std::array data; T get(std::size_t left, std::size_t right, std::size_t i, std::size_t l, std::size_t r) { if (r <= left || right <= l) return T(); if (left <= l&&r <= right) { return data[i]; } else { return get(left, right, 2 * i + 1, l, (l + r) / 2) + get(left, right, 2 * i + 2, (l + r) / 2, r); } } public: addtree() :data{} { } void change(T const& val, std::size_t index) { T from = data[index + Size - 1]; for (std::size_t i = 1, c = math::ilog(Size, 2);i <= Size;(i <<= 1), --c) { data[i + (index >> c) - 1] -= from; data[i + (index >> c) - 1] += val; } } auto begin()const { return std::next(data.begin(), Size - 1); } auto end()const { return data.end(); } T get(std::size_t left, std::size_t right) { return left > right ? T() : get(left, right + 1, 0, 0, Size); } }; templateclass minmaxtree { detail::tree_array, Depth> data; T min(std::size_t left, std::size_t right, std::size_t L, std::size_t R, std::size_t depth)const { if (right <= L || R <= left) { return std::numeric_limits::max(); } else if (left <= L&&R <= right) { return data(L >> (Depth - depth), depth).first; } else { return std::min( min(left, right, L, (L + R) / 2, depth + 1), min(left, right, (L + R) / 2, R, depth + 1)); } } T max(std::size_t left, std::size_t right, std::size_t L, std::size_t R, std::size_t depth)const { if (right <= L || R <= left) { return std::numeric_limits::min(); } else if (left <= L&&R <= right) { return data(L >> (Depth - depth), depth).second; } else { return std::max( max(left, right, L, (L + R) / 2, depth + 1), max(left, right, (L + R) / 2, R, depth + 1)); } } public: void change(T const& val, std::size_t index) { data(index, Depth).first = val; data(index, Depth).second = val; for (int i = 1;i <= Depth;++i) { data(index >> i, Depth - i).first = std::min( data((index >> i) << 1, Depth - i + 1).first, data(((index >> i) << 1) + 1, Depth - i + 1).first); data(index >> i, Depth - i).second = std::max( data((index >> i) << 1, Depth - i + 1).second, data(((index >> i) << 1) + 1, Depth - i + 1).second); } } T min(std::size_t left, std::size_t right)const { return right < left ? std::numeric_limits::max() : min(left, right + 1, 0, 1 << Depth, 0); } T max(std::size_t left, std::size_t right)const { return right < left ? std::numeric_limits::min() : max(left, right + 1, 0, 1 << Depth, 0); } minmaxtree() :data{} { } }; } void Main(); int main() { std::ios::sync_with_stdio(false); Main(); } //ここから頑張る int count(int x, std::vector& passed, int N) { if (x<1 || x>N || passed[x - 1]) { return 999999; } passed[x - 1] = 1; if (x == N) { return 1; } return std::min(count(x + math::bitcount(x), passed, N), count(x - math::bitcount(x), passed, N)) + 1; } void Main() { int N; std::cin >> N; std::vector passed(N); auto c = count(1, passed, N); std::cout << (c >= 999999 ? -1 : c )<< std::endl; }