#include #define rep(i,n) for(int i=(0);i<(n);i++) using namespace std; typedef long long ll; typedef unsigned long long ull; template bool chmax(T &a, const T &b) { if (a bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; } int main(){ cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; int INF = 1e9; vector d(n+1, INF); d[1] = 1; queue q; q.push(1); while(!q.empty()){ int x = q.front(); q.pop(); if(x == n){ cout << d[x] << endl; exit(0); } int y = __builtin_popcount(x); if(x-y >= 1 && d[x-y] > d[x] + 1){ d[x-y] = d[x] + 1; q.push(x-y); } if(x+y <= n && d[x+y] > d[x] + 1){ d[x+y] = d[x] + 1; q.push(x+y); } } cout << -1 << endl; }