program a_sequence_of_permutations
  implicit none
  integer :: n, k, p(100000) = 0, q(100000) = 0, r(100000,2) = 0, i
  read(*,*) n, k
  read(*,*) p(1:n)
  read(*,*) q(1:n)
  if (k < 3) then
    if (k == 1) then
      call output(p(1:n))
    else
      call output(q(1:n))
    end if
    stop
  end if
  do i = 3, k
    r(1:n,1) = p(1:n)
    r(1:n,2) = q(1:n)
    call quick_sort(r(1:n,:),1)
    p(1:n) = q(1:n)
    q(1:n) = r(1:n,2)
  end do
  call output(q(1:n))
contains
  subroutine output(a)
    integer, intent(in) :: a(:)
    integer :: n, i
    n = size(a)
    write(*,'(i0)',advance='no') a(1)
    do i = 2, n
      write(*,'(x,i0)',advance='no') a(i)
    end do
    write(*,*)
  end
  recursive subroutine quick_sort(a,i)
    integer, intent(inout) :: a(:,:)
    integer, intent(in) :: i
    integer :: n, l, r, m
    integer :: p, b(size(a,2))
    n = size(a,1)
    l = 1
    r = n
    m = (l+r)/2
    p = a(m,i)+a(l,i)+a(r,i)-max(a(m,i),a(l,i),a(r,i))-min(a(m,i),a(l,i),a(r,i))
    do
      do while (a(l,i) < p)
        l = l+1
      end do
      do while (a(r,i) > p)
        r = r-1
      end do
      if (l >= r) exit
      b = a(l,:)
      a(l,:) = a(r,:)
      a(r,:) = b
      l = l+1
      r = r-1
    end do
    if (1 < l-1) call quick_sort(a(1:l-1,:),i)
    if (r+1 < n) call quick_sort(a(r+1:n,:),i)
  end
end program a_sequence_of_permutations