```
public class Solution {
public int integerReplacement(int n) {
int count = 0;
long N = n;
while (N > 1) {
if (N % 2 != 0) {
if ((N + 1) / 2 % 2 == 0 && (N + 1) / 2 < (N - 1)) N += 1;
else N -= 1;
} else {
N /= 2;
}
count++;
}
return count;
}
}
```