#include <bits/stdc++.h>
using namespace std;
vector<string> split(const string &s, char c) {
  vector<string> v;
  stringstream ss(s);
  string x;
  while (getline(ss, x, c)) v.emplace_back(x);
  return move(v);
}
template <typename T, typename... Args>
inline string arrStr(T arr, int n) {
  stringstream s;
  s << "[";
  for (int i = 0; i < n - 1; i++) s << arr[i] << ",";
  s << arr[n - 1] << "]";
  return s.str();
}
inline void __evars_begin(int line) { cerr << "#" << line << ": "; }
template <typename T>
inline void __evars_out_var(vector<T> val) {
  cerr << arrStr(val, val.size());
}
template <typename T>
inline void __evars_out_var(T *val) {
  cerr << arrStr(val, 10);
}
template <typename T>
inline void __evars_out_var(T val) {
  cerr << val;
}
inline void __evars(vector<string>::iterator it) { cerr << endl; }
template <typename T, typename... Args>
inline void __evars(vector<string>::iterator it, T a, Args... args) {
  cerr << it->substr((*it)[0] == ' ', it->length()) << "=";
  __evars_out_var(a);
  cerr << "; ";
  __evars(++it, args...);
}
const long long nmax = 1000 * 1007;
struct edge {
  long long a, b, cost;
  edge(long long x, long long y, long long z) : a(x), b(y), cost(z){};
};
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
vector<long long> prime;
long long freq[10000005] = {0};
vector<long long> cnta(7e6, 0);
vector<long long> cntb(7e6, 0);
vector<pair<long long, long long> > resa, resb;
vector<long long> fina, finb;
void pre() {
  long long N = 1e7 + 5;
  for (long long i = 2; i < N; i++) {
    if (!freq[i]) {
      for (long long j = 2; i * j < N; j++) {
        freq[i * j] = i;
      }
      prime.push_back(i);
      freq[i] = i;
    }
  }
}
void work(vector<long long> &a, vector<long long> &cnta) {
  long long n = a.size();
  for (long long i = 0; i < (long long)(n); i++) {
    long long tmp = a[i];
    while (tmp > 1) {
      long long idx = lower_bound((prime).begin(), (prime).end(), freq[tmp]) -
                      prime.begin();
      tmp /= freq[tmp];
      cnta[idx]++;
    }
  }
}
void print(vector<long long> &a, vector<long long> &cnta) {
  long long n = a.size();
  for (long long i = 0; i < (long long)(n); i++) {
    long long tmp = a[i];
    long long ans = 1;
    while (tmp > 1) {
      long long idx = lower_bound((prime).begin(), (prime).end(), freq[tmp]) -
                      prime.begin();
      if (cnta[idx]) {
        cnta[idx]--;
        ans *= freq[tmp];
      }
      tmp /= freq[tmp];
    }
    cout << ans << " ";
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  pre();
  long long n, m;
  cin >> n >> m;
  vector<long long> a(n), b(m);
  for (long long i = 0; i < (long long)(n); i++) cin >> a[i];
  for (long long i = 0; i < (long long)(m); i++) cin >> b[i];
  work(a, cnta);
  work(b, cntb);
  long long ansa = 0, ansb = 0;
  for (long long i = 0; i < (long long)(cnta.size()); i++) {
    long long mini = min(cnta[i], cntb[i]);
    cnta[i] -= mini;
    cntb[i] -= mini;
  }
  cout << n << " " << m << endl;
  print(a, cnta);
  cout << "\n";
  print(b, cntb);
  return 0;
}
