#include using namespace std; int n, m = -1; queue> Q; bool f[10001]; int main() { cin >> n; Q.push({ 1, 1 }); f[1] = 1; while (!Q.empty()) { int t = Q.front().first, u = Q.front().second; Q.pop(); int w = bitset<32>(t).count(); if (t + w == n) { m = u + 1; break; } if (t > w && !f[t - w]) { f[t - w] = 1; Q.push({ t - w, u + 1 }); } if (t < n - w && !f[t + w]) { f[t + w] = 1; Q.push({ t + w, u + 1 }); } } cout << m << endl; }