object Main extends App {
  import scala.io.StdIn._

  val Array(n, m) = readLine.trim.split(' ').map(_.toInt)
  val students = List.tabulate(n){_ ⇒ readLine.trim.toInt}
  (1 to m).foldLeft(students){case (s, b) ⇒ swappingBibs(s, b)}.foreach(println(_))
  def swappingBibs(students: List[Int], baton: Int): List[Int] = {
    students match {
      case Nil ⇒ Nil
      case a::b::t if a % baton > b % baton ⇒ b::swappingBibs(a::t, baton)
      case h::t ⇒ h::swappingBibs(t, baton)
    }
  }
  implicit class ScopeFunc[T](value: T){
    def let[A](func: T ⇒ A): A = func(value)
    def also(func: T ⇒ Unit): T = {
      func(value)
      value
    }
  }
}
