#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 1e7 + 1;
char used[MAXN] = {0};
int lowest_divisor[MAXN];
int numbers[2][MAXN], numbers2[2][MAXN];
void sieve() {
  for (int i = 2; i < MAXN; i += 2) {
    used[i] = 1;
    lowest_divisor[i] = 2;
  }
  for (long long i = 3; i < MAXN; i += 2) {
    if (!used[i]) {
      used[i] = 1;
      lowest_divisor[i] = i;
      for (long long j = i; (j * i) < MAXN; j += 2) {
        if (!used[i * j]) {
          used[i * j] = 1;
          lowest_divisor[i * j] = i;
        }
      }
    }
  }
}
void factor(int n, bool fl) {
  while (n > 1) {
    numbers[fl][lowest_divisor[n]]++;
    n /= lowest_divisor[n];
  }
}
int main() {
  sieve();
  int n, m;
  scanf("%d%d", &n, &m);
  vector<int> a, b;
  a.resize(n);
  b.resize(m);
  for (int i = 0; i < n; i++) {
    int t;
    scanf("%d", &t);
    factor(t, 0);
    a[i] = t;
  }
  for (int i = 0; i < m; i++) {
    int t;
    scanf("%d", &t);
    factor(t, 1);
    b[i] = t;
  }
  for (int i = 2; i < MAXN; i++) {
    numbers2[0][i] = numbers[0][i] - min(numbers[0][i], numbers[1][i]);
    numbers2[1][i] = numbers[1][i] - min(numbers[0][i], numbers[1][i]);
  }
  cout << n << ' ' << m << endl;
  for (int i = 0; i < n; i++) {
    int t = a[i];
    while (t > 1) {
      if (numbers[0][lowest_divisor[t]] > numbers2[0][lowest_divisor[t]]) {
        numbers[0][lowest_divisor[t]]--;
        a[i] /= lowest_divisor[t];
      }
      t /= lowest_divisor[t];
    }
  }
  for (int i = 0; i < m; i++) {
    int t = b[i];
    while (t > 1) {
      if (numbers[1][lowest_divisor[t]] > numbers2[1][lowest_divisor[t]]) {
        numbers[1][lowest_divisor[t]]--;
        b[i] /= lowest_divisor[t];
      }
      t /= lowest_divisor[t];
    }
  }
  for (auto it : a) {
    printf("%d ", it);
  }
  cout << endl;
  for (auto it : b) {
    printf("%d ", it);
  }
  return 0;
}
