program reversing_and_concatenating
  implicit none
  integer :: n, k, i, j
  character(5000) :: s = "", o = ""
  character(10000) :: u = ""
  read(*,*) n, k
  read(*,*) s
  do i = 1, k
    o = s(1:n)
    u = s(1:n)
    call reverse(s(1:n))
    u = u(1:n)//s(1:n)
    do j = 1, n+1
      s = min(s(1:n),u(j:j+n-1))
    end do
    if (i < k) call reverse(s(1:n))
    if (o == s) then
      write(*,'(a)') s(1:n)
      stop
    end if
  end do
  write(*,'(a)') s(1:n)
contains
  subroutine reverse(s)
    character(*), intent(inout) :: s
    character(1) :: t
    integer :: n, i
    n = len(s)
    do i = 1, n/2
      t = s(n-i+1:n-i+1)
      s(n-i+1:n-i+1) = s(i:i)
      s(i:i) = t
    end do
  end
end program reversing_and_concatenating