#include <bits/stdc++.h>
using namespace std;
const int Max1 = 1e7 + 9, Max2 = 1e5 + 9;
int a[Max1], b[Max1], M[Max1], na[Max1], nb[Max1], g[Max2], h[Max2];
int main() {
  for (long long i = 2; i < Max1; i++)
    if (!M[i]) {
      M[i] = i;
      for (long long j = i * i; j < Max1; j += i) M[j] = i;
    }
  int n, m, x;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> x;
    g[i] = x;
    while (x > 1) a[M[x]]++, x /= M[x];
  }
  for (int i = 1; i <= m; i++) {
    cin >> x;
    h[i] = x;
    while (x > 1) b[M[x]]++, x /= M[x];
  }
  for (int i = 2; i < Max1; i++)
    if (a[i] || b[i]) {
      int mn = min(a[i], b[i]);
      na[i] = a[i] - mn, nb[i] = b[i] - mn;
    }
  for (int i = 1; i <= n; i++) {
    int x = g[i], t = M[x];
    while (x > 1) {
      while (x % t == 0 && na[t] < a[t]) x /= t, a[t]--, g[i] /= t;
      while (x % t == 0) x /= t;
      t = M[x];
    }
  }
  for (int i = 1; i <= m; i++) {
    int x = h[i], t = M[x];
    while (x > 1) {
      while (x % t == 0 && nb[t] < b[t]) x /= t, b[t]--, h[i] /= t;
      while (x % t == 0) x /= t;
      t = M[x];
    }
  }
  cout << n << ' ' << m << '\n';
  for (int i = 1; i <= n; i++) cout << g[i] << ' ';
  cout << '\n';
  for (int i = 1; i <= m; i++) cout << h[i] << ' ';
}
