```
class Solution {
public:
int integerReplacement(int n) {
int res;
res = replace(n);
return res;
}
long long replace(long long n) {
if(n <= 1) {
return 0;
}
if(n % 2 == 0) {
return replace(n/2)+1;
}
else {
return min(replace(n+1), replace(n-1))+1;
}
}
};
```