#include <bits/stdc++.h>
using namespace std;
int main() {
  int MAX_SQRT = sqrt(10000000) + 1;
  vector<int> primes;
  for (int i = 2; i < MAX_SQRT; i++) {
    int j = 2;
    for (; j * j <= i; j++)
      if (i % j == 0) break;
    if (j * j > i) primes.push_back(i);
  }
  int n, m, a1[100001], b1[100001];
  vector<pair<int, int> > a[100001], b[100001];
  map<int, int> mp1, mp2, mp3;
  cin >> n >> m;
  int x, p;
  for (int i = 0; i < n; i++) {
    cin >> x;
    a1[i] = x;
    for (int j = 0; j < primes.size(); j++) {
      int d = 0;
      while (x % primes[j] == 0) x /= primes[j], d++;
      if (d > 0) {
        mp1[j] += d;
        a[i].push_back(make_pair(j, d));
      }
    }
    if (x > 1) {
      mp1[x]++;
      a[i].push_back(make_pair(x, 1));
    }
  }
  for (int i = 0; i < m; i++) {
    cin >> x;
    b1[i] = x;
    for (int j = 0; j < primes.size(); j++) {
      int d = 0;
      while (x % primes[j] == 0) x /= primes[j], d++;
      if (d > 0) {
        mp2[j] += d;
        b[i].push_back(make_pair(j, d));
      }
    }
    if (x > 1) {
      mp2[x]++;
      b[i].push_back(make_pair(x, 1));
    }
  }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < a[i].size(); j++) {
      int indx = a[i][j].first;
      int k = min(min(mp1[indx], mp2[indx]), a[i][j].second);
      mp1[indx] -= k;
      mp2[indx] -= k;
      mp3[indx] += k;
      int smp = indx;
      if (indx < 500) smp = primes[indx];
      for (int g = 0; g < k; g++) a1[i] /= smp;
    }
  for (int i = 0; i < m; i++)
    for (int j = 0; j < b[i].size(); j++) {
      int indx = b[i][j].first;
      int k = min(mp3[indx], b[i][j].second);
      mp3[indx] -= k;
      int smp = indx;
      if (indx < 500) smp = primes[indx];
      for (int g = 0; g < k; g++) b1[i] /= smp;
    }
  cout << n << " " << m << endl;
  for (int i = 0; i < n; i++) cout << a1[i] << " ";
  cout << endl;
  for (int i = 0; i < m; i++) cout << b1[i] << " ";
  cout << endl;
  return 0;
}
