/*
TASK: man
LANG: C++
*/
#include <iostream>
#include <cstdlib>
using namespace std;
int Replace(int *arr, int what, int whit, int max_id)
{
    int replaces = 0;
     for (int i = 0; i < max_id + 1; ++i)
     if (arr[i] == what) { arr[i] = whit; replaces = 1; }
     return replaces;
}
void print(int *arr, int max_id)
{
     for (int i = 0; i < max_id + 1; ++i) cout << arr[i] << " ";
     cout << endl;
}
int main()
{
    int N[15000];
    int max_id = -1;
    for (int i = 0; i < 15000; ++i) N[i] = i;
    int connections;
    int cons = 0;
    int temp1, temp2;
    cin >> connections;
    for (int i = 0; i < connections; ++i)
    {
        cin >> temp1 >> temp2;
        int cur_id = (temp1 > temp2) ? temp1 : temp2;
        max_id = (cur_id > max_id) ? cur_id : max_id;
        if (temp1 > temp2) {
        if (Replace(N, temp1, temp2, max_id)) { ++cons; }
        } else if (Replace(N, temp2, temp1, max_id)) { ++cons; }
        
        //print(N, max_id);
}
cout << cons;
        
              
                     

    cout << endl;
    return 0;
}
