#include "bits/stdc++.h" using namespace std; #define FOR(i, j, k) for(int i = j; i < k; ++i) #define rep(i, j) FOR(i, 0, j) #define FORr(i, j, k) for(int i = j; i >= k; --i) #define repr(i, j) FOR(i, j, 0) #define INF (1 << 30) typedef long long ll; typedef unsigned long long ull; typedef pair P; typedef pair Pi; const int MOD = 1e9 + 7; const int dy[] = { 0, 0, 1, -1 }; const int dx[] = { 1, -1, 0, 0 }; template void chmin(T& a, const T& b) { a = min(a, b); } template void chmax(T& a, const T& b) { a = max(a, b); } bool went[10001]; int num[10001]; int bit_count(int n) { int res = 0; while (n) { if(n % 2) ++res; n /= 2; } return res; } void bfs(int s, int N) { num[s] = 1; queue que; que.push(s); while (!que.empty()) { int q = que.front(); que.pop(); if (went[q]) continue; went[q] = true; int cnt = bit_count(q); if (1 <= q + cnt && q + cnt <= N && !went[q + cnt]) { num[q + cnt] = min(num[q] + 1, num[q + cnt]); que.push(q + cnt); } if (1 <= q - cnt && q - cnt <= N && !went[q - cnt]) { num[q - cnt] = min(num[q] + 1, num[q - cnt]); que.push(q - cnt); } } } int main() { int N; scanf("%d", &N); fill(num, num + (N + 1), INF); bfs(1, N); (went[N]) ? printf("%d\n", num[N]) : printf("-1\n"); return 0; }