program a_sequence_of_permutations
  implicit none
  integer :: n, k, m, i
  integer, allocatable :: p(:), q(:), a(:), b(:), c(:), d(:)
  read(*,*) n, k
  allocate(p(n),q(n),a(n),b(n),c(n),d(n))
  read(*,*) p(1:n)
  read(*,*) q(1:n)
  select case (mod(k-1,6)+1)
  case (1)
    a = identity(n)
    b = p
  case (2)
    a = identity(n)
    b = q
  case (3)
    a = identity(n)
    b = composite(q,inverse(p))
  case (4)
    a = q
    b = inverse(p)
  case (5)
    a = composite(q,inverse(p))
    b = inverse(q)
  case (6)
    a = composite(q,inverse(p))
    b = composite(inverse(q),p)
  end select
  m = (k-1)/6
  c = composite(composite(q,inverse(p)),composite(inverse(q),p))
  d = identity(n)
  do while (m > 0)
    if (btest(m,0)) d = composite(c,d)
    c = composite(c,c)
    m = rshift(m,1)
  end do
  a = composite(a,d)
  call output(composite(a,composite(b,inverse(a))))
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
  function identity(n) result(ret)
    integer, intent(in) :: n
    integer :: ret(n), i
    do i = 1, n
      ret(i) = i
    end do
  end
  function composite(a,b) result(ret)
    integer, intent(in) :: a(:), b(:)
    integer :: n, ret(size(a)), i
    n = size(a)
    do i = 1, n
      ret(i) = a(b(i))
    end do
  end
  function inverse(a) result(ret)
    integer, intent(in) :: a(:)
    integer :: n, ret(size(a)), i
    n = size(a)
    do i = 1, n
      ret(a(i)) = i
    end do
  end
end program a_sequence_of_permutations