output_description: Print the minimum possible value of Z, where Z is the largest ugliness of the
  N pairs.
  
  * * *
input_description: Input is given from Standard Input in the following format:
  
      
      
      N M
      a_1 a_2 \cdots a_{2N}
attempt: N, M = map(int, input().split())
  a = list(map(int, input().split()))
  
  
  def leftSearch(hist, index):
      if index >= len(hist):
          index = len(hist) - 1
      while index >= 0:
          if hist[index] > 0:
              return index
          index -= 1
      return -1
  
  
  def rightSearch(hist, index):
      if index < 0:
          index = 0
      while index < len(hist):
          if hist[index] > 0:
              return index
          index += 1
      return -1
  
  
  target = 0
  
  while True:
      couple = []
      max_val = max(a)
      # get hist
      hist = [0] * (max_val + 1)
      for it in a:
          hist[it] += 1
  
      # main loop
      for _ in range(N):
          index_r = leftSearch(hist, len(hist))
          if index_r < 0:
              break
          hist[index_r] -= 1
  
          index_l = leftSearch(hist, target + M - index_r)
          if (index_l + index_r) % M > target:
              index_l = leftSearch(hist, target - index_r)
  
          if index_l < 0:
              break
          hist[index_l] -= 1
          couple.append((index_l, index_r))
      # end
      if len(couple) == N:
          # print(couple)
          break
      else:
          target += 1
  
  lev = 0
  for it in couple:
      if lev < (it[0] + it[1]) % M:
          lev = (it[0] + it[1]) % M
  
  print(lev)
  
problem_description: Statement
  
  Let M be a positive integer.
  
  You are given 2 N integers a_1, a_2, \ldots, a_{2 N}, where 0 \leq a_i < M for
  each i.
  
  Consider dividing the 2 N integers into N pairs. Here, each integer must
  belong to exactly one pair.
  
  We define the _ugliness_ of a pair (x, y) as (x + y) \mod M. Let Z be the
  largest ugliness of the N pairs. Find the minimum possible value of Z.
samples: [{"input": "3 10\n    0 2 3 4 5 9", "output": "5\n    \n\nOne solution is to form pairs (0, 5), (2, 3) and (4, 9), with ugliness 5, 5\nand 3, respectively.\n\n* * *"}, {"input": "2 10\n    1 9 1 9", "output": "0\n    \n\nPairs (1, 9) and (1, 9) should be formed, with ugliness both 0."}]