#include <bits/stdc++.h>
using namespace std;
int p[10000005];
int Fa[10000005], Fb[10000005];
int a[100005];
int b[100005];
void criba() {
  for (int i = 2; i * i < 10000005; i++) {
    if (!p[i])
      for (int j = i * i; j < 10000005; j += i) p[j] = i;
  }
  for (int i = 2; i < 10000005; i++)
    if (!p[i]) p[i] = i;
}
void fact(int a[], int *F, int sz) {
  for (int i = 0; i < sz; i++) {
    int k = a[i];
    while (k > 1) {
      F[p[k]]++;
      k /= p[k];
    }
  }
}
void solve(int a[], int *F, int sz) {
  for (int i = 0; i < sz; i++) {
    int res = 1;
    int k = a[i];
    while (k > 1) {
      if (!F[p[k]])
        res *= p[k];
      else
        F[p[k]]--;
      k /= p[k];
    }
    if (i) putchar(' ');
    printf("%d", res);
  }
  putchar(10);
}
int main() {
  int n, m;
  criba();
  cin >> n >> m;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < m; i++) cin >> b[i];
  cout << n << " " << m << endl;
  fact(a, Fa, n);
  fact(b, Fb, m);
  solve(a, Fb, n);
  solve(b, Fa, m);
  return 0;
}
