#include <bits/stdc++.h>
int prime[670000], m, i, j, f[670000], g[670000], x, a[670000], b[670000], r, n;
int p[10000000];
int main() {
  memset(p, 0, sizeof(p));
  m = 0;
  for (i = 2; i < 10000000; i++) {
    if (p[i] == 0) {
      prime[m] = i;
      p[i] = m;
      m++;
    }
    for (j = 0; j < m && (long long)i * prime[j] < 10000000; j++) {
      p[i * prime[j]] = -1;
      if (i % prime[j] == 0) break;
    }
  }
  r = m;
  while (scanf("%d%d", &n, &m) == 2) {
    memset(f, 0, sizeof(f));
    memset(g, 0, sizeof(g));
    for (i = 0; i < n; i++) {
      scanf("%d", &x);
      a[i] = x;
      for (j = 0; (long long)prime[j] * prime[j] <= x; j++) {
        while (x % prime[j] == 0) {
          x = x / prime[j];
          f[j]++;
        }
      }
      if (x != 1) f[p[x]]++;
    }
    for (i = 0; i < m; i++) {
      scanf("%d", &x);
      b[i] = x;
      for (j = 0; (long long)prime[j] * prime[j] <= x; j++) {
        while (x % prime[j] == 0) {
          x = x / prime[j];
          g[j]++;
          if (f[j]) {
            f[j]--;
            b[i] = b[i] / prime[j];
          }
        }
      }
      if (x != 1) {
        g[p[x]]++;
        if (f[p[x]]) {
          f[p[x]]--;
          b[i] = b[i] / x;
        }
      }
    }
    for (i = 0; i < n; i++) {
      x = a[i];
      for (j = 0; (long long)prime[j] * prime[j] <= x; j++) {
        while (x % prime[j] == 0) {
          x = x / prime[j];
          f[j]++;
          if (g[j]) {
            g[j]--;
            a[i] = a[i] / prime[j];
          }
        }
      }
      if (x != 1) {
        f[p[x]]++;
        if (g[p[x]]) {
          g[p[x]]--;
          a[i] = a[i] / x;
        }
      }
    }
    printf("%d %d\n", n, m);
    for (i = 0; i < n; i++) printf("%d ", a[i]);
    puts("");
    for (i = 0; i < m; i++) printf("%d ", b[i]);
    puts("");
  }
  return 0;
}
