#include <bits/stdc++.h>
using namespace std;
const int N = int(1e7) + 1;
int pr[N];
vector<int> primes;
int nr[int(1e5) + 1];
int dr[int(1e5) + 1];
map<int, int> myMap;
int main() {
  ios::sync_with_stdio(false);
  for (int i = 2; i <= N; i++) {
    if (pr[i] == 0) {
      primes.push_back(i);
      pr[i] = i;
    }
    for (int j = 0;
         j < primes.size() && i * primes[j] <= N && pr[i] >= primes[j]; j++)
      pr[i * primes[j]] = primes[j];
  }
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int a;
    cin >> a;
    nr[i] = a;
    while (a != 1) {
      myMap[pr[a]]++;
      a /= pr[a];
    }
  }
  for (int i = 1; i <= m; i++) {
    int a;
    cin >> a;
    dr[i] = a;
    while (a != 1) {
      myMap[pr[a]]--;
      a /= pr[a];
    }
  }
  cout << n << ' ' << m << endl;
  for (int i = 1; i <= n; i++) {
    int answer = 1;
    int a = nr[i];
    while (a != 1) {
      if (myMap[pr[a]] > 0) {
        myMap[pr[a]]--;
        answer *= pr[a];
      }
      a /= pr[a];
    }
    cout << answer << ' ';
  }
  cout << endl;
  for (int i = 1; i <= m; i++) {
    int answer = 1;
    int a = dr[i];
    while (a != 1) {
      if (myMap[pr[a]] < 0) {
        myMap[pr[a]]++;
        answer *= pr[a];
      }
      a /= pr[a];
    }
    cout << answer << ' ';
  }
  return 0;
}
