#include <bits/stdc++.h>
using namespace std;
// Problem: Each person i has range [lo[i], hi[i]] of people worse than them
// Find maximum team size where everyone's constraints are satisfied
// 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 in a team of size sz:
// - lo[i] people worse means i is at position lo[i]+1 or better
// - hi[i] people worse means i is at position sz-hi[i] or worse
// Find valid position range [l, r] for each person in team of size sz
vector<pair<int, int>> ranges;
for (int i = 1; i <= n; i++) {
// Left bound: at least sz-hi[i] position (not too good)
int l = max(1, sz - hi[i]);
// Right bound: at most lo[i]+1 position (not too bad)
int r = min(sz, lo[i] + 1);
// Only include if valid range exists
if (l <= r) {
ranges.push_back({l, r});
}
}
// Need at least sz people with valid ranges
if (ranges.size() < sz) return false;
// Sort by earliest possible position (greedy assignment)
sort(ranges.begin(), ranges.end());
// Min heap: tracks rightmost position each available person can take
// We pick person whose range ends earliest (greedy choice)
priority_queue<int, vector<int>, greater<int>> pq;
int id = 0;
// Try to fill positions 1, 2, ..., sz
for (int p = 1; p <= sz; p++) {
// Add all people who can start at position p or earlier
while (id < ranges.size() && ranges[id].first <= p) {
pq.push(ranges[id].second); // store their right bound
id++;
}
// Remove people whose range has already passed
while (!pq.empty() && pq.top() < p) {
pq.pop();
}
// No one available for this position
if (pq.empty()) return false;
// Assign the person with earliest ending range to position p
pq.pop();
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
// lo[i] = minimum number of people worse than person i
// hi[i] = maximum number of people worse than person i
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;
}