#include <bits/stdc++.h>
using namespace std;
const char al[100] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
                      'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
                      'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
int main() {
  long long int n, m;
  cin >> n >> m;
  vector<long long int> nums(n + 1);
  vector<int> op(10000000 + 1, false);
  vector<int> prost;
  for (int i = 2; i <= 10000000; ++i) {
    if (op[i] == 0) {
      op[i] = i;
      prost.push_back(i);
    }
    for (int j = 0;
         j < (int)prost.size() && prost[j] <= op[i] && i * prost[j] <= 10000000;
         ++j)
      op[i * prost[j]] = prost[j];
  }
  map<long long int, long long int> my;
  long long int x;
  long long int j;
  long long int count = 0;
  for (long long int i = 0; i < n; i++) {
    cin >> x;
    nums[i] = x;
    j = 0;
    if (op[x] == x) {
      count++;
      my[x]++;
    } else {
      while (x > 1) {
        if (op[x] != x) {
          while (x % prost[j] != 0) j++;
          x /= prost[j];
          my[prost[j]]++;
          count++;
        } else {
          my[x]++;
          count++;
          x = 0;
        }
      }
    }
  }
  vector<long long int> second;
  for (long long int i = 0; i < m; i++) {
    cin >> x;
    j = 0;
    if (op[x] == x) {
      if (my.find(x) != my.end() && my[x] > 0) {
        my[x]--;
        count--;
      } else {
        if (second.size() != 0 && second[second.size() - 1] * x <= 10000000)
          second[second.size() - 1] *= x;
        else
          second.push_back(x);
      }
    } else {
      while (x > 1) {
        if (op[x] != x) {
          while (x % prost[j] != 0) j++;
          x /= prost[j];
          if (my.find(prost[j]) != my.end() && my[prost[j]] > 0) {
            my[prost[j]]--;
            count--;
          } else {
            if (second.size() != 0 &&
                second[second.size() - 1] * prost[j] <= 10000000)
              second[second.size() - 1] *= prost[j];
            else
              second.push_back(prost[j]);
          }
        } else {
          if (my.find(x) != my.end() && my[x] > 0) {
            my[x]--;
            count--;
          } else {
            if (second.size() != 0 && second[second.size() - 1] * x <= 10000000)
              second[second.size() - 1] *= x;
            else
              second.push_back(x);
          }
          x = 0;
        }
      }
    }
  }
  long long int prev = 1;
  long long int o;
  long long int value;
  vector<long long int> ans;
  vector<pair<int, int> > ssort;
  map<long long int, long long int> now;
  int pre = 1;
  for (int i = 0; i < n; i++) {
    pre = 1;
    x = nums[i];
    j = 0;
    while (x > 1) {
      if (op[x] == x) {
        now[x]++;
        if (now[x] <= my[x]) pre *= x;
        x = 1;
      } else {
        while (x % prost[j] != 0) j++;
        x /= prost[j];
        now[prost[j]]++;
        if (now[prost[j]] <= my[prost[j]]) pre *= prost[j];
      }
    }
    ans.push_back(pre);
  }
  if (second.size() == 0) second.push_back(1);
  cout << ans.size() << " " << second.size() << endl;
  for (long long int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
  cout << endl;
  for (long long int i = 0; i < second.size(); i++) cout << second[i] << " ";
}
