#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
inline void in(int &x) { scanf("%d", &x); }
inline void in(int &x, int &y) { scanf("%d %d", &x, &y); }
inline void in(int &x, int &y, int &z) { scanf("%d %d", &x, &y, &z); }
inline void out(int x) { printf("%d ", x); }
inline void outln(int x) { printf("%d\n", x); }
inline void outln(int x, int y) { printf("%d %d\n", x, y); }
inline int _abs(int x) { return x > 0 ? x : -x; }
const int SIZE = (int)1e7 + 10;
int n, m;
int a[SIZE], b[SIZE], minp[SIZE];
int pa[SIZE], pb[SIZE];
void precalc() {
  for (int i = 2; i * i < SIZE; ++i) {
    if (!minp[i]) {
      for (int j = i * i; j < SIZE; j += i) {
        if (!minp[j]) minp[j] = i;
      }
    }
  }
  for (int i = 2; i < SIZE; ++i)
    if (!minp[i]) minp[i] = i;
}
void factor(int *mas, int val) {
  for (; val != 1; val /= minp[val]) {
    ++mas[minp[val]];
  }
}
void output(int *a, int *pa, int size) {
  for (int i = 0; i < (int)(size); i++) {
    int val = a[i], res = 1;
    for (; val != 1; val /= minp[val]) {
      if (pa[minp[val]])
        --pa[minp[val]];
      else
        res *= minp[val];
    }
    out(res);
  }
}
int main() {
  precalc();
  in(n, m);
  for (int i = 0; i < (int)(n); i++) {
    in(a[i]);
    factor(pa, a[i]);
  }
  for (int i = 0; i < (int)(m); i++) {
    in(b[i]);
    factor(pb, b[i]);
  }
  for (int i = 2; i < SIZE; ++i) {
    pa[i] = pb[i] = min(pa[i], pb[i]);
  }
  outln(n, m);
  output(a, pa, n);
  printf("\n");
  output(b, pb, m);
  return 0;
}
