#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 1e7 + 5;
const long long INF = 9e18;
const int M = 1e5 + 5;
map<long long, vector<int> > f;
long long p[N], ans1[M], a[M];
void solve() {
  for (int i = 2; i <= N; ++i) {
    if (i % 2 == 0) {
      p[i] = 2;
      continue;
    }
    if (p[i]) continue;
    p[i] = i;
    for (int j = i * 2; j <= N; j += i) {
      if (!p[j]) p[j] = i;
    }
  }
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    long long x;
    cin >> x;
    a[i] = x;
    while (x > 1) {
      long long y = p[x];
      while (x > 1 && x % y == 0) {
        f[y].push_back(i);
        x /= y;
      }
    }
  }
  for (int i = 1; i <= m; ++i) {
    ans1[i] = 1;
    long long x;
    cin >> x;
    while (x > 1) {
      long long y = p[x];
      while (x > 1 && x % y == 0) {
        if ((int)f[y].size()) {
          a[f[y].back()] /= y;
          f[y].pop_back();
        } else
          ans1[i] *= y;
        x /= y;
      }
    }
  }
  cout << n << " " << m << "\n";
  for (int i = 1; i <= n; ++i) {
    cout << a[i] << " ";
  }
  cout << "\n";
  for (int i = 1; i <= m; ++i) {
    cout << ans1[i] << " ";
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
