#include <bits/stdc++.h>
using namespace std;
vector<long long int> primes, vprimes;
int sp[10000005];
map<long long int, long long int> m1, m2;
vector<long long int> num, den, innum, inden;
void soe() {
  for (int i = 2; i < 10000005; i++)
    if (!sp[i]) {
      primes.push_back(i);
      for (int j = i; j < 10000005; j += i) sp[j] = i;
    }
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  soe();
  sp[1] = 1;
  long long int n, m, a, maxi = 0;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a;
    innum.push_back(a);
    maxi = max(maxi, a);
    while (sp[a] != 1) {
      m1[sp[a]]++;
      a /= sp[a];
    }
  }
  for (int i = 0; i < m; i++) {
    cin >> a;
    inden.push_back(a);
    maxi = max(maxi, a);
    while (sp[a] != 1) {
      m2[sp[a]]++;
      a /= sp[a];
    }
  }
  for (int i = 0; i < primes.size(); i++) {
    if (primes[i] > maxi) break;
    if (m1[primes[i]] || m2[primes[i]]) vprimes.push_back(primes[i]);
  }
  for (int i = 0; i < vprimes.size(); i++) {
    long long int g = m1[vprimes[i]];
    long long int s = m2[vprimes[i]];
    m1[vprimes[i]] -= min(g, s);
    m2[vprimes[i]] -= min(g, s);
  }
  for (int i = 0; i < n; i++) {
    map<long long int, long long int> q;
    set<long long int> p;
    long long int r = innum[i];
    while (sp[r] != 1) {
      q[sp[r]]++;
      p.insert(sp[r]);
      r /= sp[r];
    }
    long long int f = 1;
    for (auto it : p) {
      if (q[it] > m1[it]) {
        f = 0;
        break;
      }
    }
    if (f) {
      for (auto it : p) m1[it] -= q[it];
      num.push_back(innum[i]);
    }
    q.clear();
    p.clear();
  }
  for (int i = 0; i < m; i++) {
    map<long long int, long long int> q;
    set<long long int> p;
    long long int r = inden[i];
    while (sp[r] != 1) {
      q[sp[r]]++;
      p.insert(sp[r]);
      r /= sp[r];
    }
    long long int f = 1;
    for (auto it : p) {
      if (q[it] > m2[it]) {
        f = 0;
        break;
      }
    }
    if (f) {
      for (auto it : p) m2[it] -= q[it];
      den.push_back(inden[i]);
    }
    q.clear();
    p.clear();
  }
  for (int i = 0; i < vprimes.size(); i++) {
    if (vprimes[i] > maxi) break;
    long long int z = m1[vprimes[i]];
    long long int k = m2[vprimes[i]];
    long long int f = z, res = 1;
    while (f) {
      res = 1;
      while (f && res * vprimes[i] <= 10000000) {
        res *= vprimes[i];
        f--;
      }
      num.push_back(res);
    }
    f = k;
    res = 1;
    while (f) {
      res = 1;
      while (f && res * vprimes[i] <= 10000000) {
        res *= vprimes[i];
        f--;
      }
      den.push_back(res);
    }
  }
  if (!num.size()) num.push_back(1);
  if (!den.size()) den.push_back(1);
  cout << num.size() << " " << den.size() << endl;
  for (auto it : num) cout << it << " ";
  cout << endl;
  for (auto it : den) cout << it << " ";
  cout << endl;
  return 0;
}
