#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
const long long inf = 3e18 + 5;
int add(int a, int b) { return (a += b) < mod ? a : a - mod; }
int mul(int a, int b) { return 1LL * a * b % mod; }
const int ub = 1e7 + 5;
int mf[ub + 1];
vector<int> ind[ub + 1];
vector<int> primes;
void init() {
  for (int i = 1; i <= ub; i++) mf[i] = i;
  for (int i = 2; i <= ub; i += 2) mf[i] = 2;
  for (long long i = 3; i <= ub; i += 2) {
    if (mf[i] == i) {
      for (long long j = i * i; j <= ub; j += i)
        if (mf[j] == j) mf[j] = i;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  init();
  vector<int> A(n), B(m);
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    A[i] = x;
    while (x != 1) {
      ind[mf[x]].push_back(i);
      x /= mf[x];
    }
  }
  for (int i = 0; i < m; i++) {
    int x;
    cin >> x;
    B[i] = x;
    while (x != 1) {
      if (ind[mf[x]].size()) {
        A[ind[mf[x]].back()] /= mf[x];
        ind[mf[x]].pop_back();
        B[i] /= mf[x];
      }
      x /= mf[x];
    }
  }
  cout << n << " " << m << "\n";
  for (int x : A) cout << x << " ";
  cout << "\n";
  for (int x : B) cout << x << " ";
}
