#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:16777216")
using namespace std;
const double eps = 1e-9;
const double pi = acos(-1.0);
const int maxn = (int)1e7 + 10;
int p[maxn], a[maxn], b[maxn], A[maxn], B[maxn];
int main() {
  for (int i = 2; i * i < maxn; i++) {
    if (p[i]) continue;
    for (int j = i * i; j < maxn; j += i) {
      if (p[j]) continue;
      p[j] = i;
    }
  }
  for (int i = 2; i < maxn; i++)
    if (!p[i]) p[i] = i;
  int n, m, x;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    A[i] = x;
    while (x > 1) {
      a[p[x]]++;
      x /= p[x];
    }
  }
  for (int i = 0; i < m; i++) {
    scanf("%d", &x);
    B[i] = x;
    while (x > 1) {
      b[p[x]]++;
      x /= p[x];
    }
  }
  for (int i = 2; i < maxn; i++) {
    int val = min(a[i], b[i]);
    a[i] = val;
    b[i] = val;
  }
  for (int i = 0; i < n; i++) {
    int x = A[i];
    int val = 1;
    while (x > 1) {
      if (a[p[x]]) {
        a[p[x]]--;
        val *= p[x];
      }
      x /= p[x];
    }
    A[i] /= val;
  }
  for (int i = 0; i < m; i++) {
    int x = B[i];
    int val = 1;
    while (x > 1) {
      if (b[p[x]]) {
        b[p[x]]--;
        val *= p[x];
      }
      x /= p[x];
    }
    B[i] /= val;
  }
  cout << n << " " << m << endl;
  for (int i = 0; i < n; i++) printf("%d ", A[i]);
  cout << endl;
  for (int i = 0; i < m; i++) printf("%d ", B[i]);
  return 0;
}
