import std.stdio, std.string, std.conv, std.algorithm; import std.range, std.array, std.container, std.math, std.typecons; immutable int mod = 10^^9 + 7; immutable int inf = 20000; int N; void main() { N = readln.chomp.to!int; auto d = new int[](N); auto q = Queue!(int)(N + 10); fill(d, inf); d[0] = 1; q.push(0); int pos; while (!q.empty) { pos = q.front; q.pop; if (pos == N - 1) break; int dx = popcnt(pos + 1); if (pos + dx < N && d[pos + dx] == inf) { d[pos + dx] = d[pos] + 1; q.push(pos + dx); } if (pos - dx >= 0 && d[pos - dx] == inf) { d[pos - dx] = d[pos] + 1; q.push(pos - dx); } } debug { stderr.writeln(d); } writeln(d[N - 1] < inf ? d[N - 1] : -1); } int popcnt(int x) { return x > 0 ? popcnt(x>>1) + (x & 1) : 0; } unittest { assert(popcnt(0) == 0); assert(popcnt(3) == 2); assert(popcnt(15) == 4); assert(popcnt(24) == 2); assert(popcnt(7) == 3); assert(popcnt(8) == 1); } struct Queue(T) { private: int N, head, tail; T[] data; public: this(int size) { N = size + 1; data = new T[](N); } bool empty() @property { return head == tail; } bool full() @property { return (tail + 1) % N == head; } void push(T x) @property { assert(!full); data[tail++] = x; tail %= N; } void pop() @property { assert(!empty); (head += 1) %= N; } T front() @property { assert(!empty); return data[head]; } void clear() @property { head = tail = 0; } int length() @property { return (tail - head + N) % N; } }