import java.util.*;
public class Main
{
final static double MAX = 1000000 ;
static boolean prime[] = new boolean [(int) (MAX + 1.0)] ;
static int n;
static int q;
static List<Stack> a;
static List<Stack> b;
static int[] firstQ;
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
q = sc.nextInt();
a = new ArrayList<Stack<Integer>>();
b = new ArrayList<Stack<Integer>>();
//create q stacks each
for(int i=0; i<=q; i++){
a.add(new Stack<>());
b.add(new Stack<>());
}
for(int i=0; i<n; i++)
a.get(0).push(sc.nextInt());
SieveOfEratosthenes();
firstQ = getFirstNprime(q);
//System.out.println(firstQ[5]);
play();
}
private static void play()
{
for(int i=0; i<q; i++)
{
while(!a.get(i).isEmpty()) {
int x = a.get(i).pop();
if(x%firstQ[i+1]==0) //if divisible by ith prime number
b.get(i+1).add(x);
else
a.get(i+1).add(x);
}
}
// System.out.println(a.get(q));
// System.out.println(b.get(q));
while(!b.get(q).isEmpty())
System.out.println(b.get(q).pop());
while(!a.get(q).isEmpty())
System.out.println(a.get(q).pop());
}
private static int[] getFirstNprime(int q) {
int count = 1, num = 1;
int[] a = new int[q+1];
while (count <= q)
{
// if the number is prime add it
if (prime[num]) {
a[count] = num;
count++;
}
// get to next number
num++;
}
return a;
}
static void SieveOfEratosthenes()
{
for(int i = 0; i <= MAX; i++)
prime[i] = true ;
prime[1] = false;
for (int p = 2; p * p <= MAX; p++) {
// If prime[p] is not changed, then it is a prime
if (prime[p] == true) {
// Set all multiples of p to non-prime
for (int i = p * 2; i <= MAX; i += p)
prime[i] = false;
}
}
}
}