#include <bits/stdc++.h>
using namespace std;
int n, m;
int arr[200100];
map<int, vector<int> > inds;
vector<int> ps;
int isp[5000];
int main() {
  for (int i = (2), _i = (5000); i < _i; ++i) {
    if (!isp[i]) {
      ps.push_back(i);
      for (int j = i + i; j < 5000; j += i) isp[j] = true;
    }
  }
  cout << fixed << setprecision(12);
  cin >> n >> m;
  for (int i = 0, _i = (n + m); i < _i; ++i) {
    cin >> arr[i];
  }
  for (int i = 0, _i = (n + m); i < _i; ++i) {
    int t = arr[i];
    for (int j = 0; ps[j] * ps[j] <= t; j++) {
      while (t % ps[j] == 0) {
        inds[ps[j]].push_back(i);
        t /= ps[j];
      }
    }
    if (t != 1) {
      inds[t].push_back(i);
    }
  }
  for (__typeof((inds).begin()) it = (inds).begin(), _it = (inds).end();
       it != _it; ++it) {
    int div = it->first;
    vector<int> &v = it->second;
    int c = 0, c2 = 0;
    for (__typeof((v).begin()) i = (v).begin(), _i = (v).end(); i != _i; ++i) {
      int a = *i;
      if (a >= n) {
        c2++;
      } else {
        c++;
      }
    }
    int t = min(c, c2);
    c = c2 = 0;
    for (__typeof((v).begin()) i = (v).begin(), _i = (v).end(); i != _i; ++i) {
      int a = *i;
      if (a >= n) {
        c2++;
        if (c2 <= t) {
          arr[a] /= div;
        }
      } else {
        c++;
        if (c <= t) {
          arr[a] /= div;
        }
      }
    }
  }
  cout << n << ' ' << m << endl;
  for (int i = 0, _i = (n + m); i < _i; ++i) {
    if (i == n) cout << '\n';
    cout << arr[i] << ' ';
  }
}
