import sys input = sys.stdin.readline class Fenwick_Tree: def __init__(self, n): self._n = n self.data = [0] * n def add(self, p, x): assert 0 <= p < self._n p += 1 while p <= self._n: self.data[p - 1] += x p += p & -p def sum(self, l, r): assert 0 <= l <= r <= self._n return self._sum(r) - self._sum(l) def _sum(self, r): s = 0 while r > 0: s += self.data[r - 1] r -= r & -r return s N, M = map(int, input().split()) D = [] T = Fenwick_Tree(2 * M + 1) for i in range(N): a, b = map(int, input().split()) if a > b: a, b = b, a T.add(a, 1) T.add(b, -1) D.append((a, b)) D.sort() ans = 0 for a, b in D: ans += T.sum(a, b + 1) T.add(b, 1) print(ans)