#include <bits/stdc++.h>
using namespace std;
map<int, int> up;
map<int, int> down;
map<int, int> cnt;
map<int, int> now;
int a[100005];
int b[100005];
int n, m;
int prime[20010];
bool flag[20010];
int tot;
void findPrime() {
  tot = 0;
  for (int i = 2; i < 20000; i++)
    if (!flag[i]) {
      prime[++tot] = i;
      for (int j = 2; i * j < 20000; j++) flag[i * j] = true;
    }
}
int main() {
  findPrime();
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    int tmp = a[i];
    for (int j = 1; prime[j] * prime[j] <= tmp && tmp != 1; j++) {
      while (tmp % prime[j] == 0) {
        up[prime[j]]++;
        tmp /= prime[j];
      }
    }
    if (tmp != 1) up[tmp]++;
  }
  for (int i = 1; i <= m; i++) {
    scanf("%d", &b[i]);
    int tmp = b[i];
    for (int j = 1; prime[j] * prime[j] <= tmp && tmp != 1; j++) {
      while (tmp % prime[j] == 0) {
        down[prime[j]]++;
        tmp /= prime[j];
      }
    }
    if (tmp != 1) down[tmp]++;
  }
  map<int, int>::iterator it;
  for (it = up.begin(); it != up.end(); it++) {
    int d = down[it->first];
    if (d != 0) now[it->first] = min(it->second, d);
  }
  for (int i = 1; i <= n; i++) {
    int tmp = a[i];
    for (int j = 1; prime[j] * prime[j] <= tmp && tmp != 1; j++) {
      while (tmp % prime[j] == 0) {
        if (cnt[prime[j]] < now[prime[j]]) a[i] /= prime[j], cnt[prime[j]]++;
        tmp /= prime[j];
      }
    }
    if (tmp != 1 && cnt[tmp] < now[tmp]) a[i] /= tmp, cnt[tmp]++;
  }
  cnt.clear();
  for (int i = 1; i <= m; i++) {
    int tmp = b[i];
    for (int j = 1; prime[j] * prime[j] <= tmp && tmp != 1; j++) {
      while (tmp % prime[j] == 0) {
        if (cnt[prime[j]] < now[prime[j]]) b[i] /= prime[j], cnt[prime[j]]++;
        tmp /= prime[j];
      }
    }
    if (tmp != 1 && cnt[tmp] < now[tmp]) b[i] /= tmp, cnt[tmp]++;
  }
  printf("%d %d\n", n, m);
  for (int i = 1; i <= n; i++) printf("%d%c", a[i], (i == n) ? '\n' : ' ');
  for (int i = 1; i <= m; i++) printf("%d%c", b[i], (i == n) ? '\n' : ' ');
  return 0;
}
