output_description: Print the minimum required number of additional chairs.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N M
      L_1 R_1
      :
      L_N R_N
attempt: #!/usr/bin/env python3
  import heapq
  
  
  INF = 10**9
  
  
  class SegTreeX:
  
      def __init__(self, n):
          k = 1
          while k < n:
              k *= 2
          self.n = k
          self.dat = [0] * (k * 2)
          self.pos = [i - (k - 1) for i in range(k * 2)]
  
      def update(self, i, x):
          i += self.n - 1
          self.dat[i] = x
          while 0 < i:
              i = (i - 1) // 2
              d1 = self.dat[i * 2 + 1]
              d2 = self.dat[i * 2 + 2]
              if d1 <= d2:
                  self.dat[i] = d1
                  self.pos[i] = self.pos[i * 2 + 1]
              else:
                  self.dat[i] = d2
                  self.pos[i] = self.pos[i * 2 + 2]
  
      def query(self, a, b, k=0, l=0, r=-1):
          if r < 0:
              r = self.n
          if r <= a or b <= l:
              return INF, -1
          if a <= l and r <= b:
              return self.dat[k], self.pos[k]
          else:
              vl, pl = self.query(a, b, k * 2 + 1, l, (l + r) // 2)
              vr, pr = self.query(a, b, k * 2 + 2, (l + r) // 2, r)
              if vl <= vr:
                  return vl, pl
              else:
                  return vr, pr
  
  
  def solve(n, m, a):
  
      a.sort()
      ans = 0
      q = []
      ol = -1
      seg = SegTreeX(m + 1)
      for i in range(n):
          li, ri = a[i]
          if li != ol and 0 <= ol:
              while ol < len(q):
                  rj = heapq.heappop(q)
                  if m < rj:
                      ans += 1
                  else:
                      v, p = seg.query(rj, m + 1)
                      if p < 0 or v == 1:
                          ans += 1
                      else:
                          seg.update(p, 1)
          heapq.heappush(q, ri)
          ol = li
      while ol < len(q):
          rj = heapq.heappop(q)
          if m < rj:
              ans += 1
          else:
              v, p = seg.query(rj, m + 1)
              if p < 0 or v == 1:
                  ans += 1
              else:
                  seg.update(p, 1)
  
      if ans < n - m:
          ans = n - m
  
      return ans
  
  
  def main():
      n, m = input().split()
      n = int(n)
      m = int(m)
      a = []
      for _ in range(n):
          li, ri = input().split()
          li = int(li)
          ri = int(ri)
          a.append((li, ri))
  
      print(solve(n, m, a))
  
  
  if __name__ == "__main__":
      main()
  
problem_description: Statement
  
  There are M chairs arranged in a line. The coordinate of the i-th chair (1 ≤ i
  ≤ M) is i.
  
  N people of the Takahashi clan played too much games, and they are all
  suffering from backaches. They need to sit in chairs and rest, but they are
  particular about which chairs they sit in. Specifically, the i-th person
  wishes to sit in a chair whose coordinate is not greater than L_i, or not less
  than R_i. Naturally, only one person can sit in the same chair.
  
  It may not be possible for all of them to sit in their favorite chairs, if
  nothing is done. Aoki, who cares for the health of the people of the Takahashi
  clan, decides to provide additional chairs so that all of them can sit in
  chairs at their favorite positions.
  
  Additional chairs can be placed at arbitrary real coordinates. Find the
  minimum required number of additional chairs.
samples: [{"input": "4 4\n    0 3\n    2 3\n    1 3\n    3 4", "output": "0\n    \n\nThe four people can sit in chairs at the coordinates 3, 2, 1 and 4,\nrespectively, and no more chair is needed.\n\n* * *"}, {"input": "7 6\n    0 7\n    1 5\n    3 6\n    2 7\n    1 6\n    2 6\n    3 7", "output": "2\n    \n\nIf we place additional chairs at the coordinates 0 and 2.5, the seven people\ncan sit at coordinates 0, 5, 3, 2, 6, 1 and 2.5, respectively.\n\n* * *"}, {"input": "3 1\n    1 2\n    1 2\n    1 2", "output": "2\n    \n\n* * *"}, {"input": "6 6\n    1 6\n    1 6\n    1 5\n    1 5\n    2 6\n    2 6", "output": "2"}]