#include <bits/stdc++.h>
using namespace std;
int n;
int PRIMES_COUNTER = 0;
int up[10000001];
int down[10000001];
vector<int> up_result;
vector<int> down_result;
int nums[10000001];
int ups[100009];
int downs[100009];
int main() {
  for (int i = 2; i < 10000001; ++i) {
    if (nums[i]) continue;
    for (int j = i + i; j < 10000001; j += i) nums[j] = i;
  }
  int counter = 0;
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; ++i) {
    int a;
    scanf("%d", &a);
    ups[i] = a;
    if (a == 1) continue;
    while (nums[a] != 0) {
      up[nums[a]]++;
      a /= nums[a];
    }
    up[a]++;
  }
  for (int i = 0; i < m; ++i) {
    int a;
    scanf("%d", &a);
    downs[i] = a;
    if (a == 1) continue;
    while (nums[a] != 0) {
      down[nums[a]]++;
      a /= nums[a];
    }
    down[a]++;
  }
  for (int i = 0; i < 10000001; ++i) {
    int mn = min(up[i], down[i]);
    up[i] -= mn;
    down[i] -= mn;
  }
  printf("%d %d\n", n, m);
  for (int i = 0; i < n; ++i) {
    int a = ups[i];
    int to_print = 1;
    while (nums[a] != 0) {
      if (up[nums[a]] > 0) {
        to_print *= nums[a];
        up[nums[a]]--;
      }
      a /= nums[a];
    }
    if (up[a] > 0) {
      to_print *= a;
      up[a]--;
    }
    printf("%d ", to_print);
  }
  printf("\n");
  for (int i = 0; i < m; ++i) {
    int a = downs[i];
    int to_print = 1;
    while (nums[a] != 0) {
      if (down[nums[a]] > 0) {
        to_print *= nums[a];
        down[nums[a]]--;
      }
      a /= nums[a];
    }
    if (down[a] > 0) {
      to_print *= a;
      down[a]--;
    }
    printf("%d ", to_print);
  }
  printf("\n");
  return 0;
}
