#include <bits/stdc++.h>
using namespace std;
long long prime[10000010], n, m, cnt[10000010], a[100010], b[100010];
signed main() {
  for (long long i = 2; i <= 10000001; i++) {
    if (prime[i]) continue;
    prime[i] = i;
    for (long long j = 1ll * i * i; j <= 10000001; j += i) {
      prime[j] = i;
    }
  }
  scanf("%lld %lld", &n, &m);
  for (long long i = 1; i <= n; i++) {
    scanf("%lld", &a[i]);
    long long k = a[i];
    while (k > 1) cnt[prime[k]]++, k /= prime[k];
  }
  for (long long i = 1; i <= m; i++) {
    scanf("%lld", &b[i]);
    long long k = b[i];
    while (k > 1) cnt[prime[k]]--, k /= prime[k];
  }
  printf("%lld %lld\n", n, m);
  for (long long i = 1; i <= n; i++) {
    long long k = a[i];
    while (k > 1) {
      long long val = prime[k], cur = 0;
      while (k % val == 0) cur++, k /= val;
      while (cnt[val] < cur && cur > 0) a[i] /= val, cur--;
      cnt[val] -= cur;
    }
    printf("%lld ", a[i]);
  }
  printf("\n");
  for (long long i = 1; i <= m; i++) {
    long long k = b[i];
    while (k > 1) {
      long long val = prime[k], cur = 0;
      while (k % val == 0) cur--, k /= val;
      while (cnt[val] > cur && cur < 0) b[i] /= val, cur++;
      cnt[val] -= cur;
    }
    printf("%lld ", b[i]);
  }
}
