#include <bits/stdc++.h>
using namespace std;
const int N = 10 * 1000 * 1000 + 10;
const int M = 100 * 1000 + 20;
int n, m, a, cntp[N], cnt[N], cnt_[N], p[N], x[M], y[M];
bool mark[N];
int main() {
  for (int i = 2; i <= N; ++i) {
    if (mark[i]) continue;
    p[i] = i;
    for (int j = 2; j * i <= N; ++j) {
      mark[i * j] = 1;
      p[i * j] = i;
    }
  }
  cin >> n >> m;
  for (int i = 0; i < n; ++i) {
    cin >> a;
    x[i] = a;
    while (a > 1) {
      cnt[p[a]]++;
      a /= p[a];
    }
  }
  for (int i = 0; i < m; ++i) {
    cin >> a;
    y[i] = a;
    while (a > 1) {
      cnt_[p[a]]++;
      cntp[p[a]]++;
      a /= p[a];
    }
  }
  for (int i = 0; i < n; ++i) {
    a = x[i];
    while (a > 1) {
      if (cnt[p[a]] && cnt_[p[a]]) {
        cnt[p[a]]--;
        cnt_[p[a]]--;
        x[i] /= p[a];
      }
      a /= p[a];
    }
  }
  for (int i = 0; i < m; ++i) {
    a = y[i];
    while (a > 1) {
      if (cntp[p[a]] > cnt_[p[a]]) {
        cntp[p[a]]--;
        y[i] /= p[a];
      }
      a /= p[a];
    }
  }
  cout << n << ' ' << m << '\n';
  for (int i = 0; i < n; ++i) cout << x[i] << ' ';
  cout << '\n';
  for (int i = 0; i < m; ++i) cout << y[i] << ' ';
  cout << '\n';
  return 0;
}
