#include <bits/stdc++.h>
using namespace std;
const int maxn = int(1.5 * 1e7);
const int inf = 1e9 + 7;
const long long ll_inf = 1e18 + 420;
const double eps = 1e-4;
const int N = 1e6 + 7;
const int MAX = 2e5 + 9;
const int mod = 1e9 + 7;
const long double pi = 3.14159265359;
long long prime[10000010], n, m, a[100010], b[100010], x, val, cnt[10000010],
    cur;
signed main() {
  for (long long i = 2; i <= 10000000; i++) {
    prime[i] = 1;
  }
  for (long long i = 2; i <= 10000000; i++) {
    if (prime[i] == 1) {
      prime[i] = i;
      for (long long j = i; i * j <= 10000000; j++) {
        prime[i * j] = i;
      }
    }
  }
  scanf("%lld %lld", &n, &m);
  for (long long i = 1; i <= n; i++) scanf("%lld", &a[i]);
  for (long long i = 1; i <= m; i++) scanf("%lld", &b[i]);
  for (long long i = 1; i <= n; i++) {
    x = a[i];
    while (x > 1) {
      val = prime[x];
      x /= val;
      cnt[val]++;
    }
  }
  for (long long i = 1; i <= m; i++) {
    x = b[i];
    while (x > 1) {
      val = prime[x];
      x /= val;
      cnt[val]--;
    }
  }
  printf("%lld %lld\n", n, m);
  for (long long i = 1; i <= n; i++) {
    x = a[i];
    cur = 1;
    while (x > 1) {
      val = prime[x];
      x /= val;
      if (cnt[val] > 0) {
        cur *= val;
        cnt[val]--;
      }
    }
    printf("%lld ", cur);
  }
  printf("\n");
  for (long long i = 1; i <= m; i++) {
    x = b[i];
    cur = 1;
    while (x > 1) {
      val = prime[x];
      x /= val;
      if (cnt[val] < 0) {
        cur *= val;
        cnt[val]++;
      }
    }
    printf("%lld ", cur);
  }
}
