#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 50;
int a[N], b[N];
int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) scanf("%d", a + i);
    for (int i = 0; i < n; i++) scanf("%d", b + i);
    multiset<int> A{a, a + n}, B{b, b + n};
    deque<int> q{b, b + n};
    int zcnt = count(b, b + n, 0);
    int opcnt = 0;
    while (zcnt)
    {
        for (auto i : q)
            printf("%2d ", i);
        puts("");
        if (A.count(q.back() + 1))
        {
            A.erase(q.back() + 1);
            q.push_back(q.back() + 1);
            B.insert(q.back() + 1);
            if (q.front() == 0) zcnt--;
            A.insert(q.front());
            B.erase(q.front());
            q.pop_front();
            opcnt++;
        }
        else
        {
            A.erase(A.begin());
            q.push_back(0);
            B.insert(0);
            zcnt++;
            if (q.front() == 0) zcnt--;
            A.insert(q.front());
            B.erase(q.front());
            q.pop_front();
            opcnt++;
        }
    }
    printf("%d", opcnt);
    return 0;
}
