#include <bits/stdc++.h>
int intcmp(const void *f, const void *s) { return (*(int *)f - *(int *)s); }
int gcd(int a, int b) { return ((b == 0) ? a : gcd(b, a % b)); }
int a[11234567];
int b[11234567];
int largestprimediv[11234567];
int hist[11234567];
void use(int n, int add) {
  while (largestprimediv[n]) {
    hist[largestprimediv[n]] += add;
    n /= largestprimediv[n];
  }
  hist[n] += add;
}
int calc(int n, int add) {
  int ans = 1, div;
  while (div = largestprimediv[n]) {
    if (hist[div] * add > 0) {
      hist[div] -= add;
      ans *= div;
    }
    n /= div;
  }
  div = n;
  if (hist[div] * add > 0) {
    hist[div] -= add;
    ans *= div;
  }
  return ans;
}
int main() {
  long long int i, j;
  int n, m;
  scanf("%d", &n);
  scanf("%d", &m);
  printf("%d %d\n", n, m);
  for (i = 0; i < n; ++i) scanf("%d", &a[i]);
  qsort(a, n, sizeof(int), intcmp);
  for (i = 0; i < m; ++i) scanf("%d", &b[i]);
  qsort(b, m, sizeof(int), intcmp);
  for (i = 2; i < 11234567; ++i)
    if (largestprimediv[i] == 0)
      for (j = i * i; j < 11234567; j += i) largestprimediv[j] = i;
  for (i = 0; i < n; ++i) use(a[i], 1);
  for (i = 0; i < m; ++i) use(b[i], -1);
  for (i = 0; i < n; ++i) printf("%d ", calc(a[i], 1));
  printf("\n");
  for (i = 0; i < m; ++i) printf("%d ", calc(b[i], -1));
  printf("\n");
  return 0;
}
