#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<int> A, B;
int d[10000010];
int a[10000010];
int pr[10010], pn;
bool isp[100010];
void GetPrime(int n) {
  memset(isp + 2, 1, sizeof(isp) - 2);
  for (int i = 2; i < n; i++) {
    if (isp[i]) pr[pn++] = i;
    for (int j = 0; j < pn && pr[j] * i < n; j++) {
      isp[pr[j] * i] = 0;
      if (i % pr[j] == 0) break;
    }
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  GetPrime(10000);
  cin >> n >> m;
  A.resize(n), B.resize(m);
  for (int i = 0; i < n; i++) cin >> A[i];
  for (int i = 0; i < m; i++) cin >> B[i];
  for (int i = 0; i < n; i++) {
    int t = A[i];
    for (int j = 0; pr[j] * pr[j] <= t; j++) {
      while (t % pr[j] == 0) d[pr[j]]++, t /= pr[j];
    }
    if (t > 1) d[t]++;
  }
  for (int i = 0; i < m; i++) {
    int t = B[i];
    for (int j = 0; pr[j] * pr[j] <= t; j++) {
      while (t % pr[j] == 0) d[pr[j]]--, t /= pr[j];
    }
    if (t > 1) d[t]--;
  }
  cout << n << " " << m << endl;
  for (int i = 0; i < n; i++) {
    int r = 1;
    int t = A[i];
    for (int j = 0; pr[j] * pr[j] <= t; j++) {
      while (t % pr[j] == 0) {
        t /= pr[j];
        if (d[pr[j]] > 0) {
          d[pr[j]]--;
          r *= pr[j];
        }
      }
    }
    if (t > 1 && d[t] > 0) d[t]--, r *= t;
    cout << r << " ";
  }
  cout << endl;
  for (int i = 0; i < m; i++) {
    int r = 1;
    int t = B[i];
    for (int j = 0; pr[j] * pr[j] <= t; j++) {
      while (t % pr[j] == 0) {
        t /= pr[j];
        if (d[pr[j]] < 0) {
          d[pr[j]]++;
          r *= pr[j];
        }
      }
    }
    if (t > 1 && d[t] < 0) d[t]++, r *= t;
    cout << r << " ";
  }
  cout << endl;
  return 0;
}
