#include <bits/stdc++.h>
using namespace std;
int a[10000010], b[10000010], n, m;
int nume[10000010], deno[10000010];
int no_prime[10000010];
void facto(int x, int v[]) {
  for (int i = 2; i * i <= x && no_prime[x]; i++)
    while (x % i == 0) v[i]++, x /= i;
  if (x > 1) v[x]++;
}
void get_number(int x, int v[]) {
  int f = 1;
  for (int i = 2; i * i <= x && no_prime[x]; i++) {
    while (x % i == 0) {
      if (v[i]) v[i]--, f *= i;
      x /= i;
    }
  }
  if (x > 1 && v[x]) v[x]--, f *= x;
  cout << f << " ";
}
int main() {
  for (int i = (int)2; i < (int)10000010; i++)
    if (!no_prime[i]) {
      for (int j = (int)i + i; j < (int)10000010; j += i) no_prime[j] = 1;
    }
  while (cin >> n >> m) {
    for (int i = (int)0; i < (int)n; i++) cin >> a[i], facto(a[i], nume);
    for (int i = (int)0; i < (int)m; i++) cin >> b[i], facto(b[i], deno);
    for (int i = (int)0; i < (int)10000010; i++) {
      int inf = min(deno[i], nume[i]);
      deno[i] -= inf, nume[i] -= inf;
    }
    cout << n << " " << m << endl;
    for (int i = (int)0; i < (int)n; i++) get_number(a[i], nume);
    puts("");
    for (int i = (int)0; i < (int)m; i++) get_number(b[i], deno);
    puts("");
  }
  return 0;
}
