#define ll int
#define f first
#define s second
struct info{
pair<ll,ll> next[2];
info(){
next[0]=next[1]={-1,0};
}
};
vector<info> dp;
ll sum_n=0;
class Solution {
public:
int solve(int N, vector<int> A) {
vector<ll> ans(N,0);
sum_n+=N;
assert(sum_n<=((ll)(5e5)));
dp.clear();
dp.emplace_back();
for(ll i=0;i<N;i++){
ll best=1,cur=0;
for(ll j=30;j>=0;j--){
ll d=min(1,A[i]&(1<<j));
if(d){
best=max(best,dp[cur].next[0].s+1);
if(dp[cur].next[1].f==-1){
dp[cur].next[1].f=dp.size();
dp.emplace_back();
}
cur=dp[cur].next[1].f;
}
else{
dp[cur].next[0].s=max(dp[cur].next[0].s,best);
if(dp[cur].next[0].f==-1){
dp[cur].next[0].f=dp.size();
dp.emplace_back();
}
cur=dp[cur].next[0].f;
}
}
ans[i]=best;
}
dp.clear();
dp.emplace_back();
ll final=N;
for(ll i=N-1;i>=0;i--){
ll best=1,cur=0;
for(ll j=30;j>=0;j--){
ll d=min(1,A[i]&(1<<j));
if(d){
dp[cur].next[1].s=max(dp[cur].next[1].s,best);
if(dp[cur].next[1].f==-1){
dp[cur].next[1].f=dp.size();
dp.emplace_back();
}
cur=dp[cur].next[1].f;
}
else{
best=max(best,dp[cur].next[1].s+1);
if(dp[cur].next[0].f==-1){
dp[cur].next[0].f=dp.size();
dp.emplace_back();
}
cur=dp[cur].next[0].f;
}
}
ans[i]+=best;
ans[i]=N-ans[i];
final=min(final,ans[i]+1);
}
return final;
}
};