#include <bits/stdc++.h>
using namespace std;
const int INF = 2147483647;
const int maxn = 10000001;
bool p[maxn] = {0};
int prime[674590], pos[maxn];
int a[100005], b[100005];
int len;
void pre_deal() {
  len = 0;
  for (int i = 2; i < maxn; i++) {
    if (!p[i]) {
      prime[len] = i;
      pos[i] = len++;
      for (int j = (i << 1); j < maxn; j += i) p[j] = 1;
    }
  }
}
map<int, int> fn;
map<int, int> fm;
int main() {
  pre_deal();
  int n, m;
  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++) {
    int tmp = a[i];
    for (int j = 0; tmp > 1; j++) {
      if (!p[tmp]) {
        fn[tmp]++;
        break;
      }
      while (tmp % prime[j] == 0) {
        fn[prime[j]]++;
        tmp /= prime[j];
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    int tmp = b[i];
    for (int j = 0; tmp > 1; j++) {
      if (!p[tmp]) {
        fm[tmp]++;
        break;
      }
      while (tmp % prime[j] == 0) {
        fm[prime[j]]++;
        tmp /= prime[j];
      }
    }
  }
  printf("%d %d\n", n, m);
  for (int i = 1; i <= n; i++) {
    int tmp = a[i];
    int ans = 1;
    for (int j = 0; tmp > 1; j++) {
      if (!p[tmp]) {
        if (fm[tmp] > 0) {
          fm[tmp]--;
        } else
          ans *= tmp;
        break;
      }
      while (tmp % prime[j] == 0) {
        if (fm[prime[j]] > 0)
          fm[prime[j]]--;
        else
          ans *= prime[j];
        tmp /= prime[j];
      }
    }
    printf("%d ", ans);
  }
  puts("");
  for (int i = 1; i <= m; i++) {
    int tmp = b[i];
    int ans = 1;
    for (int j = 0; tmp > 1; j++) {
      if (!p[tmp]) {
        if (fn[tmp] > 0) {
          fn[tmp]--;
        } else
          ans *= tmp;
        break;
      }
      while (tmp % prime[j] == 0) {
        if (fn[prime[j]] > 0)
          fn[prime[j]]--;
        else
          ans *= prime[j];
        tmp /= prime[j];
      }
    }
    printf("%d ", ans);
  }
  puts("");
  return 0;
}
