//#define PROB "test"    //{{{ Preamble
#ifndef LOCAL_PC
#  define NDEBUG
#  define LOG(...) static_cast<void>(0)
#else
#  include <logging.hpp>
#endif
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
//#include <iostream>
#include <cstdio>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <valarray>
#include <vector>
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
#  include <forward_list>
#  include <unordered_map>
#  include <unordered_set>
#endif
#include <ext/numeric>
using namespace std; typedef long long ll; typedef unsigned long long ull;
const double pi=3.14159265358979323846; const double E=2.7182818284590452354;
//static struct _{ ios_base::Init i; _(){cin.sync_with_stdio(0);cin.tie(0);} } _;
//inline ostream& endl(ostream& os) {return os << '\n';}  using __gnu_cxx::power;
template<class T> inline T sq(const T& x) { return x*x; }
//template<class I> inline void in(I d, int n) { do cin >> *d++; while (--n); }
#if defined(PROB) && !defined(LOCAL_PC)
static struct __{ __(){cin.rdbuf(a.open(PROB".in",ios::in));cout.rdbuf(b.open(
PROB".out",ios::out));} ~__(){cin.rdbuf(0);cout.rdbuf(0);} filebuf a,b; } __;
#endif
#define pb(...)      push_back(__VA_ARGS__)
#define eb(...)      emplace_back(__VA_ARGS__)
#define sz(v)        static_cast<int>((v).size())
#define X            first
#define Y            second
#define FOREACH(i,c) for(__typeof__((c).begin())i=(c).begin();i!=(c).end();++i)
#define mp(x, y)     make_pair((x), (y))
//}}}

inline void write_int(int x)
{
    char buf[10], *p = buf;
    do
    {
        *p++ = '0' + x % 10;
        x /= 10;
    }
    while (x);
    do
    {
        putchar(*--p);
    }
    while (p > buf);
}

inline int read_int()
{
    char c;

    while (c = getchar(), c <= ' ');

    int res = c - '0';
    while (c = getchar(), c >= '0' && c <= '9')
    {
        res = res * 10 + (c - '0');
    }

    return res;
}

const int lim = 10000001;

struct {
    char p;
    short a[8];
} __attribute__((__packed__)) sieve[lim];

int num[100000], denom[100000];

unordered_map<int, stack<int *>> um;

template<bool insert>
inline void add(int x, int *p)
{
    int y = x;
    for (int i = 0; i < sieve[x].p; i++)
    {
        int q = sieve[x].a[i];
        do
        {
            if (insert)
            {
                um[q].push(p);
            }
            else if (!um[q].empty())
            {
                *um[q].top() /= q; um[q].pop();
                *p /= q;
            }
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
    {
        if (insert)
        {
            um[y].push(p);
        }
        else if (!um[y].empty())
        {
            *um[y].top() /= y; um[y].pop();
            *p /= y;
        }
    }
}

int main()
{
    for (int i = 2; i*i < lim; i++)
    {
        if (sieve[i].p == 0)
        {
            for (int j = i*2; j < lim; j += i)
            {
                sieve[j].a[sieve[j].p++] = i;
            }
        }
    }

    um.reserve(1000000);

    int n = read_int(), m = read_int();
    for (int i = 0; i < n; i++)
    {
        num[i] = read_int();
        add<true>(num[i], num+i);
    }
    for (int i = 0; i < m; i++)
    {
        denom[i] = read_int();
        add<false>(denom[i], denom+i);
    }

    write_int(n); putchar(' '); write_int(m); putchar('\n');
    write_int(num[0]);
    for (int i = 1; i < n; i++)
        putchar(' '), write_int(num[i]);
    putchar('\n');
    write_int(denom[0]);
    for (int i = 1; i < m; i++)
        putchar(' '), write_int(denom[i]);
    putchar('\n');
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define fi first
#define se second

const int maxN = (int)1.01e7;
int a[maxN];
int cnt[2][maxN];

void fact(int n, int d) {
    if (n == 1) {
        return;
    }
    cnt[d][a[n]]++;
    fact(n / a[n], d);
}

int upd(int n, int d) {
    if (n == 1) {
        return 1;
    }
    int v = 1;
    if (cnt[d][a[n]] > 0) {
        cnt[d][a[n]]--;
        v = a[n];
    }
    return v * upd(n / a[n], d);
}

int main() {
    for (int i = 1; i < maxN; i++) {
        a[i] = i;
    }
    for (int i = 2; i < maxN; i++) {
        if (a[i] == i) {
            for (int j = i * 2; j < maxN; j += i) {
                a[j] = i;
            }
        }
    }
    int n, m;
    scanf("%d%d", &n, &m);
    vector<int> a(n);
    vector<int> b(m);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        fact(a[i], 0);
    }
    for (int i = 0; i < m; i++) {
        scanf("%d", &b[i]);
        fact(b[i], 1);
    }
    printf("%d %d\n", n, m);
    for (int i = 0; i < maxN; i++) {
        cnt[0][i] = cnt[1][i] = min(cnt[0][i], cnt[1][i]);
    }
    for (int i = 0; i < n; i++) {
        a[i] /= upd(a[i], 0);
        printf("%d ", a[i]);
    }
    printf("\n");
    for (int i = 0; i < m; i++) {
        b[i] /= upd(b[i], 1);
        printf("%d ", b[i]);
    }
    printf("\n");
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define fi first
#define se second

const int maxN = 1 << 24;
int a[maxN];
int cnt[2][maxN];

void fact(int n, int d) {
    if (n == 1) {
        return;
    }
    cnt[d][a[n]]++;
    fact(n / a[n], d);
}

int upd(int n, int d) {
    if (n == 1) {
        return 1;
    }
    int v = 1;
    if (cnt[d][a[n]] > 0) {
        cnt[d][a[n]]--;
        v = a[n];
    }
    return v * upd(n / a[n], d);
}

int main() {
    for (int i = 1; i < maxN; i++) {
        a[i] = i;
    }
    for (int i = 2; i < maxN; i++) {
        if (a[i] == i) {
            for (int j = i * 2; j < maxN; j += i) {
                a[j] = i;
            }
        }
    }
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    vector<int> b(m);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        fact(a[i], 0);
    }
    for (int i = 0; i < m; i++) {
        cin >> b[i];
        fact(b[i], 1);
    }
    cout << n << " " << m << endl;
    for (int i = 0; i < maxN; i++) {
        cnt[0][i] = cnt[1][i] = min(cnt[0][i], cnt[1][i]);
    }
    for (int i = 0; i < n; i++) {
        a[i] /= upd(a[i], 0);
        cout << a[i] << " ";
    }
    cout << endl;
    for (int i = 0; i < m; i++) {
        b[i] /= upd(b[i], 1);
        cout << b[i] << " ";
    }
    cout << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define fi first
#define se second

const int maxN = 1 << 24;
int a[maxN];
int cnt[2][maxN];

void fact(int n, int d) {
    if (n == 1) {
        return;
    }
    cnt[d][a[n]]++;
    fact(n / a[n], d);
}

int upd(int n, int d) {
    if (n == 1) {
        return 1;
    }
    int v = 1;
    if (cnt[d][a[n]] > 0) {
        cnt[d][a[n]]--;
        v = a[n];
    }
    return v * upd(n / a[n], d);
}

int main() {
ios_base::sync_with_stdio(0);
    for (int i = 1; i < maxN; i++) {
        a[i] = i;
    }
    for (int i = 2; i < maxN; i++) {
        if (a[i] == i) {
            for (int j = i * 2; j < maxN; j += i) {
                a[j] = i;
            }
        }
    }
    int n, m;
    cin >> n >> m;
    vector<int> a(n);
    vector<int> b(m);
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        fact(a[i], 0);
    }
    for (int i = 0; i < m; i++) {
        cin >> b[i];
        fact(b[i], 1);
    }
    cout << n << " " << m << endl;
    for (int i = 0; i < maxN; i++) {
        cnt[0][i] = cnt[1][i] = min(cnt[0][i], cnt[1][i]);
    }
    for (int i = 0; i < n; i++) {
        a[i] /= upd(a[i], 0);
        cout << a[i] << " ";
    }
    cout << endl;
    for (int i = 0; i < m; i++) {
        b[i] /= upd(b[i], 1);
        cout << b[i] << " ";
    }
    cout << endl;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define fi first
#define se second

const int maxN = (int)1.01e7;
int a[maxN];
int cnt[2][maxN];

void fact(int n, int d) {
    if (n == 1) {
        return;
    }
    cnt[d][a[n]]++;
    fact(n / a[n], d);
}

int upd(int n, int d) {
    if (n == 1) {
        return 1;
    }
    int v = 1;
    if (cnt[d][a[n]] > 0) {
        cnt[d][a[n]]--;
        v = a[n];
    }
    return v * upd(n / a[n], d);
}

int main() {
    freopen("a.in", "r", stdin);
    freopen("a.out", "w", stdout);
    for (int i = 1; i < maxN; i++) {
        a[i] = i;
    }
    for (int i = 2; i < maxN; i++) {
        if (a[i] == i) {
            for (int j = i * 2; j < maxN; j += i) {
                a[j] = i;
            }
        }
    }
    int n, m;
    scanf("%d%d", &n, &m);
    vector<int> a(n);
    vector<int> b(m);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        fact(a[i], 0);
    }
    for (int i = 0; i < m; i++) {
        scanf("%d", &b[i]);
        fact(b[i], 1);
    }
    printf("%d %d\n", n, m);
    for (int i = 0; i < maxN; i++) {
        cnt[0][i] = cnt[1][i] = min(cnt[0][i], cnt[1][i]);
    }
    for (int i = 0; i < n; i++) {
        a[i] /= upd(a[i], 0);
        printf("%d ", a[i]);
    }
    printf("\n");
    for (int i = 0; i < m; i++) {
        b[i] /= upd(b[i], 1);
        printf("%d ", b[i]);
    }
    printf("\n");
    return 0;
}
#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
#define fi first
#define se second

const int maxN = (int)1.01e7;
int a[maxN];
int cnt[2][maxN];

void fact(int n, int d) {
    if (n == 1) {
        return;
    }
    cnt[d][a[n]]++;
    fact(n / a[n], d);
}

int upd(int n, int d) {
    if (n == 1) {
        return 1;
    }
    int v = 1;
    if (cnt[d][a[n]] > 0) {
        cnt[d][a[n]]--;
        v = a[n];
    }
    return v * upd(n / a[n], d);
}

int main() {
    for (int i = 1; i < maxN; i++) {
        a[i] = i;
    }
    for (int i = 2; i < maxN; i++) {
        if (a[i] == i) {
            for (int j = i * 2; j < maxN; j += i) {
                a[j] = i;
            }
        }
    }
    int n, m;
    scanf("%d%d", &n, &m);
    vector<int> a(n);
    vector<int> b(m);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
        fact(a[i], 0);
    }
    for (int i = 0; i < m; i++) {
        scanf("%d", &b[i]);
        fact(b[i], 1);
    }
    printf("%d %d\n", n, m);
    for (int i = 0; i < maxN; i++) {
        cnt[0][i] = cnt[1][i] = min(cnt[0][i], cnt[1][i]);
    }
    for (int i = 0; i < n; i++) {
        a[i] /= upd(a[i], 0);
        printf("%d ", a[i]);
    }
    printf("\n");
    for (int i = 0; i < m; i++) {
        b[i] /= upd(b[i], 1);
        printf("%d ", b[i]);
    }
    printf("\n");
    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define sz size()
#define For(i, st, en) for(int i=(st); i<=(int)(en); i++)
#define Ford(i, st, en) for(int i=(st); i>=(int)(en); i--)
#define forn(i, n) for(int i=0; i<(int)(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)
#define fori(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define in(x) int (x); input((x));
#define out(x) cout << (x);

typedef long double ld;
template <class _T> inline _T sqr(const _T& x) { return x * x; }
template <class _T> inline string tostr(const _T& a) { ostringstream os(""); os << a; return os.str(); }

// Constants
const ld PI = 3.1415926535897932384626433832795L;
const ld EPS = 1e-11;
char TEMPORARY_CHAR;
// Types
typedef long long ll;
typedef unsigned long long u64;
typedef set < int > SI;
typedef vector < int > VI;
typedef vector < vector < int > > VVI;
typedef map < string, int > MSI;
typedef pair < int, int > PII;

const ll INF = 1e17;

inline void input(int &a)
{
    a = 0;
    while (((TEMPORARY_CHAR = getchar()) > '9' || TEMPORARY_CHAR < '0') && (TEMPORARY_CHAR != '-'))
    {}
    char neg = 0;
    if (TEMPORARY_CHAR == '-')
    {
        neg = 1;
        TEMPORARY_CHAR = getchar();
    }

    while (TEMPORARY_CHAR <= '9' && TEMPORARY_CHAR >= '0')
    {
        a = (a << 3) + (a << 1) + TEMPORARY_CHAR - '0';
        TEMPORARY_CHAR = getchar();
    }

    if (neg)
        a = -a;
}
inline int nxt() {in(ret); return ret;}

#define x first
#define y second

int main()
{
    const int N = 10000000;
    int lp[N+1];
    int dp[N+1];
    clr(lp);
    clr(dp);
    vector<int> pr;

    for (int i=2; i<=N; ++i) {
        if (lp[i] == 0) {
            lp[i] = i;
        dp[i] = 1;
            pr.push_back (i);
        }
        for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
        {
        lp[i * pr[j]] = pr[j];
        dp[i * pr[j]] = i;
    }
    }

    in(n);in(m);
    int q[n], w[n];
    int a[N];
    int b[N];
    clr(b);
    clr(a);

    forn(i,n)
    {
        in(t);
        q[i] = t;
        while (t > 1)
        {
            a[lp[t]]++;
            t = dp[t];
        }
    }
    forn(i,m)
    {
        in(t);
        w[i] = t;
        while (t > 1)
        {
            b[lp[t]]++;
            t = dp[t];
        }
    }
    forn(i,N + 1)
    {
        int r = min(a[i],b[i]);
        a[i] = r;
        b[i] = r;
    }

    cout << n << " " << m << endl;
    forn(i,n)
    {
        int t = q[i];
        int ret = t;
        while (t > 1)
        {
            if (a[lp[t]])
            {
                ret /= lp[t];
                a[lp[t]]--;
            }
            t = dp[t];
        }
        cout << ret << " ";
    }
    cout << endl;
    forn(i,m)
    {
        int t = w[i];
        int ret = t;
        while (t > 1)
        {
            if (b[lp[t]])
            {
                ret /= lp[t];
                b[lp[t]]--;
            }
            t = dp[t];
        }
        cout << ret << " ";
    }
    cout << endl;

    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define sz size()
#define For(i, st, en) for(int i=(st); i<=(int)(en); i++)
#define Ford(i, st, en) for(int i=(st); i>=(int)(en); i--)
#define forn(i, n) for(int i=0; i<(int)(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)
#define fori(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define in(x) int (x); input((x));
#define out(x) cout << (x);

typedef long double ld;
template <class _T> inline _T sqr(const _T& x) { return x * x; }
template <class _T> inline string tostr(const _T& a) { ostringstream os(""); os << a; return os.str(); }

// Constants
const ld PI = 3.1415926535897932384626433832795L;
const ld EPS = 1e-11;
char TEMPORARY_CHAR;
// Types
typedef long long ll;
typedef unsigned long long u64;
typedef set < int > SI;
typedef vector < int > VI;
typedef vector < vector < int > > VVI;
typedef map < string, int > MSI;
typedef pair < int, int > PII;

const ll INF = 1e17;

inline void input(int &a)
{
    a = 0;
    while (((TEMPORARY_CHAR = getchar()) > '9' || TEMPORARY_CHAR < '0') && (TEMPORARY_CHAR != '-'))
    {}
    char neg = 0;
    if (TEMPORARY_CHAR == '-')
    {
        neg = 1;
        TEMPORARY_CHAR = getchar();
    }

    while (TEMPORARY_CHAR <= '9' && TEMPORARY_CHAR >= '0')
    {
        a = (a << 3) + (a << 1) + TEMPORARY_CHAR - '0';
        TEMPORARY_CHAR = getchar();
    }

    if (neg)
        a = -a;
}
inline int nxt() {in(ret); return ret;}

#define x first
#define y second

int main()
{
    const int N = 10000000;
    int lp[N+1];
    int dp[N+1];
    clr(lp);
    clr(dp);
    vector<int> pr;

    for (int i=2; i<=N; ++i) {
        if (lp[i] == 0) {
            lp[i] = i;
        dp[i] = 1;
            pr.push_back (i);
        }
        for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
        {
        lp[i * pr[j]] = pr[j];
        dp[i * pr[j]] = i;
    }
    }

    in(n);in(m);
    int q[n], w[n];
    int a[N + 1];
    int b[N + 1];
    clr(b);
    clr(a);

    forn(i,n)
    {
        in(t);
        q[i] = t;
        while (t > 1)
        {
            a[lp[t]]++;
            t = dp[t];
        }
    }
    forn(i,m)
    {
        in(t);
        w[i] = t;
        while (t > 1)
        {
            b[lp[t]]++;
            t = dp[t];
        }
    }
    forn(i,N + 1)
    {
        int r = min(a[i],b[i]);
        a[i] = r;
        b[i] = r;
    }

    cout << n << " " << m << endl;
    forn(i,n)
    {
        int t = q[i];
        int ret = t;
        while (t > 1)
        {
            if (a[lp[t]])
            {
                ret /= lp[t];
                a[lp[t]]--;
            }
            t = dp[t];
        }
        cout << ret << " ";
    }
    cout << endl;
    forn(i,m)
    {
        int t = w[i];
        int ret = t;
        while (t > 1)
        {
            if (b[lp[t]])
            {
                ret /= lp[t];
                b[lp[t]]--;
            }
            t = dp[t];
        }
        cout << ret << " ";
    }
    cout << endl;

    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define sz size()
#define For(i, st, en) for(int i=(st); i<=(int)(en); i++)
#define Ford(i, st, en) for(int i=(st); i>=(int)(en); i--)
#define forn(i, n) for(int i=0; i<(int)(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)
#define fori(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define in(x) int (x); input((x));
#define out(x) cout << (x);

typedef long double ld;
template <class _T> inline _T sqr(const _T& x) { return x * x; }
template <class _T> inline string tostr(const _T& a) { ostringstream os(""); os << a; return os.str(); }

// Constants
const ld PI = 3.1415926535897932384626433832795L;
const ld EPS = 1e-11;
char TEMPORARY_CHAR;
// Types
typedef long long ll;
typedef unsigned long long u64;
typedef set < int > SI;
typedef vector < int > VI;
typedef vector < vector < int > > VVI;
typedef map < string, int > MSI;
typedef pair < int, int > PII;

const ll INF = 1e17;

inline void input(int &a)
{
    a = 0;
    while (((TEMPORARY_CHAR = getchar()) > '9' || TEMPORARY_CHAR < '0') && (TEMPORARY_CHAR != '-'))
    {}
    char neg = 0;
    if (TEMPORARY_CHAR == '-')
    {
        neg = 1;
        TEMPORARY_CHAR = getchar();
    }

    while (TEMPORARY_CHAR <= '9' && TEMPORARY_CHAR >= '0')
    {
        a = (a << 3) + (a << 1) + TEMPORARY_CHAR - '0';
        TEMPORARY_CHAR = getchar();
    }

    if (neg)
        a = -a;
}
inline int nxt() {in(ret); return ret;}

#define x first
#define y second

int main()
{
    const ll N = 10000000;
    ll lp[N+1];
    ll dp[N+1];
    clr(lp);
    clr(dp);
    vector<ll> pr;

    for (int i=2; i<=N; ++i) {
        if (lp[i] == 0) {
            lp[i] = i;
        dp[i] = 1;
            pr.push_back (i);
        }
        for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
        {
        lp[i * pr[j]] = pr[j];
        dp[i * pr[j]] = i;
    }
    }

    in(n);in(m);
    int q[n], w[n];
    int a[N + 1];
    int b[N + 1];
    clr(b);
    clr(a);

    forn(i,n)
    {
        in(t);
        q[i] = t;
        while (t > 1)
        {
            a[lp[t]]++;
            t = dp[t];
        }
    }
    forn(i,m)
    {
        in(t);
        w[i] = t;
        while (t > 1)
        {
            b[lp[t]]++;
            t = dp[t];
        }
    }
    forn(i,N + 1)
    {
        int r = min(a[i],b[i]);
        a[i] = r;
        b[i] = r;
    }

    cout << n << " " << m << endl;
    forn(i,n)
    {
        int t = q[i];
        int ret = t;
        while (t > 1)
        {
            if (a[lp[t]])
            {
                ret /= lp[t];
                a[lp[t]]--;
            }
            t = dp[t];
        }
        cout << ret << " ";
    }
    cout << endl;
    forn(i,m)
    {
        int t = w[i];
        int ret = t;
        while (t > 1)
        {
            if (b[lp[t]])
            {
                ret /= lp[t];
                b[lp[t]]--;
            }
            t = dp[t];
        }
        cout << ret << " ";
    }
    cout << endl;

    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define sz size()
#define For(i, st, en) for(int i=(st); i<=(int)(en); i++)
#define Ford(i, st, en) for(int i=(st); i>=(int)(en); i--)
#define forn(i, n) for(int i=0; i<(int)(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)
#define fori(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define in(x) int (x); input((x));
#define out(x) cout << (x);

typedef long double ld;
template <class _T> inline _T sqr(const _T& x) { return x * x; }
template <class _T> inline string tostr(const _T& a) { ostringstream os(""); os << a; return os.str(); }

// Constants
const ld PI = 3.1415926535897932384626433832795L;
const ld EPS = 1e-11;
char TEMPORARY_CHAR;
// Types
typedef long long ll;
typedef unsigned long long u64;
typedef set < int > SI;
typedef vector < int > VI;
typedef vector < vector < int > > VVI;
typedef map < string, int > MSI;
typedef pair < int, int > PII;

const ll INF = 1e17;

inline void input(int &a)
{
    a = 0;
    while (((TEMPORARY_CHAR = getchar()) > '9' || TEMPORARY_CHAR < '0') && (TEMPORARY_CHAR != '-'))
    {}
    char neg = 0;
    if (TEMPORARY_CHAR == '-')
    {
        neg = 1;
        TEMPORARY_CHAR = getchar();
    }

    while (TEMPORARY_CHAR <= '9' && TEMPORARY_CHAR >= '0')
    {
        a = (a << 3) + (a << 1) + TEMPORARY_CHAR - '0';
        TEMPORARY_CHAR = getchar();
    }

    if (neg)
        a = -a;
}
inline int nxt() {in(ret); return ret;}

#define x first
#define y second

int main()
{
    const int N = 10000000;
    int lp[N+1];
    int dp[N+1];
    clr(lp);
    clr(dp);
    vector<int> pr;

    for (int i=2; i<=N; ++i) {
        if (lp[i] == 0) {
            lp[i] = i;
        dp[i] = 1;
            pr.push_back (i);
        }
        for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
        {
        lp[i * pr[j]] = pr[j];
        dp[i * pr[j]] = i;
    }
    }

    in(n);in(m);
    int q[n], w[m];
    int a[N + 1];
    int b[N + 1];
    clr(b);
    clr(a);

    forn(i,n)
    {
        in(t);
        q[i] = t;
        while (t > 1)
        {
            a[lp[t]]++;
            t = dp[t];
        }
    }
    forn(i,m)
    {
        in(t);
        w[i] = t;
        while (t > 1)
        {
            b[lp[t]]++;
            t = dp[t];
        }
    }
    forn(i,N + 1)
    {
        int r = min(a[i],b[i]);
        a[i] = r;
        b[i] = r;
    }

    cout << n << " " << m << endl;
    forn(i,n)
    {
        int t = q[i];
        int ret = t;
        while (t > 1)
        {
            if (a[lp[t]])
            {
                ret /= lp[t];
                a[lp[t]]--;
            }
            t = dp[t];
        }
        cout << ret << " ";
    }
    cout << endl;
    forn(i,m)
    {
        int t = w[i];
        int ret = t;
        while (t > 1)
        {
            if (b[lp[t]])
            {
                ret /= lp[t];
                b[lp[t]]--;
            }
            t = dp[t];
        }
        cout << ret << " ";
    }
    cout << endl;

    return 0;
}


#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define sz size()
#define For(i, st, en) for(int i=(st); i<=(int)(en); i++)
#define Ford(i, st, en) for(int i=(st); i>=(int)(en); i--)
#define forn(i, n) for(int i=0; i<(int)(n); i++)
#define ford(i, n) for(int i=(n)-1; i>=0; i--)
#define fori(it, x) for (__typeof((x).begin()) it = (x).begin(); it != (x).end(); it++)
#define in(x) int (x); input((x));
#define out(x) cout << (x);

typedef long double ld;
template <class _T> inline _T sqr(const _T& x) { return x * x; }
template <class _T> inline string tostr(const _T& a) { ostringstream os(""); os << a; return os.str(); }

// Constants
const ld PI = 3.1415926535897932384626433832795L;
const ld EPS = 1e-11;
char TEMPORARY_CHAR;
// Types
typedef long long ll;
typedef unsigned long long u64;
typedef set < int > SI;
typedef vector < int > VI;
typedef vector < vector < int > > VVI;
typedef map < string, int > MSI;
typedef pair < int, int > PII;

const ll INF = 1e17;

inline void input(int &a)
{
    a = 0;
    while (((TEMPORARY_CHAR = getchar()) > '9' || TEMPORARY_CHAR < '0') && (TEMPORARY_CHAR != '-'))
    {}
    char neg = 0;
    if (TEMPORARY_CHAR == '-')
    {
        neg = 1;
        TEMPORARY_CHAR = getchar();
    }

    while (TEMPORARY_CHAR <= '9' && TEMPORARY_CHAR >= '0')
    {
        a = (a << 3) + (a << 1) + TEMPORARY_CHAR - '0';
        TEMPORARY_CHAR = getchar();
    }

    if (neg)
        a = -a;
}
inline int nxt() {in(ret); return ret;}

#define x first
#define y second

int main()
{
    const ll N = 10000000;
    ll lp[N+1];
    ll dp[N+1];
    clr(lp);
    clr(dp);
    vector<ll> pr;

    for (ll i=2; i<=N; ++i) {
        if (lp[i] == 0) {
            lp[i] = i;
        dp[i] = 1;
            pr.push_back (i);
        }
        for (int j=0; j<(int)pr.size() && pr[j]<=lp[i] && i*pr[j]<=N; ++j)
        {
        lp[i * pr[j]] = pr[j];
        dp[i * pr[j]] = i;
    }
    }

    in(n);in(m);
    int q[n], w[n];
    int a[N + 1];
    int b[N + 1];
    clr(b);
    clr(a);

    forn(i,n)
    {
        in(t);
        q[i] = t;
        while (t > 1)
        {
            a[lp[t]]++;
            t = dp[t];
        }
    }
    forn(i,m)
    {
        in(t);
        w[i] = t;
        while (t > 1)
        {
            b[lp[t]]++;
            t = dp[t];
        }
    }
    forn(i,N + 1)
    {
        int r = min(a[i],b[i]);
        a[i] = r;
        b[i] = r;
    }

    cout << n << " " << m << endl;
    forn(i,n)
    {
        int t = q[i];
        int ret = t;
        while (t > 1)
        {
            if (a[lp[t]])
            {
                ret /= lp[t];
                a[lp[t]]--;
            }
            t = dp[t];
        }
        cout << ret << " ";
    }
    cout << endl;
    forn(i,m)
    {
        int t = w[i];
        int ret = t;
        while (t > 1)
        {
            if (b[lp[t]])
            {
                ret /= lp[t];
                b[lp[t]]--;
            }
            t = dp[t];
        }
        cout << ret << " ";
    }
    cout << endl;

    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <climits>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}
typedef int iii;
#define int ll

iii main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vll rn, rd;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    if (sz(rn) >= 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rn); i++) {
            unsigned long long a = rn[i];
            if (x * a < LLONG_MAX)
                x *= a;
            else
               aa.pb((ll)x), x = 1;
        }
        rn = aa;
    }
    if (sz(rd) >= 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rd); i++) {
            unsigned long long a = rd[i];
            if (x * a < LLONG_MAX)
                x *= a;
            else
                aa.pb((ll)x), x = 1;
        }
        rd = aa;
    }
    cout << sz(rn) << ' ' << sz(rd) << endl;
    assert(sz(rn) < 100001);
    assert(sz(rd) < 100001);
    for_each(it(rn), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    //eprintf("%lld == %lld\n", c / d, A / b);
    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}
typedef int iii;
#define int ll

iii main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("o.txt", "w", stdout);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vll rn, rd;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    if (sz(rn) > 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rn); i++) {
            unsigned long long a = rn[i];
            if (x * a < 10000000)
                x *= a;
            else
                aa.pb((ll)x), x = 1;
        }
        rn = aa;
    }
    if (sz(rd) > 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rd); i++) {
            unsigned long long a = rd[i];
            if (x * a < 10000000)
                x *= a;
            else
                aa.pb((ll)x), x = 1;
        }
        rd = aa;
    }
    cout << sz(rn) << ' ' << sz(rd) << endl;
    for_each(it(rn), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    //eprintf("%lld == %lld\n", c / d, A / b);
   // cout << LLONG_MAX << endl;
    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}
typedef int iii;
#define int ll

iii main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    //freopen("o.txt", "w", stdout);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    _set<int> nu, de;
    vll rn, rd;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        rn.pb(a);
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        rd.push_back(a);
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    printf("%lld %lld\n", n, m);
    for (int i = 0; i < sz(rn); i++) {
        int a = rn[i];
        int x = 1;
        while (a > 1) {
            if (cn[tek[a]])
                cn[tek[a]]--;
            else
                x *= tek[a];
            a /= tek[a];
        }
        printf("%lld ", x);
    }
    putchar('\n');
    for (int i = 0; i < sz(rd); i++) {
        int a = rd[i], x = 1;
        while (a > 1) {
            if (cnt[tek[a]])
                cnt[tek[a]]--;
            else
                x *= tek[a];
            a /= tek[a];
        }
        printf("%lld ", x);
    }
    putchar('\n');
    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}
typedef int iii;
#define int ll

iii main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    ll A = 1, b = 1;
    ll c = 1, d = 1;
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        A *= a;
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        b *= a;
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vll rn, rd;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            int x = 1;
            while (cn[a]--) {
                x *= a;
            }
            rn.pb(x);
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            int x = 1;
            while (cnt[a]--) {
                x *= a;
            }
            rn.pb(x);
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            int x = 1;
            while (cn[a]--) {
                x *= a;
            }
            rd.pb(a);
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            int x = 1;
            while (cnt[a]--) {
                x *= a;
            }
            rd.pb(a);
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    cout << sz(rn) << ' ' << sz(rd) << endl;
    assert(sz(rn) < 100001);
    assert(sz(rd) < 100001);
    for_each(it(rn), [&](int i) { c *= i, printf("%lld ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { d *= i, printf("%lld ", i); });
    putchar('\n');
    //eprintf("%lld == %lld\n", c / d, A / b);
    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}
#define int ll

void main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    ll A = 1, b = 1;
    ll c = 1, d = 1;
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        A *= a;
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        b *= a;
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vll rn, rd;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    cout << sz(rn) << ' ' << sz(rd) << endl;
    for_each(it(rn), [&](int i) { c *= i, printf("%lld ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { d *= i, printf("%lld ", i); });
    putchar('\n');
    //eprintf("%lld == %lld\n", c / d, A / b);
   // return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}
typedef int iii;
#define int ll

iii main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("o.txt", "w", stdout);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vll rn, rd;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    if (sz(rn) >= 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rn); i++) {
            unsigned long long a = rn[i];
            if (x * a < 10000000)
                x *= a;
            else
                aa.pb((ll)x), x = 1;
        }
        rn = aa;
    }
    if (sz(rd) >= 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rd); i++) {
            unsigned long long a = rd[i];
            if (x * a <= 10000000)
                x *= a;
            else
                aa.pb((ll)x), x = 1;
        }
        rd = aa;
    }
    cout << sz(rn) << ' ' << sz(rd) << endl;
    for_each(it(rn), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    //eprintf("%lld == %lld\n", c / d, A / b);
    cout << LLONG_MAX << endl;
    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <climits>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}
typedef int iii;
#define int ll

iii main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vll rn, rd;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    if (sz(rn) >= 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rn); i++) {
            unsigned long long a = rn[i];
            if (x * a < LLONG_MAX)
                x *= a;
            else
                x = 1, aa.pb((ll)x);
        }
        rn = aa;
    }
    if (sz(rd) >= 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rd); i++) {
            unsigned long long a = rd[i];
            if (x * a < LLONG_MAX)
                x *= a;
            else
                x = 1, aa.pb((ll)x);
        }
        rd = aa;
    }
    cout << sz(rn) << ' ' << sz(rd) << endl;
    for_each(it(rn), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    //eprintf("%lld == %lld\n", c / d, A / b);
    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}
typedef int iii;
#define int ll

iii main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("o.txt", "w", stdout);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vll rn, rd;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    if (sz(rn) >= 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rn); i++) {
            unsigned long long a = rn[i];
            if (x * a < 10000000)
                x *= a;
            else
                aa.pb((ll)x), x = 1;
        }
        rn = aa;
    }
    if (sz(rd) >= 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rd); i++) {
            unsigned long long a = rd[i];
            if (x * a <= 10000000)
                x *= a;
            else
                aa.pb((ll)x), x = 1;
        }
        rd = aa;
    }
    cout << sz(rn) << ' ' << sz(rd) << endl;
    for_each(it(rn), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    //eprintf("%lld == %lld\n", c / d, A / b);
   // cout << LLONG_MAX << endl;
    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}
typedef int iii;
#define int ll

iii main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("o.txt", "w", stdout);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vll rn, rd;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    if (sz(rn) > 100000) {
        vll aa;
        ll x = 1;
        for (int i = 0; i < sz(rn); i++) {
            ll a = rn[i];
            if (x * a <= 10000000 && x * a > 0)
                x *= a;
            else
                aa.pb(x), x = 1;
        }
        rn = aa;
    }
    if (sz(rd) > 100000) {
        vll aa;
        ll x = 1;
        for (int i = 0; i < sz(rd); i++) {
            ll a = rd[i];
            if (x * a <= 10000000 && x * a > 0)
                x *= a;
            else
                aa.pb(x), x = 1;
        }
        rd = aa;
    }
    cout << sz(rn) << ' ' << sz(rd) << endl;
    for_each(it(rn), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <climits>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}
typedef int iii;
#define int ll

iii main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vll rn, rd;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    if (sz(rn) >= 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rn); i++) {
            unsigned long long a = rn[i];
            if (x * a < LLONG_MAX)
                x *= a;
            else
                x = 1, aa.pb((ll)x);
        }
        rn = aa;
    }
    if (sz(rd) >= 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rd); i++) {
            unsigned long long a = rd[i];
            if (x * a < LLONG_MAX)
                x *= a;
            else
                x = 1, aa.pb((ll)x);
        }
        rd = aa;
    }
    cout << sz(rn) << ' ' << sz(rd) << endl;
    assert(sz(rn) < 100001);
    assert(sz(rd) < 100001);
    for_each(it(rn), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    //eprintf("%lld == %lld\n", c / d, A / b);
    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}
typedef int iii;
#define int ll

iii main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    ll A = 1, b = 1;
    ll c = 1, d = 1;
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        A *= a;
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        b *= a;
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vll rn, rd;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    assert(sz(rn) < 100001);
    assert(sz(rd) < 100001);
    cout << sz(rn) << ' ' << sz(rd) << endl;
    for_each(it(rn), [&](int i) { c *= i, printf("%lld ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { d *= i, printf("%lld ", i); });
    putchar('\n');
    //eprintf("%lld == %lld\n", c / d, A / b);
    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}
typedef int iii;
#define int ll

iii main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    ll A = 1, b = 1;
    ll c = 1, d = 1;
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        A *= a;
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        b *= a;
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vll rn, rd;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    cout << sz(rn) << ' ' << sz(rd) << endl;
    for_each(it(rn), [&](int i) { c *= i, printf("%lld ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { d *= i, printf("%lld ", i); });
    putchar('\n');
    //eprintf("%lld == %lld\n", c / d, A / b);
    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}
typedef int iii;
#define int ll

iii main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    ll A = 1, b = 1;
    ll c = 1, d = 1;
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        A *= a;
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        b *= a;
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vll rn, rd;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    if (sz(rn) >= 100000) {
        vll aa;
        int x = 1;
        for (int i = 0; i < sz(rn); i++) {
            int a = rn[i];
            if (x * a < 0)
                aa.pb(x), x = 1;
            x *= a;
        }
        rn = aa;
    }
    if (sz(rd) >= 100000) {
        vll aa;
        int x = 1;
        for (int i = 0; i < sz(rd); i++) {
            int a = rd[i];
            if (x * a < 0)
                aa.pb(x), x = 1;
            x *= a;
        }
        rd = aa;
    }
    cout << sz(rn) << ' ' << sz(rd) << endl;
    for_each(it(rn), [&](int i) { c *= i, printf("%lld ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { d *= i, printf("%lld ", i); });
    putchar('\n');
    //eprintf("%lld == %lld\n", c / d, A / b);
    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i * i; j < up; j += i) {
                assert(j > 0);
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}

int main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%d %d", &n, &m);
    _set <int> num, den;
    vi nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%d", &a);
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.pb(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%d", &a);
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.pb(tek[x]);
            x /= tek[x];
        }
    }
    vi rn, rd;
    _set <int> ok;
    for (int i = 0; i < sz(nu); i++) {
        int a = nu[i];
        if (ok.count(a))
            continue;
        ok.insert(a);
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
        } else {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
        }
    }
    for (int i = 0; i < sz(de); i++) {
        int a = de[i];
        if (ok.count(a))
            continue;
        ok.insert(a);
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
        } else {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    printf("%u %u\n", sz(rn), sz(rd));
    for_each(it(rn), [&](int i) {printf("%d ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) {printf("%d ", i); });
    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}
typedef int iii;
#define int ll

iii main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("o.txt", "w", stdout);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vll rn, rd;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    if (sz(rn) > 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rn); i++) {
            unsigned long long a = rn[i];
            if (x * a < LLONG_MAX)
                x *= a;
            else
                aa.pb((ll)x), x = 1;
        }
        rn = aa;
    }
    if (sz(rd) > 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rd); i++) {
            unsigned long long a = rd[i];
            if (x * a < LLONG_MAX)
                x *= a;
            else
                aa.pb((ll)x), x = 1;
        }
        rd = aa;
    }
    cout << sz(rn) << ' ' << sz(rd) << endl;
    for_each(it(rn), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    //eprintf("%lld == %lld\n", c / d, A / b);
    cout << LLONG_MAX << endl;
    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}
typedef int iii;
#define int ll

iii main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    ll A = 1, b = 1;
    ll c = 1, d = 1;
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        A *= a;
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        b *= a;
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vll rn, rd;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            int x = 1;
            while (cn[a]--) {
                x *= a;
            }
            rn.pb(x);
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            int x = 1;
            while (cnt[a]--) {
                x *= a;
            }
            rn.pb(x);
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            int x = 1;
            while (cn[a]--) {
                x *= a;
            }
            rd.pb(x);
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            int x = 1;
            while (cnt[a]--) {
                x *= a;
            }
            rd.pb(x);
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    cout << sz(rn) << ' ' << sz(rd) << endl;
    assert(sz(rn) < 100001);
    assert(sz(rd) < 100001);
    for_each(it(rn), [&](int i) { c *= i, printf("%lld ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { d *= i, printf("%lld ", i); });
    putchar('\n');
    //eprintf("%lld == %lld\n", c / d, A / b);
    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}
typedef int iii;
#define int ll

iii main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vll rn, rd;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    if (sz(rn) >= 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rn); i++) {
            unsigned long long a = rn[i];
            if (x * a < LLONG_MAX)
                x *= a;
            else
                x = 1, aa.pb((ll)x);
        }
        rn = aa;
    }
    if (sz(rd) >= 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rd); i++) {
            unsigned long long a = rd[i];
            if (x * a < LLONG_MAX)
                x *= a;
            else
                x = 1, aa.pb((ll)x);
        }
        rd = aa;
    }
    cout << sz(rn) << ' ' << sz(rd) << endl;
    for_each(it(rn), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    //eprintf("%lld == %lld\n", c / d, A / b);
    return 0;
}


#include <unordered_set>
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <unordered_map>

using namespace std;

#define eprintf(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back
#define mp make_pair
#define _map unordered_map
#define _set unordered_set
#define sz(x) ((int)(x).size())
#define it(x) (x).begin(), (x).end()

#define TIMESTAMP(x) eprintf("[" #x "] Time = %.3lfs\n", clock()*1.0/CLOCKS_PER_SEC)

#ifdef DEBUG
struct __timestamper {
    ~__timestamper() {
        TIMESTAMP(end);
    }
} __Timestamper;
#else
struct __timestamper {};
#endif

typedef long double ld;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;
typedef pair<ll, ll> pll;
typedef vector<pii> vpi;

int id = 0, primes[10000009], tek[10000009];
int cnt[10000009] = { 0 };
int cn[10000009] = { 0 };

void seive(int up = 10000009) {
    for (ll i = 2; i < up; i++) {
        if (!tek[i]) {
            tek[i] = i;
            for (ll j = i; j < up; j += i) {
                tek[j] = i;
            }
            primes[id++] = i;
        }
    }
}
typedef int iii;
#define int ll

iii main(void) {
#ifdef DEBUG
    freopen("in.txt", "r", stdin);
    freopen("o.txt", "w", stdout);
#endif // DEBUG
    seive();
    int n, m;
    scanf("%lld %lld", &n, &m);
    _set<int> nu, de;
    for (int i = 0; i < n; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cnt[tek[x]]++;
            nu.insert(tek[x]);
            x /= tek[x];
        }
    }
    for (int i = 0; i < m; i++) {
        int a;
        scanf("%lld", &a);
        int x = a;
        while (x > 1) {
            cn[tek[x]]++;
            de.insert(tek[x]);
            x /= tek[x];
        }
    }
    vll rn, rd;
    for (auto a : nu) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    for (auto a : de) {
        if (cn[a] == cnt[a]) {
            cn[a] = cnt[a] = 0;
        } else if (cn[a] > cnt[a]) {
            cn[a] -= cnt[a];
            cnt[a] = 0;
            while (cn[a]--) {
                rd.pb(a);
            }
            cn[a] = 0;
        } else if (cnt[a] > 0) {
            cnt[a] -= cn[a];
            cn[a] = 0;
            while (cnt[a]--) {
                rn.pb(a);
            }
            cnt[a] = 0;
        }
    }
    if (sz(rn) == 0)
        rn.pb(1);
    if (sz(rd) == 0)
        rd.pb(1);
    if (sz(rn) > 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rn); i++) {
            unsigned long long a = rn[i];
            if (x * a < LLONG_MAX)
                x *= a;
            else
                aa.pb((ll)x), x = 1;
        }
        rn = aa;
    }
    if (sz(rd) > 100000) {
        vll aa;
        unsigned long long x = 1;
        for (int i = 0; i < sz(rd); i++) {
            unsigned long long a = rd[i];
            if (x * a < LLONG_MAX)
                x *= a;
            else
                aa.pb((ll)x), x = 1;
        }
        rd = aa;
    }
    cout << sz(rn) << ' ' << sz(rd) << endl;
    for_each(it(rn), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    for_each(it(rd), [&](int i) { printf("%lld ", i); });
    putchar('\n');
    //eprintf("%lld == %lld\n", c / d, A / b);
   // cout << LLONG_MAX << endl;
    return 0;
}