#include <bits/stdc++.h>
using namespace std;
int p[300000], pcnt;
int deg[10000000 + 10];
bool np[10000000 + 10];
int a[300000], b[300000];
int n, m;
int len, lenb;
int x, t;
void add(int x) {
  int xx = x;
  for (int j = 1; j <= pcnt; j++) {
    if (p[j] * 1ll * p[j] > xx) break;
    while (x % p[j] == 0) {
      x /= p[j];
      deg[p[j]]++;
    }
  }
  if (x > 1) {
    deg[x]++;
  }
}
void dec(int x) {
  int xx = x;
  for (int j = 1; j <= pcnt; j++) {
    if (p[j] * 1ll * p[j] > xx) break;
    while (x % p[j] == 0) {
      x /= p[j];
      deg[p[j]]--;
    }
  }
  if (x > 1) {
    deg[x]--;
  }
}
int geta(int x) {
  int ret = 1;
  int xx = x;
  for (int j = 1; j <= pcnt; j++) {
    if (p[j] * 1ll * p[j] > xx) break;
    while (x % p[j] == 0) {
      x /= p[j];
      if (deg[p[j]] > 0) {
        ret *= p[j];
        deg[p[j]]--;
      }
    }
  }
  if (x > 1 && deg[x] > 0) {
    ret *= x;
    deg[x]--;
  }
  return ret;
}
int getb(int x) {
  int ret = 1;
  int xx = x;
  for (int j = 1; j <= pcnt; j++) {
    if (p[j] * 1ll * p[j] > xx) break;
    while (x % p[j] == 0) {
      x /= p[j];
      if (deg[p[j]] < 0) {
        ret *= p[j];
        deg[p[j]]++;
      }
    }
  }
  if (x > 1 && deg[x] < 0) {
    ret *= x;
    deg[x]++;
  }
  return ret;
}
int main() {
  cin >> n >> m;
  for (int i = 2; i <= 10000000; i++)
    if (!np[i]) {
      pcnt++;
      p[pcnt] = i;
      if (i * 1ll * i > 10000000) break;
      for (int j = i * i; j <= 10000000; j += i) np[j] = 1;
    }
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
    add(a[i]);
  }
  for (int i = 1; i <= m; i++) {
    cin >> b[i];
    dec(b[i]);
  }
  for (int i = 1; i <= n; i++) a[i] = geta(a[i]);
  for (int i = 1; i <= m; i++) b[i] = getb(b[i]);
  cout << n << " " << m << endl;
  for (int i = 1; i <= n; i++) cout << a[i] << " ";
  cout << endl;
  for (int j = 1; j <= m; j++) cout << b[j] << " ";
  return 0;
}
