#include<iostream>
#include<map>
#include<queue>
#include<algorithm>
using namespace std;

#define fs first
#define sc second
typedef pair<int,int> P;

int n;
P p[200100];

int main(){
  cin >> n;
  for(int i=0;i<n;i++)cin >> p[i].fs >> p[i].sc;
  p[n] = P(1,100001);
  sort(p,p+n+1);

  priority_queue<int, vector<int>, greater<int> > q;
  unsigned int ans = 1;

  for(int i=0;i<=n;i++){
    while(!q.empty() && q.top() < p[i].fs)q.pop();
    q.push(p[i].sc);    
    if(p[i].fs <= q.size() && q.size() <= q.top())ans = max(ans,q.size());
  }

  cout << ans-1 << endl;
}