#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e7 + 7;
const int maxnn = 1e5 + 7;
int prime[maxn];
int row[maxnn], col[maxnn];
int up[maxn], down[maxn];
void init() {
  for (int i = 2; i <= 10000000; i++) {
    if (!prime[i]) {
      prime[i] = i;
      for (int j = i + i; j <= 10000000; j += i) {
        prime[j] = i;
      }
    }
  }
}
int main() {
  int n, k;
  cin >> n >> k;
  init();
  for (int i = 1; i <= n; i++) scanf("%d", &row[i]);
  for (int i = 1; i <= k; i++) scanf("%d", &col[i]);
  for (int i = 1; i <= n; i++) {
    for (int j = row[i]; j > 1; j /= prime[j]) {
      up[prime[j]]++;
    }
  }
  for (int i = 1; i <= k; i++) {
    for (int j = col[i]; j > 1; j /= prime[j]) {
      down[prime[j]]++;
    }
  }
  printf("%d %d\n", n, k);
  for (int i = 1; i <= n; i++) {
    int tmp = 1;
    for (int j = row[i]; j > 1; j /= prime[j]) {
      if (down[prime[j]] > 0)
        down[prime[j]]--;
      else
        tmp *= prime[j];
    }
    printf("%d ", tmp);
  }
  puts("");
  for (int i = 1; i <= k; i++) {
    int tmp = 1;
    for (int j = col[i]; j > 1; j /= prime[j]) {
      if (up[prime[j]] > 0)
        up[prime[j]]--;
      else
        tmp *= prime[j];
    }
    printf("%d ", tmp);
  }
  return 0;
}
