#include <bits/stdc++.h>
using namespace std;
void read_file(bool outToFile = true) {}
int n, m;
int A[100000 + 9], B[100000 + 9];
int cntA[664579 + 1 + 9], cntB[664579 + 1 + 9];
bitset<10000000 + 1 + 9> isPrime;
vector<int> prime;
int pdiv[10000000 + 1 + 9];
void sieve() {
  isPrime.set();
  isPrime[0] = isPrime[1] = 0;
  prime.push_back(2);
  pdiv[2] = 0;
  for (int i = 4; i < 10000000 + 1 + 9; i += 2) isPrime[i] = 0, pdiv[i] = 0;
  for (int i = 3; i < 10000000 + 1 + 9; i += 2) {
    if (!isPrime[i]) continue;
    pdiv[i] = prime.size();
    prime.push_back(i);
    if (1LL * i * i >= 10000000 + 1 + 9) continue;
    for (int j = i * i; j < 10000000 + 1 + 9; j += i)
      isPrime[j] = 0, pdiv[j] = pdiv[i];
  }
}
void factorize(int x, int cntA[]) {
  while (x != 1) {
    int pf_idx = pdiv[x];
    cntA[pf_idx]++;
    x /= prime[pf_idx];
  }
}
void doit(int A[], int n, int cntA[]) {
  memset(cntA, 0, sizeof cntA);
  for (int i = 0; i < n; i++) {
    factorize(A[i], cntA);
  }
}
void go(int A[], int n, int cntA[]) {
  for (int i = 0; i < n; i++) {
    int x = A[i];
    int y = 1;
    while (x != 1) {
      int pf_idx = pdiv[x];
      x /= prime[pf_idx];
      if (cntA[pf_idx] == 0) continue;
      cntA[pf_idx]--;
      y *= prime[pf_idx];
    }
    printf("%d%c", y, " \n"[i == n - 1]);
  }
}
int main() {
  read_file();
  sieve();
  while (scanf("%d%d", &n, &m) != EOF) {
    for (int i = 0; i < n; i++) scanf("%d", &A[i]);
    for (int j = 0; j < m; j++) scanf("%d", &B[j]);
    doit(A, n, cntA);
    doit(B, m, cntB);
    for (int i = 0; i < 664579 + 1 + 9; i++) {
      int mn = min(cntA[i], cntB[i]);
      cntA[i] -= mn;
      cntB[i] -= mn;
    }
    printf("%d %d\n", n, m);
    go(A, n, cntA);
    go(B, m, cntB);
  }
}
