program integer_of_the_s
  implicit none
  integer :: n, k, a, c(-1:31), i, j
  integer(8) :: m(-1:31), b
  c = -1
  m = 0_8
  read(*,*) n, k
  c(-1) = k
  do i = 0, 31
    if (btest(k,i)) then
      c(i) = ibclr(k,i)
      do j = 0, i-1
        c(i) = ibset(c(i),j)
      end do
    end if
  end do
  do i = 1, n
    read(*,*) a, b
    do j = -1, 31
      if (c(j).lt.0) cycle
      if (or(c(j),a).eq.c(j)) then
        m(j) = m(j)+b
      end if
    end do
  end do
  write(*,'(i0)') maxval(m)
  stop
end program integer_of_the_s