```
public int integerReplacement(int n) {
int count = 0;
while(n!=1){
count++;
if(n%2==0){
n = n/2;
}else{
int temp1 = (n-1)/2;
int temp2 = temp1+1;
int temp = 0;
if(temp1%2==0 || temp1==1){
temp = temp1;
}else{
temp = temp2;
}
n = temp;
count++;
}
}
return count;
}
```