#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:500000000")
using namespace std;
int d[10000009];
vector<int> V[10000009];
vector<int> ra, rb;
int A[100009], B[100009];
int main() {
  int i, j, k;
  for (i = 2; i * i <= 1e7; i++) {
    if (d[i] == 0) {
      d[i] = i;
      for (j = 2 * i; j <= 1e7; j += i)
        if (d[j] == 0) d[j] = i;
    }
  }
  for (i = 2; i <= 1e7; i++)
    if (d[i] == 0) d[i] = i;
  int n, m, a;
  cin >> n >> m;
  for (i = 0; i < n; i++) {
    scanf("%d", &a);
    A[i] = a;
    while (a != 1) {
      V[d[a]].push_back(i);
      a /= d[a];
    }
  }
  for (i = 0; i < m; i++) {
    scanf("%d", &a);
    int res = 1;
    while (a != 1) {
      if (V[d[a]].size()) {
        A[V[d[a]].back()] /= d[a];
        V[d[a]].pop_back();
      } else {
        res *= d[a];
      }
      a /= d[a];
    }
    B[i] = res;
  }
  printf("%d %d\n", n, m);
  for (i = 0; i < n; i++) printf("%d ", A[i]);
  puts("");
  for (i = 0; i < m; i++) printf("%d ", B[i]);
  puts("");
}
