#include <bits/stdc++.h>
using namespace std;

int n;
vector<pair<int,int> > tasks;

inline bool can(int days){

    int cur_day = days;

    for(auto task : tasks){
        cur_day += task.second;

        if(cur_day > task.first){
            return false;
        }
    }

    return true;
}

int main(){
    freopen("passat.in", "r", stdin);
    freopen("passat.out", "w", stdout);
    cin >> n;
    for(int i = 1; i <= n; i++){
        int task_time;
        int deadline;
        cin >> task_time >> deadline;

        tasks.push_back({deadline, task_time});
    }

    sort(tasks.begin(), tasks.end());

    int l = 0, r = 1e9 + 1;

    while(l+1<r){
        int mid = (l+r)/2;

        if(can(mid))
            l = mid;
        else
            r = mid;
    }

    cout << l;

    return 0;
}
