<|instruction|>
Solve the below problem. The solution should start with an abridged problem statement. Then key observations. Then full solution based on the observations. Then C++ and Python implementations with comments.

178. Golden chain
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
input: standard
output: standard



Peter arrived to the city and stayed in the hotel. He has no money but few day's later (maximum N days) he will receive a lot of money. And so he has a golden chain with N links. Peter must pay one link everyday, but he can pay more and take change by some of his links paid before.
When he receives money he will get his chain out of pawn. Peter likes his chain and he wants to break minimal number of links. Help him!
For example, if N=5, he must break one link (the second). After it his chain will consist of 3 parts with lengths 1, 1 and 3.
At the first day he will pay one link, at the second - one link too. Now he have one part consists of three connected links. At the third day he will pay 3-linked part and take two separated links (as change) back. On the fourth and fifth day he will pay one link.
You may assume that Peter can choose the way to get change.

Input
Input contains one integer number N (1<=N<=10^16).

Output
In output file you must write one integer number (number of the broken links).

Sample test(s)

Input
9

Output
2
Author:	Vasiliy Astakhov
Resource:	Saratov ST team Spring Contest #1
Date:	18.05.2003

<|response|>
1. Abridged problem statement  
Given a chain of N links, you need to pay exactly one link per day for N days by pawning whole connected segments (and receiving previously pawned single links as change). Initially you may break some links to create loose rings (connectors). What is the minimum number of links to break so that you can pay one link each day for N consecutive days?

2. Key observations  
- If you break k links up front, you obtain k loose rings and the remaining chain splits into k+1 connected segments.  
- On each day you hand over one or more entire segments or loose rings to the pawnbroker and receive back some loose rings from previous days so that your net payment is exactly one link.  
- With an optimal choice of where to break, the maximum number of days you can cover using k breaks is  
  M(k) = (k+1) × 2^(k+1) − 1.  
- We need the smallest k such that M(k) ≥ N.

3. Full solution approach  
a. Derive M(k):  
   - Base case k=0 (no breaks): you have one segment of length N, no loose rings; you can only pay on day 1. M(0)=1=(0+1)·2^(0+1)−1.  
   - When you go from k−1 breaks to k breaks, you double your reachable days and add one more full block, yielding the recurrence M(k) = 2·M(k−1) + 1, which solves to M(k) = (k+1)·2^(k+1) − 1.  
b. To find the minimum k with M(k) ≥ N, observe that M(k) grows very quickly (exponentially in k). We can simply loop k = 0,1,2,… and compute M(k) until it reaches or exceeds N.  
c. Instead of recomputing powers every time, maintain two variables:  
   - ans = current k, initialized to 0  
   - x = 2^(ans+1), initialized to 2  
   Then M(ans) = x·(ans+1) − 1.  
   In each iteration, if x·(ans+1) − 1 < N, increment ans and double x.  
d. When the loop finishes, ans is the minimum number of breaks required.  

4. C++ implementation with detailed comments  
```cpp
#include <bits/stdc++.h>
using namespace std;

/*
 We want the smallest k such that M(k) = (k+1)*2^(k+1) - 1 >= N.
 We'll keep ans = k and x = 2^(k+1).  Start with k=0 => x=2^1=2.
*/

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    long long N;
    cin >> N;

    long long ans = 0;
    long long x = 2;  // x = 2^(ans+1)

    // While M(ans) = x*(ans+1) - 1 is still less than N, we need more breaks
    while (x * (ans + 1) - 1 < N) {
        ans++;      // increase the number of broken links
        x <<= 1;    // double x to become 2^(ans+1)
    }

    cout << ans << "\n";
    return 0;
}
```

5. Python implementation with detailed comments  
```python
import sys

def main():
    data = sys.stdin.read().strip()
    if not data:
        return
    n = int(data)

    # ans = number of links to break
    # x = 2^(ans+1); start with ans=0 => x=2
    ans = 0
    x = 2

    # M(ans) = x * (ans+1) - 1
    # Loop until M(ans) >= n
    while x * (ans + 1) - 1 < n:
        ans += 1
        x <<= 1  # same as x *= 2

    print(ans)

if __name__ == "__main__":
    main()
```