human
I found an interesting problem on https://codeforces.com/problemset/problem/479/C:
Student Valera is an undergraduate student at the University. His end of term exams are approaching and he is to pass exactly n exams. Valera is a smart guy, so he will be able to pass any exam he takes on his first try. Besides, he can take several exams on one day, and in any order.

According to the schedule, a student can take the exam for the i-th subject on the day number a_{i}. However, Valera has made an arrangement with each teacher and the teacher of the i-th subject allowed him to take an exam before the schedule time on day b_{i} (b_{i} < a_{i}). Thus, Valera can take an exam for the i-th subject either on day a_{i}, or on day b_{i}. All the teachers put the record of the exam in the student's record book on the day of the actual exam and write down the date of the mark as number a_{i}.

Valera believes that it would be rather strange if the entries in the record book did not go in the order of non-decreasing date. Therefore Valera asks you to help him. Find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date.

-----Input-----

The first line contains a single positive integer n (1 ≤ n ≤ 5000) — the number of exams Valera will take.

Each of the next n lines contains two positive space-separated integers a_{i} and b_{i} (1 ≤ b_{i} < a_{i} ≤ 10^9) — the date of the exam in the schedule and the early date of passing the i-th exam, correspondingly.

-----Output-----

Print a single integer — the minimum possible number of the day when Valera can take the last exam if he takes all the exams so that all the records in his record book go in the order of non-decreasing date.

-----Examples-----
Input
3
5 2
3 1
4 2

Output
2

Input
3
6 1
5 2
4 3

Output
6


-----Note-----

In the first sample Valera first takes an exam in the second subject on the first day (the teacher writes down the schedule date that is 3). On the next day he takes an exam in the third subject (the teacher...

I tried it in Python, but could not do it. Can you solve it?
assistant
#Here is Python program to find the minimum possible value of the day when Valera can take the final exam if he takes exams so that all the records in his record book go in the order of non-decreasing date.

import math
import random
import itertools
import collections
import sys
import time
import fractions
import os
import functools
import bisect

def timer(f):
    def tmp(*args, **kwargs):
        t = time.time()
        res = f(*args, **kwargs)
        print("Время выполнения функции: %f" % (time.time()-t))
        return res
    return tmp

def contains(l, elem):
    index = bisect.bisect_left(l, elem)
    if index < len(l):
        return l[index] == elem
    return False

n = int(input())

array = []
for i in range(n):
    array.append(list(map(int, input().split(' '))))

array = sorted(array, key = lambda x:x[0]*100+x[1])
currentDate = 0

#print(array)

for i in range(len(array)):
    if array[i][0]>=currentDate and array[i][1]>=currentDate:
        currentDate = min(array[i][0], array[i][1])
    else:
        if array[i][0]>currentDate:
            currentDate = array[i][0]
        else:
            if array[i][1]>currentDate:
                currentDate = array[i][1]
    #print(currentDate)

print(currentDate)

"""
5
6 4
3 2
3 1
9 7
5 3
"""