There is an infinite sequence consisting of all positive integers in the increasing order: p = {1, 2, 3, ...}. We performed n swap operations with this sequence. A swap(a, b) is an operation of swapping the elements of the sequence on positions a and b. Your task is to find the number of inversions in the resulting sequence, i.e. the number of such index pairs (i, j), that i < j and pi > pj.

Input

The first line contains a single integer n (1 ≤ n ≤ 105) — the number of swap operations applied to the sequence.

Each of the next n lines contains two integers ai and bi (1 ≤ ai, bi ≤ 109, ai ≠ bi) — the arguments of the swap operation.

Output

Print a single integer — the number of inversions in the resulting sequence.

Examples

Input

2
4 2
1 4


Output

4


Input

3
1 6
3 4
2 5


Output

15

Note

In the first sample the sequence is being modified as follows: <image>. It has 4 inversions formed by index pairs (1, 4), (2, 3), (2, 4) and (3, 4).
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Arrays;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;

// Codeforces Round #301 (Div. 2)
// Infinite Inversions
public class Problem_540_E {


  public static void main(String[] args)
  {
    InputStream inputStream = System.in;
    OutputStream outputStream = System.out;
    PrintWriter out = new PrintWriter(outputStream);
    Problem_540_E solver = new Problem_540_E();
    solver.solve(1, new InputStreamReader(inputStream), out);
    out.close();
  }

  public class FenwickTree {
    long[] t;

    int n;

    public FenwickTree(int n)
    {
      this.n = n;
      t = new long[n];
    }

    public void add(int i, long value)
    {
      for(; i < n; i += (i + 1) & -(i + 1)) {
        t[i] += value;
      }
    }

    public long sum(int i)
    {
      long res = 0;
      for(; i >= 0; i -= (i + 1) & -(i + 1)) {
        res += t[i];
      }
      return res;
    }
    
    public long sum(int l, int r)
    {
      return sum(r) - sum(l-1);
    }
  }
  
  static class Item implements Comparable<Item> {
    int pos, val;

    public Item(int pos, int val) {
      this.pos = pos;
      this.val = val;
    }

    public int compareTo(Item o) {
      if (pos != o.pos) {
        return Integer.compare(pos, o.pos);
      }
      return Integer.compare(val, o.val);
    }
  }
  
  private void compress(int[] a) {
    TreeSet<Integer> set = new TreeSet<Integer>();
    for (int x : a) {
      set.add(x);
    }
    TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
    for (int x : set) {
      int id = map.size();
      map.put(x, id);
    }
    for (int i = 0; i < a.length; i++) {
      a[i] = map.get(a[i]);
    }
  }

  public void solve(int test, Reader input, PrintWriter out)
  {
    MyScanner in = new MyScanner(input);

    int q = in.nextInt();
    TreeMap<Integer, Integer> map = new TreeMap<Integer, Integer>();
    for (int i = 0; i < q; i++) {
      int pos1 = in.nextInt() - 1;
      int pos2 = in.nextInt() - 1;
      int num1 = map.containsKey(pos1) ? map.get(pos1) : pos1;
      int num2 = map.containsKey(pos2) ? map.get(pos2) : pos2;
      map.put(pos1, num2);
      map.put(pos2, num1);
    }
    Item[] a = new Item[map.size()];
    int n = 0;
    for (Map.Entry<Integer, Integer> e : map.entrySet()) {
      a[n++] = new Item(e.getKey(), e.getValue());
    }
    int[] b = new int[n];
    for (int i = 0; i < n; i++) {
      b[i] = a[i].val;
    }
    compress(b);
    long ans = 0;
    FenwickTree tree = new FenwickTree(n);
    for (int i = 0; i < n; i++) {
      ans += tree.sum(b[i], n - 1);
      tree.add(b[i], 1);
    }
    for (int idxAfter = 0; idxAfter < n; idxAfter++) {
      int idxBefore = ~Arrays.binarySearch(a, new Item(a[idxAfter].val, -1));
      ans += Math.abs(a[idxAfter].pos - a[idxBefore].pos);
      ans -= Math.abs(idxAfter - idxBefore);
    }
    out.println(ans);
  }

  //-----------MyScanner class for faster input----------
  public static class MyScanner {
    BufferedReader br;
    StringTokenizer st;

    public MyScanner(Reader reader) {
      br = new BufferedReader(reader);
    }

    String next() {
      while (st == null || !st.hasMoreElements()) {
        try {
          st = new StringTokenizer(br.readLine());
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      return st.nextToken();
    }

    int nextInt() {
      return Integer.parseInt(next());
    }

    long nextLong() {
      return Long.parseLong(next());
    }

    double nextDouble() {
      return Double.parseDouble(next());
    }

    int[] nextIntArray(int n)
    {
      int[] res = new int[n];
      for(int i = 0; i < n; i++)
        res[i] = nextInt();
      return res;
    }

    long[] nextLongArray(int n)
    {
      long[] res = new long[n];
      for(int i = 0; i < n; i++)
        res[i] = nextLong();
      return res;
    }

    double[] nextDoubleArray(int n)
    {
      double[] res = new double[n];
      for(int i = 0; i < n; i++)
        res[i] = nextDouble();
      return res;
    }

    String nextLine(){
      String str = "";
      try {
        str = br.readLine();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return str;
    }

  }
  //--------------------------------------------------------

}
