#include<bits/stdc++.h>
using namespace std;
#define lli long long int
#define inf 1000000000
#define pb push_back
#define mp make_pair
#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl "\n"
#define yoi cout<<"yo"<<endl;
#define debug(i) cout<<i<<endl;
int have[200001]={0};
const int mod=1e9 +7;
int main(){
int n,i,j;
cin>>n;
int a[n],b[n];
for(i=0;i<n;i++){
cin>>a[i];
}
for(i=0;i<n;i++){
cin>>b[i];
}
int zero=0;
for(i=0;i<n;i++){
if(!a[i])
zero++;
}
int start=0,last=zero,middle,index=0;
int have[n+1];
while(start<=last){
memset(have,0,sizeof(have));
for(i=0;i<n;i++){
if(a[i])
have[a[i]]++;
}
middle=(start+last)/2;
for(j=0;j<middle;j++){
if(b[j])
have[b[j]]++;
}
//cout<<"yo"<<" "<<middle<<endl;
for(i=1;i<=n;i++){
if(!have[i])
break;
else{
if(b[j])
have[b[j]]++;
j=(j+1)%n;
}
}
//cout<<i<<" "<<middle<<endl;
if(i==n+1){
last=middle-1;
index=middle;
}
else start=middle+1;
}
//cout<<index<<endl;
if(index){
cout<<index+n<<endl;
}
else{
for(j=0;j<n;j++){
if(b[j]==1)
break;
}
cout<<j<<endl;
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKI2RlZmluZSBsbGkgbG9uZyBsb25nIGludAojZGVmaW5lIGluZiAxMDAwMDAwMDAwCiNkZWZpbmUgcGIgcHVzaF9iYWNrCiNkZWZpbmUgbXAgbWFrZV9wYWlyCiNkZWZpbmUgSU9TIGlvczo6c3luY193aXRoX3N0ZGlvKDApOyBjaW4udGllKDApOyBjb3V0LnRpZSgwKTsKI2RlZmluZSBlbmRsICJcbiIKI2RlZmluZSB5b2kgY291dDw8InlvIjw8ZW5kbDsKI2RlZmluZSBkZWJ1ZyhpKSBjb3V0PDxpPDxlbmRsOwppbnQgaGF2ZVsyMDAwMDFdPXswfTsKY29uc3QgaW50IG1vZD0xZTkgKzc7CmludCBtYWluKCl7CmludCBuLGksajsKY2luPj5uOwppbnQgYVtuXSxiW25dOwpmb3IoaT0wO2k8bjtpKyspewogICAgY2luPj5hW2ldOwp9CmZvcihpPTA7aTxuO2krKyl7CiAgICBjaW4+PmJbaV07Cn0KaW50IHplcm89MDsKZm9yKGk9MDtpPG47aSsrKXsKICAgIGlmKCFhW2ldKQogICAgICAgIHplcm8rKzsKfQppbnQgc3RhcnQ9MCxsYXN0PXplcm8sbWlkZGxlLGluZGV4PTA7CmludCBoYXZlW24rMV07CndoaWxlKHN0YXJ0PD1sYXN0KXsKICAgIG1lbXNldChoYXZlLDAsc2l6ZW9mKGhhdmUpKTsKICAgIGZvcihpPTA7aTxuO2krKyl7CiAgICAgICAgaWYoYVtpXSkKICAgICAgICAgICAgaGF2ZVthW2ldXSsrOwogICAgfQogICAgbWlkZGxlPShzdGFydCtsYXN0KS8yOwogICAgZm9yKGo9MDtqPG1pZGRsZTtqKyspewogICAgICAgIGlmKGJbal0pCiAgICAgICAgICAgIGhhdmVbYltqXV0rKzsKICAgIH0KICAgIC8vY291dDw8InlvIjw8IiAiPDxtaWRkbGU8PGVuZGw7CiAgICBmb3IoaT0xO2k8PW47aSsrKXsKICAgICAgICBpZighaGF2ZVtpXSkKICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgZWxzZXsKICAgICAgICAgICAgaWYoYltqXSkKICAgICAgICAgICAgICAgIGhhdmVbYltqXV0rKzsKICAgICAgICAgICAgICAgIGo9KGorMSklbjsKICAgICAgICB9CiAgICB9CiAgICAvL2NvdXQ8PGk8PCIgIjw8bWlkZGxlPDxlbmRsOwogICAgaWYoaT09bisxKXsKICAgICAgICBsYXN0PW1pZGRsZS0xOwogICAgICAgIGluZGV4PW1pZGRsZTsKICAgIH0KICAgIGVsc2Ugc3RhcnQ9bWlkZGxlKzE7Cn0KLy9jb3V0PDxpbmRleDw8ZW5kbDsKaWYoaW5kZXgpewogICAgY291dDw8aW5kZXgrbjw8ZW5kbDsKfQplbHNlewogICAgZm9yKGo9MDtqPG47aisrKXsKICAgICAgICBpZihiW2pdPT0xKQogICAgICAgICAgICBicmVhazsKICAgIH0KICAgIGNvdXQ8PGo8PGVuZGw7Cn0KfQo=