#include <bits/stdc++.h>
using namespace std;
const int maxn = 100007, maxt = 10000007;
int n, m;
int a[maxn], b[maxn];
int A[maxt], B[maxt], C[maxt], D[maxt];
int fac[maxt];
void gao(int x, int A[]) {
  for (int i = x; i > 1; i /= fac[i]) A[fac[i]]++;
}
void Do(int &x, int A[]) {
  for (int i = x; i > 1; i /= fac[i])
    if (A[fac[i]] > 0) A[fac[i]]--, x /= fac[i];
}
void work() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 1; i <= m; i++) scanf("%d", &b[i]);
  for (int i = 1; i <= n; i++) gao(a[i], A);
  for (int i = 1; i <= m; i++) gao(b[i], B);
  for (int i = 1; i <= 10000000; i++) C[i] = D[i] = min(A[i], B[i]);
  for (int i = 1; i <= n; i++) Do(a[i], C);
  for (int i = 1; i <= m; i++) Do(b[i], D);
  printf("%d %d\n", n, m);
  for (int i = 1; i <= n; i++)
    if (i < n)
      printf("%d ", a[i]);
    else
      printf("%d\n", a[i]);
  for (int i = 1; i <= m; i++)
    if (i < m)
      printf("%d ", b[i]);
    else
      printf("%d\n", b[i]);
}
int main() {
  for (int i = 2; i <= 10000000; i++)
    if (fac[i] == 0) {
      for (int j = i; j <= 10000000; j += i) fac[j] = i;
    }
  work();
}
