#include <bits/stdc++.h>
using namespace std;
int N;
int circle_dist(int s, int e){
int circle_dist = min(abs(s - e), (2 * N) - abs(s - e));
return circle_dist;
}
int main(){
cin.tie(0);
ios::sync_with_stdio(0);
int n;
cin >> n;
vector<pair<int, int>> strings(n);
for(int i = 0; i < n; i++){
cin >> strings[i].first >> strings[i].second;
}
N = n;
vector<int> dists;
int good = true;
vector<int> attached(n * 2);
for(auto p : strings){
int s = p.first;
int e = p.second;
attached[s] = e;
attached[e] = s;
}
// find the pin where it is attached to one away
int start = -1;
for(int i = 0; i < 2 * n; i++){
if(circle_dist(i, attached[i]) == 1){
start = i;
break;
}
}
if(start == -1){
cout << 2 << "\n";
return 0;
}
// check that every string after that is parallel
int checked = 1;
int p = start - 1;
int midpoint = n / 2;
midpoint += n % 2;
while(checked < n){
if(p == -1) p = (2 * n) - 1;
int dist = circle_dist(p, attached[p]);
int ideal = 2 * (checked - 1) + 1;
if(checked > midpoint){
ideal = 2 * (n - checked - 1) + 1;
}
if(dist != ideal){
cout << 2 << "\n";
return 0;
}
checked++;
}
cout << 0 << "\n";
}