#include <bits/stdc++.h>
using namespace std;
int n, m;
bool p[10000006];
int smpf[10000006];
int u[10000007], d[10000007];
int num[100005], den[100005];
int main() {
  scanf("%d", &n);
  scanf("%d", &m);
  for (int i = 1; i <= n; i++) scanf("%d", &num[i]);
  for (int i = 1; i <= m; i++) scanf("%d", &den[i]);
  memset(p, 1, sizeof p);
  p[0] = 0;
  p[1] = 0;
  for (int i = 2; i <= 10000000; i++) {
    if (p[i]) {
      smpf[i] = i;
      for (int j = 2 * i; j <= 10000000; j += i) {
        if (p[j]) smpf[j] = i;
        p[j] = 0;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    int x = num[i];
    while (x != 1) {
      int j = smpf[x], cnt = 0;
      while (x % j == 0) x /= j, cnt++;
      u[j] += cnt;
    }
  }
  for (int i = 1; i <= m; i++) {
    int x = den[i];
    while (x != 1) {
      int j = smpf[x], cnt = 0;
      while (x % j == 0) x /= j, cnt++;
      d[j] += cnt;
    }
  }
  for (int i = 1; i <= 10000000; i++) {
    u[i] = min(u[i], d[i]);
    d[i] = u[i];
  }
  for (int i = 1; i <= n; i++) {
    int x = num[i];
    while (x != 1) {
      int j = smpf[x];
      while (u[j] > 0 && num[i] % j == 0) num[i] /= j, u[j]--;
      while (x % j == 0) x /= j;
    }
  }
  for (int i = 1; i <= m; i++) {
    int x = den[i];
    while (x != 1) {
      int j = smpf[x];
      while (d[j] > 0 && den[i] % j == 0) den[i] /= j, d[j]--;
      while (x % j == 0) x /= j;
    }
  }
  printf("%d %d\n", n, m);
  for (int i = 1; i <= n; i++) printf("%d ", num[i]);
  printf("\n");
  for (int i = 1; i <= m; i++) printf("%d ", den[i]);
  printf("\n");
  return 0;
}
