program main
  implicit none
  integer :: N,M
  integer,allocatable :: a(:),b(:)
  integer :: minl(1),maxl(1)
  integer :: minl_bf(1),maxl_bf(1)
  integer :: tmp
  integer ::i 
  read(*,*) N,M
  allocate(a(2*N))
  allocate(b(N))
  read(*,*) A
  minl_bf = -1
  maxl_bf = -1
  do
     do i = 1,N
        b(i)=mod(a(2*i-1)+a(2*i),M)
     end do
     if ( maxl_bf(1) == maxl(1) .and. minl_bf(1) == minl(1) ) then
        exit
     end if
     maxl=maxloc(b)
     minl=minloc(b)
     ! swap
     tmp = a(2*minl(1)) 
     a(2*minl(1)) = a(2*maxl(1)) 
     a(2*maxl(1)) = tmp
     maxl_bf = maxl
     minl_bf = minl
  end do
  write(*,'(i0)') maxval(b)
  stop
end program main
