#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int N = 1e7;
const long long mod = 1e9 + 7;
const int bits = 20;
const long double pi = 3.14159265358979323846;
int firstf[N + 1], isprime[N + 1];
vector<int> primes;
map<int, int> indx;
void pre() {
  firstf[1] = 1;
  for (int i = 2; i <= N; i++) {
    if (!isprime[i]) {
      indx[i] = primes.size();
      primes.push_back(i);
      firstf[i] = i;
      for (int j = 2 * i; j <= N; j += i) {
        isprime[j] = 1;
        if (!firstf[j]) {
          firstf[j] = i;
        }
      }
    }
  }
}
void solve() {
  pre();
  int n, m;
  cin >> n >> m;
  vector<int> val1(primes.size()), val2(primes.size());
  vector<int> arr(n), brr(m);
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
    int x = arr[i];
    while (x != 1) {
      int ff = firstf[x];
      val1[indx[ff]]++;
      x /= ff;
    }
  }
  for (int i = 0; i < m; i++) {
    cin >> brr[i];
    int x = brr[i];
    while (x != 1) {
      int ff = firstf[x];
      val2[indx[ff]]++;
      x /= ff;
    }
  }
  for (int i = 0; i < primes.size(); i++) {
    int mi = min(val1[i], val2[i]);
    val1[i] = mi;
    val2[i] = mi;
  }
  for (int i = 0; i < n; i++) {
    int x = arr[i];
    while (x != 1) {
      int ff = firstf[x];
      if (val1[indx[ff]]) {
        arr[i] /= ff;
        val1[indx[ff]]--;
      }
      x /= ff;
    }
  }
  for (int i = 0; i < m; i++) {
    int x = brr[i];
    while (x != 1) {
      int ff = firstf[x];
      if (val2[indx[ff]]) {
        brr[i] /= ff;
        val2[indx[ff]]--;
      }
      x /= ff;
    }
  }
  cout << n << " " << m << "\n";
  for (int x : arr) {
    cout << x << " ";
  }
  cout << "\n";
  for (int x : brr) {
    cout << x << " ";
  }
  cout << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  std::cout << std::fixed << std::setprecision(20);
  ;
  long long t = 1;
  while (t--) {
    solve();
  }
}
