#include using namespace std; #define times(n, i) uptil(0, n, i) #define rtimes(n, i) downto((n) - 1, 0, i) #define upto(f, t, i) for(int i##_to_ = (t), i = (f); i <= i##_to_; i++) #define uptil(f, t, i) for(int i##_to_ = (t), i = (f); i < i##_to_; i++) #define downto(f, t, i) for(int i##_to_ = (t), i = (f); i >= i##_to_; i--) #define downtil(f, t, i) for(int i##_to_ = (t), i = (f); i > i##_to_; i--) typedef long double LD; #define long long long #if defined(EBUG) && !defined(ONLINE_JUDGE) #define debug true #define _GLIBCXX_DEBUG #define _LIBCPP_DEBUG 2 #define _LIBCPP_DEBUG2 2 #define ln << endl #else #define debug false #define ln << '\n' #endif #define tb << '\t' #define sp << ' ' inline int bc(long x) { int a = 0; while(x) { a += x & 1; x >>= 1; } return a; } signed main() { // long: 64bit if(!debug) { cin.tie(0); ios::sync_with_stdio(0); } int N; scanf("%d", &N); vector pyon(N+1, -1); pyon[1] = 1; queue q; q.push(1); while(!q.empty()) { int i = q.front(); q.pop(); int b = bc(i); if(debug) cout << i sp << b ln; for(int j : {i+b, i-b}) { if(1 <= j && j <= N && pyon[j] == -1) { pyon[j] = pyon[i] + 1; q.push(j); if(debug) cout << j ln; } } } cout << pyon[N] ln; return 0; }