#include <bits/stdc++.h>
using namespace std;
// Problem: Each person i has constraints on team composition
// lo[i] = max number of people with LOWER skill than i in the team
// hi[i] = max number of people with HIGHER skill than i in the team
// Find maximum team size where everyone's constraints are satisfied
// IMPORTANT: Selected people occupy positions by skill order
// Check if we can make a valid team of size sz
bool check(int sz, vector<int>& lo, vector<int>& hi) {
int n = lo.size() - 1;
// For person i at position pos in a team of size sz:
// - (pos-1) people have lower skill (better positions)
// - (sz-pos) people have higher skill (worse positions)
// Valid if: pos-1 <= lo[i] AND sz-pos <= hi[i]
// Rearranging: max(1, sz-hi[i]) <= pos <= min(sz, lo[i]+1)
// Precompute valid position ranges for each person
vector<pair<int, int>> ranges(n + 1);
for (int i = 1; i <= n; i++) {
int L = max(1, sz - hi[i]);
int R = min(sz, lo[i] + 1);
ranges[i] = {L, R};
}
// Greedy: fill positions 1, 2, ..., sz
// For each position, pick the smallest skill index that can go there
int nextPos = 1;
for (int skill = 1; skill <= n && nextPos <= sz; skill++) {
int L = ranges[skill].first;
int R = ranges[skill].second;
// Can person with this skill be placed at nextPos?
if (L <= nextPos && nextPos <= R) {
nextPos++; // Assign this person to nextPos, move to next position
}
// Otherwise skip this person (can't use them)
}
// Successfully filled all sz positions?
return nextPos > sz;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
// lo[i] = max number of people with lower skill (in selected team)
// hi[i] = max number of people with higher skill (in selected team)
vector<int> lo(n + 1), hi(n + 1);
for (int i = 1; i <= n; i++) cin >> lo[i];
for (int i = 1; i <= n; i++) cin >> hi[i];
// Binary search on maximum team size
int l = 0, r = n;
while (l < r) {
int m = (l + r + 1) / 2;
if (check(m, lo, hi)) {
l = m; // Can make team of size m, try bigger
} else {
r = m - 1; // Cannot make team of size m, try smaller
}
}
cout << l << "\n";
return 0;
}