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