// CodeForces round 712 (div 1) problem D: Flip the Cards
//
// There is a deck of n cards. The i-th card has a number a[i] on the front and
// a number b[i] on the back. Every integer between 1 and 2n appears exactly
// once on the cards.
//
// A deck is called sorted if the front values are in increasing order and the
// back values are in decreasing order. That is, if a[i]<a[i+1] and b[i]>b[i+1]
// for all 1≤i<n.
//
// To flip a card i means swapping the values of a[i] and b[i]. You must flip
// some subset of cards (possibly, none), then put all the cards in any order
// you like. What is the minimum number of cards you must flip in order to sort
// the deck?
//
// INPUT
// The first line contains a single integer n (1≤n≤2⋅105) — the number of cards.
//
// The next n lines describe the cards. The i-th of these lines contains two
// integers a[i],b[i] (1≤a[i],b[i]≤2n). Every integer between 1 and 2n appears
// exactly once.
//
// OUTPUT
// If it is impossible to sort the deck, output "-1". Otherwise, output the
// minimum number of flips required to sort the deck.

#include <bits/stdc++.h>

using namespace std;

#define FOR(i,a,b) for (int i = int(a), _end_##i = int(b); i < _end_##i; ++i)
#define REP(i,n) FOR(i,0,n)


// Editorial: https://codeforces.com/blog/entry/89319
//
// (My solution mostly stolen from the editorial because I couldn't figure out
// how to do it myself! But really only the first part is critical.)
//
// Important observation 1: each card must contain one value between 1 and N,
// and one value between N+1 and 2N. Rationale: suppose card i is placed in
// the configuration at (1-based) index j. Then on the top row there are
// (N - j) numbers greater than A[i], and on the bottom row (j - 1) numbers
// greater than B[i], for a total of (N - 1) numbers greater than either A[i]
// or B[i]. But if max(A[i], B[i]) <= N, then there are at least N numbers
// greater than both A[i] and B[i], and only (N - 1) places to put them.
// (The argument that not both values can be greater than N is symmetric.)
//
// As a consequence, we can detect an unsolvable state early if a card does not
// contain exactly one value in [1..N] and one in [N+1..2N].
//
// Important observation 2: from the above, it follows that if we look at the
// solution as a 2xN matrix, each column contains exactly one value beteen 1 and
// N. By definition, on the top row, these values occur in increasing order, and
// on the bottom, these values occur in decreasing order.
//
// Example:
//
//    Front (increasing):      1 4 c d 5
//    Back (decreasing):       a b 3 2 e
//
// Consider now the sequence [1..N], and the complementary sequence that
// consists of the values on the other side of the cards. In the example above,
// that would be [1, 2, 3, 4, 5] -> [a, d, c, b, e]. Now, the question is if we
// can partition this complementary sequence into two decreasing sequences.
//
// We can greedily try to construct two subsequences, which suffices to prove
// whether a solution exists. However, the problem asks for the minimum number
// of card flips!
//
// Consider the sample input:
//
//  3 10
//  6 4
//  1 9
//  5 8
//  2 7
//
// Sort by the lower number on each card:
//
//  1 9
//  2 7
//  3 10
//  6 4
//  5 8
//
// This leads to the complementary sequence [9, 7, 10, 6*, 8] (note that *
// indicates that this number appeared on the front instead of the back side).
//
// A normal partitioning into two decreasing subsequences works as follows (this
// can be generalized to any fixed number of subsequences):
//
// last[0] = inf
// last[1] = inf
// for each value:
//   if value > last[0] and value > last[1]:
//     no solution possible
//   if value < last[0] and value > last[1]:
//     last[0] = value  // assign to first sequence
//   if value > last[0] and value < last[1]:
//     last[1] = value  // assign to second sequence
//   if value < last[0] and value < last[1]:
//     // can assign to either one. greedily assign to sequence that ends
//     // with lowest number:
//     if last[0] < last[1]:
//       last[0] = value
//     else:
//       last[1] = value
//
// This would work for us, but note that if we assign a number that appeared on
// the front of the card to the second row, or on the back of the card to the
// first row, then we have to flip the card.
//
// In the given example, we would greedily assign 9 and 7 to the front row.
// Now we must assign 10 to the back row, 6 to the front row, and 8 to the
// back row.
// [9, 7, 10, 6*, 8]

int Solve(const int N, const vector<pair<int, int>> &A) {
  vector<pair<int, bool>> value(N);
  for (auto [a, b] : A) {
    if (a <= N) {
      if (b <= N) return -1;
      value[a - 1] = {b - N - 1, false};
    } else if (b <= N) {
      value[b - 1] = {a - N - 1, true};
    } else return -1;
  }

  // TODO: explain how this all works!
  int flips = 0, flip1 = 0, flip2 = 0;
  vector<char> used(N);
  int x = N, y = N;
  for (auto [v, back] : value) {
    assert(0 <= v && v < N);
    assert(v != x && v != y);
    if (v < x && (x <= y || v > y)) {
      x = v;
      if (!back) ++flip1; else ++flip2;
    } else if (v < y && (y < x || v > x)) {
      y = v;
      if (back) ++flip1; else ++flip2;
    } else return -1;
    used[v] = true;
    while (x > 0 && used[x - 1]) --x;
    while (y > 0 && used[y - 1]) --y;
    if (x == y) {
      flips += min(flip1, flip2);
      flip1 = flip2 = 0;
    }
  }
  flips += min(flip1, flip2);
  return flips;
}

int main() {
  // Make C++ I/O not slow. It's sad that this is necessary :-(
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int N = 0;
  cin >> N;
  vector<pair<int, int>> A(N);
  for (pair<int, int> &pii : A) cin >> pii.first >> pii.second;
  cout << Solve(N, A) << endl;
}
