#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;

const int N = 200100;
int a[N], b[N], num[N];
int m, n, tn, tmp[N], T[N];
LL ans;

int find(int x) {int ret = 0; for (; x; x -= (x & -x)) ret += T[x]; return ret;}
void plus(int x) {for (; x <= n; x += (x & -x)) T[x]++;}

int main()
{

	scanf("%d", &m);
	for (int i = 1; i <= m; i++) scanf("%d%d", &a[i], &b[i]), tmp[++tn] = a[i], tmp[++tn] = b[i];
	sort(tmp + 1, tmp + 1 + tn);
	tn = unique(tmp + 1, tmp + 1 + tn) - tmp - 1;

	n = tn;
	for (int i = 1; i <= n; i++) num[i] = i;
	for (int i = 1; i <= m; i++)
	{
		a[i] = lower_bound(tmp + 1, tmp + 1 + tn, a[i]) - tmp;
		b[i] = lower_bound(tmp + 1, tmp + 1 + tn, b[i]) - tmp;

		swap(num[a[i]], num[b[i]]);
	}

	ans = 0;
	for (int i = n; i >= 1; i--)
	{
		ans = ans + find(num[i] - 1);
		plus(num[i]);
 	}

	for (int i = 1; i <= n; i++)
		if (num[i] != i)
		{
			if (num[i] > i)
			{
				ans = ans + (tmp[num[i]] - tmp[i]) - (num[i] - i);
			} else
			{
				ans = ans + (tmp[i] - tmp[num[i]]) - (i - num[i]);
			}
		}

	printf("%I64d\n", ans);

	return 0;
}
