#include<bits/stdc++.h>usingnamespace std;constint N =1e6+10;int a[N], b[N], pos[N], ans[N];intmain(){int n;
cin >> n;for(int i =1; i <= n; i ++)
cin >> a[i];for(int i =1; i <= n; i ++)
cin >> b[i];for(int i =1; i <= n; i ++)
pos[b[i]]= i;for(int i =1; i <= n; i ++)if(pos[i]< i)
ans[i]= pos[i]+ n - i +1;else
ans[i]= pos[i]- i;int res =0;for(int i =1; i <= n; i ++)
res =max(res, ans[i]);bool flag =true;while(flag){
flag =false;for(int i =1; i <= n; i ++)if(pos[i]){if(res > ans[i])
ans[i]= pos[i]+ n - i +1;if(res < ans[i])
res = ans[i], flag =true;}}
cout << res <<'\n';}