#include <cstdio>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    scanf("%d", &n);

    int s[100002] = {};
    for (int i = 0; i < n; ++i) {
        int a, b;
        scanf("%d %d", &a, &b);
        for (int j = a; j <= b; ++j)
            ++s[j];
    }

    /*
    for (int i = 0; i <= n + 1; ++i)
        printf("%6d: %d\n", i, s[i]);
    */

    int ans = 0;
    for (int i = 0; i <= n + 1; ++i)
        if (i - 1 <= s[i])
            ans = i - 1;
    printf("%d\n", ans);

    return 0;
}