#include <bits/stdc++.h>
using namespace std;
// Check if we can make a team of size sz
bool check(int sz, vector<int>& lo, vector<int>& hi) {
int n = lo.size() - 1;
// Find valid position range for each person
vector<pair<int, int>> ranges;
for (int i = 1; i <= n; i++) {
// Calculate which positions person i can take
int l = max(1, sz - hi[i]);
int r = min(sz, lo[i] + 1);
if (l <= r) {
ranges.push_back({l, r});
}
}
// Need at least sz people with valid ranges
if (ranges.size() < sz) return false;
// Sort by start position
sort(ranges.begin(), ranges.end());
// Min heap to pick person whose range ends earliest
priority_queue<int, vector<int>, greater<int>> pq;
int id = 0;
// Fill each position greedily
for (int p = 1; p <= sz; p++) {
// Add all people who can start at position p
while (id < ranges.size() && ranges[id].first <= p) {
pq.push(ranges[id].second);
id++;
}
// Remove people who can't be at position p anymore
while (!pq.empty() && pq.top() < p) {
pq.pop();
}
// No one available for this position
if (pq.empty()) return false;
// Assign someone to position p
pq.pop();
}
return true;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
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 answer
int l = 0, r = n;
while (l < r) {
int m = (l + r + 1) / 2;
if (check(m, lo, hi)) {
l = m; // Try bigger
} else {
r = m - 1; // Try smaller
}
}
cout << l << "\n";
return 0;
}