/* * main.cpp * * Created on: 2017/10/06 * Author: sep */ #include #include #include #include #include #include #include #include #include #include using namespace std; inline int main003(); inline int main2(); inline int main3(); inline int main4(); int main() { main003(); return 0; } inline int get_bit(int x) { int c = 0; while (x) { if (x & 1) c++; x >>= 1; } return c; } inline int main003() { int num; int route, v1, v2, bit_count; int result = 0; map v; queue q; cin >> num; q.push(1); v[1] = 1; while (!q.empty()) { route = q.front(); q.pop(); bit_count = get_bit(route); v1 = route + bit_count; v2 = route - bit_count; if (v1 == num) { result = v[route] + 1; break; } if ((v1 < num) && (v[v1] == 0 || v[route] + 1 < v[v1])) { v[v1] = v[route] + 1; q.push(v1); } if ((0 < v2) && (v[v2] == 0 || v[route] + 1 < v[v2])) { v[v2] = v[route] + 1; q.push(v2); } } if (result == 0) { cout << -1; } else { cout << result; } return 0; }