#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx,avx2,sse,sse2")
using namespace std;
long long fastexp(long long a, long long n, long long nod = LLONG_MAX) {
  long long ans = 1;
  while (n) {
    if (n & 1) ans = (ans * a) % nod;
    a = (a * a) % nod;
    n >>= 1;
  }
  return ans;
}
vector<bool> is_prime((int)1e7 + 1 + 5, 0);
void sieve(int n) {
  for (long long i = 2; i * i <= n; i++) {
    if (is_prime[i] == 0) {
      for (int j = i * i; j <= n; j += i) is_prime[j] = 1;
    }
  }
}
void fun() {
  sieve((int)1e7 + 1);
  int n, m;
  cin >> n >> m;
  vector<long long> factorsB((int)1e7 + 1 + 1, 0);
  int a[n], b[m];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < m; i++) {
    int k;
    cin >> k;
    b[i] = k;
    if (is_prime[k] == 0) {
      factorsB[k]++;
      continue;
    }
    for (int i = 2; i * i <= k; i++) {
      while (k % i == 0) {
        k /= i;
        factorsB[i]++;
      }
    }
    if (k > 1) {
      factorsB[k]++;
    }
  }
  cout << n << " " << m << "\n";
  for (int i = 0; i < n; i++) {
    int k = a[i];
    int num = 1;
    if (is_prime[k] == 0) {
      if (factorsB[k]) {
        cout << "1 ";
        factorsB[k]--;
      } else {
        cout << k << " ";
      }
      continue;
    }
    for (int i = 2; i * i <= k; i++) {
      while (k % i == 0) {
        k /= i;
        if (factorsB[i]) {
          factorsB[i]--;
        } else {
          num *= i;
        }
      }
    }
    if (k > 1) {
      if (factorsB[k]) {
        factorsB[k]--;
      } else {
        num *= k;
      }
    }
    cout << num << " ";
  }
  cout << "\n";
  for (int i = 0; i < m; i++) {
    int k = b[i];
    int num = 1;
    if (is_prime[k] == 0) {
      if (factorsB[k]) {
        cout << k << " ";
        factorsB[k]--;
      } else {
        cout << "1 ";
      }
      continue;
    }
    for (int i = 2; i * i <= k; i++) {
      while (k % i == 0) {
        k /= i;
        if (factorsB[i]) {
          num *= i;
          factorsB[i]--;
        }
      }
    }
    if (k > 1) {
      if (factorsB[k]) {
        num *= k;
        factorsB[k]--;
      }
    }
    cout << num << " ";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout.precision(20);
  cout << fixed;
  int t = 1;
  while (t--) {
    fun();
  }
}
