#include <bits/stdc++.h>
using namespace std;
const int N = (int)1e7 + 7;
const int Mx = (int)1e7;
int n, m;
int A[N], B[N];
int lp[N];
map<int, vector<int> > a, b;
void precalc() {
  for (int i = 2; i <= Mx; i++) {
    if (!lp[i]) {
      lp[i] = i;
      if (i * 1ll * i <= Mx) {
        for (int j = i * i; j <= Mx; j += i) {
          if (!lp[j]) lp[j] = i;
        }
      }
    }
  }
}
int main() {
  precalc();
  scanf("%d%d", &n, &m);
  for (int i = 1, x; i <= n; i++) {
    scanf("%d", &A[i]);
    x = A[i];
    while (x > 1) {
      int p = lp[x];
      a[p].push_back(i);
      x /= p;
    }
  }
  for (int i = 1, x; i <= m; i++) {
    scanf("%d", &B[i]);
    x = B[i];
    while (x > 1) {
      int p = lp[x];
      b[p].push_back(i);
      x /= p;
    }
  }
  for (int i = 2; i < N; i++) {
    if (lp[i] == i) {
      int len = min(a[i].size(), b[i].size());
      while (len) {
        len--;
        A[a[i].back()] /= i;
        B[b[i].back()] /= i;
        a[i].pop_back();
        b[i].pop_back();
      }
    }
  }
  printf("%d %d\n", n, m);
  for (int i = 1; i <= n; i++) {
    printf("%d ", A[i]);
  }
  printf("\n");
  for (int i = 1; i <= m; i++) {
    printf("%d ", B[i]);
  }
  return 0;
}
