// nauynix
// 03/04/2021
#include <bits/stdc++.h>
using namespace std;
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define fi first
#define se second
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<PII> VII;
typedef vector<VI> VVI;
typedef long long int  ll;
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int nxt() {int x;cin >> x;return x;}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n = nxt();
    bool poss = true;
    VI a(n), swp(n);
    rep (i, 0, n) {
        int b = nxt()-1, c = nxt()-1;
        bool swps = false;
        if (b > c) {
            swap(b, c);
            swps = true;
        }
        if (b >= n || c < n) {
            poss = false;
            break;
        }
        a[b] = c;
        swp[b] = swps;
    }
    if (!poss) {
        puts("-1");
        return 0;
    }
    VI b(2), c(2), d(2);
    b[0] = b[1] = INF;
    c[0] = c[1] = 0;
    d[0] = d[1] = 0;
    int ans = 0;
    set<int> s;
    rep (i, 0, n) {
        /* printf("%d %d\n", a[i]+1, swp[i]); */
        int j = 0;
        s.insert(a[i]);
        if (b[0] > b[1]) j = 1;
        // b[j] is smaller
        if (a[i] < b[j]) {
            b[j] = a[i];
            if (swp[i]) c[j]++;
            d[j]++;
        } else if (a[i] < b[1-j]) {
            b[1-j] = a[i];
            if (swp[i]) c[1-j]++;
            d[1-j]++;
        } else {
            poss = false;
            break;
        }
        if (*s.begin() == 2 * n - i - 1) {
            ans += min(c[0] + d[1] - c[1], c[1] + d[0] - c[0]);
            b[0] = b[1] = INF;
            c[0] = c[1] = 0;
            d[0] = d[1] = 0;
        }
    }
    if (!poss) printf("-1\n");
    else printf("%d\n", ans);
    return 0;
}

