#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
map<long long, vector<int>> num, dem;
long long n, m, x, a[N], b[N];
bool prime[10000007];
vector<long long> v, v2;
void sieve() {
  memset(prime, 1, sizeof prime);
  prime[0] = prime[1] = 0;
  for (int i = 2; i * i <= 1e7; i++) {
    if (prime[i])
      for (int j = i * i; j <= 1e7; j += i) prime[j] = 0;
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  sieve();
  cin >> n >> m;
  for (int j = 0; j < n; j++) {
    cin >> x;
    a[j] = x;
    for (int i = 2; i * i <= x; i++) {
      if (prime[x]) {
        num[x].push_back(j);
        x = 1;
        break;
      }
      while (x % i == 0) {
        num[i].push_back(j);
        x /= i;
      }
    }
    if (x > 1) num[x].push_back(j);
  }
  for (int j = 0; j < m; j++) {
    cin >> x;
    b[j] = x;
    for (int i = 2; i * i <= x; i++) {
      if (prime[x]) {
        dem[x].push_back(j);
        x = 1;
        break;
      }
      while (x % i == 0) {
        dem[i].push_back(j);
        x /= i;
      }
    }
    if (x > 1) dem[x].push_back(j);
  }
  for (auto i : num) {
    for (int j = 0; j < min(i.second.size(), dem[i.first].size()); j++) {
      a[num[i.first][j]] /= i.first;
      b[dem[i.first][j]] /= i.first;
    }
  }
  cout << n << " " << m << endl;
  for (int i = 0; i < n; i++) cout << a[i] << " ";
  cout << endl;
  for (int i = 0; i < m; i++) cout << b[i] << " ";
  return 0;
}
