#include <bits/stdc++.h>
using namespace std;
long n, m, a[100020], b[100020];
long fact_num[10000020];
long fact_den[10000020];
long p[10000020];
void factorization(long n, long a[], long fact[]) {
  for (long i = 0; i < n; ++i) {
    for (long j = a[i]; j > 1; j /= p[j]) fact[p[j]]++;
  }
}
void reduction(long n, long a[], long fact[]) {
  for (int i = 0; i < n; ++i) {
    long k = 1;
    for (long j = a[i]; j > 1; j /= p[j]) {
      if (fact[p[j]])
        fact[p[j]]--;
      else
        k *= p[j];
    }
    cout << k << (i < n - 1 ? ' ' : '\n');
  }
}
int main() {
  for (int i = 2; i < 10000020; ++i) {
    if (!p[i]) {
      for (int j = i; j < 10000020; j += i) p[j] = i;
    }
  }
  cin >> n >> m;
  for (long i = 0; i < n; ++i) cin >> a[i];
  for (long i = 0; i < m; ++i) cin >> b[i];
  cout << n << " " << m << "\n";
  factorization(n, a, fact_num);
  factorization(m, b, fact_den);
  reduction(n, a, fact_den);
  reduction(m, b, fact_num);
  return 0;
}
