import sys from bisect import bisect_left class FenwickTree: def __init__(self, size): self.size = size self.tree = [0] * (size + 2) def update(self, idx, delta=1): idx += 1 while idx <= self.size + 1: self.tree[idx] += delta idx += idx & -idx def query(self, idx): idx += 1 res = 0 while idx > 0: res += self.tree[idx] idx -= idx & -idx return res def main(): input = sys.stdin.read().split() ptr = 0 N, M = int(input[ptr]), int(input[ptr+1]) ptr +=2 chords = [] for _ in range(N): a, b = int(input[ptr]), int(input[ptr+1]) ptr +=2 if a > b: a, b = b, a chords.append((a, b)) # Sort the chords based on their s_i (start) chords.sort() ft = FenwickTree(M) total = 0 for s, e in chords: if s < e: # Forward chord, query (s, e) upper = e -1 count = ft.query(upper) - ft.query(s) total += count else: # Backward chord (s >= e), split into (s, M-1) and [0, e-1] # Part1: (s, M-1] part1 = ft.query(M-1) - ft.query(s) # Part2: [0, e-1] part2 = 0 if e > 0: part2 = ft.query(e-1) total += part1 + part2 # Add e to Fenwick tree. Ensure e is in [0, M-1] ft.update(e) print(total) if __name__ == "__main__": main()