#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define lowbit(x) ((x)&(-(x)))
#define mid ((l+r)>>1)
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid+1, r
#define fors(i, a, b) for(int i = (a); i < (b); ++i)
#define all(vec) vec.begin(),vec.end()
using namespace std;
#define P pair<int,int>
const int maxn = 4e5 + 5;
P a[maxn];
int n;
int mi[maxn],mx[maxn];
int sol(int l, int r){
    int x1=2*n+1,x2=2*n+2;
    int c1[2],c2[2];c1[0]=c1[1]=c2[0]=c2[1]=0;
    fors(i,l,r+1){
        int x=a[i].first;
        if(x<x1) x1=x,c1[a[i].second]++;
        else x2=x,c2[a[i].second]++;
    }
    return min(c1[0]+c2[1],c1[1]+c2[0]);
}
int main()
{
    cin>>n; int f = 1;
    fors(i,1,n+1){
        int x, y; scanf("%d%d", &x, &y);
        if(x<=n && y<=n) f = 0;
        if(x <= n) a[x]=P(y,0);
        else a[y]=P(x,1);
    }
    if(!f) {puts("-1");return 0;}
    int b[4],num=0;memset(b,0,sizeof b);
    fors(i,1,n+1){
        int x=a[i].first;
        if(num==0||b[num-1]<=x) b[num++]=x;
        else fors(i,0,num) if(b[i] > x) {b[i]=x; break;}
        if(num>2) {f=0;break;}
    }
    if(!f) {puts("-1");return 0;} mi[1]=a[1].first;mx[n]=a[n].first;
    fors(i,2,n+1) mi[i]=min(mi[i-1],a[i].first);
    for(int i=n-1;i>0;--i) mx[i]=max(mx[i+1],a[i].first);
    vector<int> t; t.pb(0);
    fors(i,1,n) if(mi[i]>mx[i+1]) t.pb(i); t.pb(n);
    int ans=0; fors(i,0,t.size()-1) ans+=sol(t[i]+1,t[i+1]);
    cout<<ans<<endl;
	return 0;
}
