#include <bits/stdc++.h>

#define ST first
#define ND second
#define MP make_pair
#define PB push_back

using namespace std;


typedef long long ll;
typedef pair<int, int> pi;

const int MAXN = 200010;

int n, a[MAXN], b[MAXN], su[MAXN], res;
set<pi> s;


int main () {
    ios_base::sync_with_stdio(false);

    cin >> n;
    for (int i=1; i<=n; i++) {
        cin >> a[i] >> b[i];
        s.insert(MP(a[i], i));
        s.insert(MP(b[i], i));
      //  pos[a[i]] = i;
      //  pos[b[i]] = i;
        su[i] = a[i] + b[i];
    }
    while (s.size()) {
        int ile_dol = s.begin()->first, ile_gor;
        int swaps = 0, total = 0;
        int pop_fr = 2*n+1, pop_ba = 0;
      //  cout << s.begin()->first << endl;
        while (true) {
            int obr = 0;
            while (s.size()) {
                auto it=*s.begin();
                int aa = it.ST;
                if (aa > ile_dol) break;
                int po = it.ND;
                int bb = su[po] - aa;
                if (b[po] == aa) swaps++;
                if (bb > pop_fr) {
                    cout << -1 << endl;
                    return 0;
                }
                pop_fr = bb;
                ile_gor = bb;
                s.erase(MP(aa, po));
                s.erase(MP(bb, po));
          //      printf("(1) usuwam %d %d z %d\n", aa, bb, po);
                total++;
                obr++;
            }
            if (obr == 0) break;
            obr = 0;
            while (s.size()) {
                auto it=*s.rbegin();
                int aa = it.ST;
                if (aa < ile_gor) break;
                int po = it.ND;
                int bb = su[po] - aa;
                if (b[po] == aa) swaps++;
                if (bb < pop_ba) {
                    cout << -1 << endl;
                    return 0;
                }
                pop_ba = bb;
                ile_dol = bb;
                s.erase(MP(aa, po));
                s.erase(MP(bb, po));
               // printf("(2) usuwam %d %d z %d\n", aa, bb, po);
                total++;
                obr++;
            }
            if (obr == 0) break;
        }
        if (s.size() == 0 && pop_fr < pop_ba) {
            cout << -1 << endl;
            return 0;
        }
        
        res += min(swaps, total - swaps);
    }
    
    cout << res << endl;
}
