//#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 <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))
//}}}

const int lim = 10000001;

struct {
    short p;
    short a[8];
} sieve[lim];

int arr[lim];

template<typename P>
inline void add(int x)
{
    int y = x;
    static P p;
    for (int i = 0; i < sieve[x].p; i++)
    {
        int q = sieve[x].a[i];
        do
        {
            arr[q] = p(arr[q], 1);
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
    {
        arr[y] = p(arr[y], 1);
    }
}

template<typename G, typename S>
inline vector<int> compress()
{
    vector<int> res;

    res.reserve(100000);

    int l = 2, r = lim-1;
    static G g; static S s;
    while (l <= r)
    {
        if (!g(arr[r], 0))
        {
            r--;
            continue;
        }

        ll a = r; arr[r] = s(arr[r], 1);
        while (g(arr[r], 0) && a * r <= 10000000)
        {
            a *= r;
            arr[r] = s(arr[r], 1);
        }
        while (g(arr[l], 0) && a * l <= 10000000)
        {
            a *= l;
            arr[l] = s(arr[l], 1);
        }
        res.pb(a);

        if (!g(arr[l], 0))
            l++;
    }
    if (res.empty())
        res.pb(1);

    return res;
}

#if 0
inline void compress(const vector<int>& data, vector<int>& res)
{
    int i = 0, j = sz(data)-1;

    res.reserve(100000);

    while (i <= j)
    {
        ll a = data[j];
        while (i < j && a*data[i] <= 10000000)
        {
            a *= data[i++];
        }
        res.pb(a);
        j--;
    }
}
#endif

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;
            }
        }
    }

    int n, m; cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        int a; cin >> a;
        add<plus<int>>(a);
    }
    for (int i = 0; i < m; i++)
    {
        int b; cin >> b;
        add<minus<int>>(b);
    }

    vector<int> num(compress<greater<int>, minus<int>>());
    vector<int> denom(compress<less<int>, plus<int>>());

#if 0
    vector<int> num_all, denom_all;
    num_all.reserve(2500000); denom_all.reserve(2500000);

    for (int i : touched)
    {
        while (arr[i] > 0)
        {
            num_all.pb(i);
            arr[i]--;
        }
        while (arr[i] < 0)
        {
            denom_all.pb(i);
            arr[i]++;
        }
    }
    if (num_all.empty())
        num_all.pb(1);
    if (denom_all.empty())
        denom_all.pb(1);

    vector<int> num, denom;
    compress(num_all, num);
    compress(denom_all, denom);
#endif

#if 0
    vector<int> num, denom;
    ll a = 1, b = 1;
    for (int i = 2; i < lim; !arr[i] ? i++ : 0)
    {
        if (arr[i] > 0)
        {
            if (a * i > 10000000)
            {
                num.pb(a);
                a = i;
            }
            else
            {
                a *= i;
            }
            arr[i]--;
        }
        else if (arr[i] < 0)
        {
            if (b * i > 10000000)
            {
                denom.pb(b);
                b = i;
            }
            else
            {
                b *= i;
            }
            arr[i]++;
        }
    }
    num.pb(a);
    denom.pb(b);
#endif

    cout << sz(num) << ' ' << sz(denom) << endl;
    cout << num[0];
    for (int i = 1; i < sz(num); i++)
        cout << ' '  << num[i];
    cout << endl;
    cout << denom[0];
    for (int i = 1; i < sz(denom); i++)
        cout << ' ' << denom[i];
    cout << endl;
}


//#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(2000000);

    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');
}


//#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 <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))
//}}}

const int lim = 10000001;

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

int num[100000], denom[100000];

struct mystruct {
    stack<int *> n, d;
};

unordered_map<int, mystruct> um;

template<stack<int *> mystruct::* A, stack<int *> mystruct::* B>
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 (!(um[q].*B).empty())
            {
                *((um[q].*B).top()) /= q; (um[q].*B).pop();
                *p /= q;
            }
            else
            {
                (um[q].*A).push(p);
            }
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
    {
        if (!(um[y].*B).empty())
        {
            *((um[y].*B).top()) /= y; (um[y].*B).pop();
            *p /= y;
        }
        else
        {
            (um[y].*A).push(p);
        }
    }
}

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(2000000);

    int n, m; cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> num[i];
        add<&mystruct::n, &mystruct::d>(num[i], num+i);
    }
    for (int i = 0; i < m; i++)
    {
        cin >> denom[i];
        add<&mystruct::d, &mystruct::n>(denom[i], denom+i);
    }

    cout << n << ' ' << m << endl;
    cout << num[0];
    for (int i = 1; i < n; i++)
        cout << ' '  << num[i];
    cout << endl;
    cout << denom[0];
    for (int i = 1; i < m; i++)
        cout << ' ' << denom[i];
    cout << endl;
}


//#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(100000);

    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');
}


//#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 <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))
//}}}

const int lim = 10000001;

struct {
    short p;
    short a[8];
} sieve[lim];

int num[100000], denom[100000];

struct mystruct {
    vector<int *> n, d;
} arr[lim];

template<vector<int *> mystruct::* V>
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
        {
            (arr[q].*V).eb(p);
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
        (arr[y].*V).eb(p);
}

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;
            }
        }
    }

    int n, m; cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> num[i];
        add<&mystruct::n>(num[i], num+i);
    }
    for (int i = 0; i < m; i++)
    {
        cin >> denom[i];
        add<&mystruct::d>(denom[i], denom+i);
    }

    for (int i = 2; i < lim; i++)
    {
        for (int j = 0; j < min(sz(arr[i].n), sz(arr[i].d)); j++)
        {
            *arr[i].n[j] /= i;
            *arr[i].d[j] /= i;
        }
    }

    cout << n << ' ' << m << endl;
    cout << num[0];
    for (int i = 1; i < n; i++)
        cout << ' '  << num[i];
    cout << endl;
    cout << denom[0];
    for (int i = 1; i < m; i++)
        cout << ' ' << denom[i];
    cout << endl;
}


//#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];

struct mystruct {
    stack<int *> n, d;
};

unordered_map<int, mystruct> um;

template<stack<int *> mystruct::* A, stack<int *> mystruct::* B>
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 (!(um[q].*B).empty())
            {
                *((um[q].*B).top()) /= q; (um[q].*B).pop();
                *p /= q;
            }
            else
            {
                (um[q].*A).push(p);
            }
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
    {
        if (!(um[y].*B).empty())
        {
            *((um[y].*B).top()) /= y; (um[y].*B).pop();
            *p /= y;
        }
        else
        {
            (um[y].*A).push(p);
        }
    }
}

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(4000000);

    int n = read_int(), m = read_int();
    for (int i = 0; i < n; i++)
    {
        num[i] = read_int();
        add<&mystruct::n, &mystruct::d>(num[i], num+i);
    }
    for (int i = 0; i < m; i++)
    {
        denom[i] = read_int();
        add<&mystruct::d, &mystruct::n>(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');
}


//#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(1050000);

    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');
}


//#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(2500000);

    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');
}


//#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 <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))
//}}}

const int lim = 10000001;

struct {
    char p;
    short a[8];
} sieve[lim];

int arr[lim];

template<typename O>
inline void add(int x)
{
    int y = x;
    for (int i = 0; i < sieve[x].p; i++)
    {
        int q = sieve[x].a[i];
        do
        {
            arr[q] = O()(arr[q], 1);
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
    {
        arr[y] = O()(arr[y], 1);
    }
}

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;
            }
        }
    }

    int n, m; cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        int a; cin >> a;
        add<plus<int>>(a);
    }
    for (int i = 0; i < m; i++)
    {
        int b; cin >> b;
        add<minus<int>>(b);
    }

    vector<int> num, denom;
    ll a = 1, b = 1;
    for (int i = 2; i < lim; !arr[i] ? i++ : 0)
    {
        if (arr[i] > 0)
        {
            if (a * i > 10000000)
            {
                num.pb(a);
                a = i;
            }
            else
            {
                a *= i;
            }
            arr[i]--;
        }
        else if (arr[i] < 0)
        {
            if (b * i > 10000000)
            {
                denom.pb(b);
                b = i;
            }
            else
            {
                b *= i;
            }
            arr[i]++;
        }
    }
    num.pb(a);
    denom.pb(b);

    cout << sz(num) << " " << sz(denom) << endl;
    cout << num[0];
    for (int i = 1; i < sz(num); i++)
        cout << " " << num[i];
    cout << endl;
    cout << denom[0];
    for (int i = 1; i < sz(denom); i++)
        cout << " " << denom[i];
    cout << endl;
}


//#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(1900000);

    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');
}


//#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 <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))
//}}}

const int lim = 10000001;

struct {
    short p;
    short a[8];
} sieve[lim];

int arr[lim];

unordered_set<int> touched;

template<typename O>
inline void add(int x)
{
    int y = x;
    for (int i = 0; i < sieve[x].p; i++)
    {
        int q = sieve[x].a[i];
        touched.insert(q);
        do
        {
            arr[q] = O()(arr[q], 1);
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
    {
        touched.insert(y);
        arr[y] = O()(arr[y], 1);
    }
}

inline void compress(const vector<int>& data, vector<int>& res)
{
    int i = 0, j = sz(data)-1;

    res.reserve(100000);

    while (i <= j)
    {
        ll a = data[j];
        while (i < j && a*data[i] <= 10000000)
        {
            a *= data[i++];
        }
        res.pb(a);
        j--;
    }
}

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;
            }
        }
    }

    int n, m; cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        int a; cin >> a;
        add<plus<int>>(a);
    }
    for (int i = 0; i < m; i++)
    {
        int b; cin >> b;
        add<minus<int>>(b);
    }

    vector<int> num_all, denom_all;
    num_all.reserve(2500000); denom_all.reserve(2500000);

    for (int i : touched)
    {
        while (arr[i] > 0)
        {
            num_all.pb(i);
            arr[i]--;
        }
        while (arr[i] < 0)
        {
            denom_all.pb(i);
            arr[i]++;
        }
    }
    if (num_all.empty())
        num_all.pb(1);
    if (denom_all.empty())
        denom_all.pb(1);

    vector<int> num, denom;
    compress(num_all, num);
    compress(denom_all, denom);

#if 0
    vector<int> num, denom;
    ll a = 1, b = 1;
    for (int i = 2; i < lim; !arr[i] ? i++ : 0)
    {
        if (arr[i] > 0)
        {
            if (a * i > 10000000)
            {
                num.pb(a);
                a = i;
            }
            else
            {
                a *= i;
            }
            arr[i]--;
        }
        else if (arr[i] < 0)
        {
            if (b * i > 10000000)
            {
                denom.pb(b);
                b = i;
            }
            else
            {
                b *= i;
            }
            arr[i]++;
        }
    }
    num.pb(a);
    denom.pb(b);
#endif

    cout << sz(num) << " " << sz(denom) << endl;
    cout << num[0];
    for (int i = 1; i < sz(num); i++)
        cout << " " << num[i];
    cout << endl;
    cout << denom[0];
    for (int i = 1; i < sz(denom); i++)
        cout << " " << denom[i];
    cout << endl;
}


//#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(2200000);

    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');
}


//#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 {
    short p;
    short a[8];
} sieve[lim];

int num[100000], denom[100000];

unordered_map<int, stack<int *, vector<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(100000);

    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');
}


//#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(3000000);

    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');
}


//#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];

struct mystruct {
    stack<int *> n, d;
};

map<int, mystruct> um;

template<stack<int *> mystruct::* A, stack<int *> mystruct::* B>
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 (!(um[q].*B).empty())
            {
                *((um[q].*B).top()) /= q; (um[q].*B).pop();
                *p /= q;
            }
            else
            {
                (um[q].*A).push(p);
            }
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
    {
        if (!(um[y].*B).empty())
        {
            *((um[y].*B).top()) /= y; (um[y].*B).pop();
            *p /= y;
        }
        else
        {
            (um[y].*A).push(p);
        }
    }
}

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(4000000);

    int n = read_int(), m = read_int();
    for (int i = 0; i < n; i++)
    {
        num[i] = read_int();
        add<&mystruct::n, &mystruct::d>(num[i], num+i);
    }
    for (int i = 0; i < m; i++)
    {
        denom[i] = read_int();
        add<&mystruct::d, &mystruct::n>(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');
}


//#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];

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(4000000);

    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');
}


//#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 <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))
//}}}

const int lim = 10000001;

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

int num[100000], denom[100000];

struct mystruct {
    stack<int *> n, d;
};

unordered_map<int, mystruct> um;

template<stack<int *> mystruct::* A, stack<int *> mystruct::* B>
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 (!(um[q].*B).empty())
            {
                *((um[q].*B).top()) /= q; (um[q].*B).pop();
                *p /= q;
            }
            else
            {
                (um[q].*A).push(p);
            }
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
    {
        if (!(um[y].*B).empty())
        {
            *((um[y].*B).top()) /= y; (um[y].*B).pop();
            *p /= y;
        }
        else
        {
            (um[y].*A).push(p);
        }
    }
}

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(2000000);

    int n, m; cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> num[i];
        add<&mystruct::n, &mystruct::d>(num[i], num+i);
    }
    for (int i = 0; i < m; i++)
    {
        cin >> denom[i];
        add<&mystruct::d, &mystruct::n>(denom[i], denom+i);
    }

    cout << n << ' ' << m << endl;
    cout << num[0];
    for (int i = 1; i < n; i++)
        cout << ' '  << num[i];
    cout << endl;
    cout << denom[0];
    for (int i = 1; i < m; i++)
        cout << ' ' << denom[i];
    cout << endl;
}


//#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 <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))
//}}}

const int lim = 10000001;

struct {
    short p;
    short a[8];
} sieve[lim];

int arr[lim];

set<int> touched;

template<typename P>
inline void add(int x)
{
    int y = x;
    static P p;
    for (int i = 0; i < sieve[x].p; i++)
    {
        int q = sieve[x].a[i];
        do
        {
            touched.insert(q);
            arr[q] = p(arr[q], 1);
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
    {
        touched.insert(y);
        arr[y] = p(arr[y], 1);
    }
}

vector<int> z;

#if 1
template<typename G, typename S>
inline vector<int> compress()
{
    vector<int> res;

    res.reserve(100000);

    auto l = &z[0], r = &z[sz(z)-1];
    static G g; static S s;
    while (l <= r)
    {
        if (!g(arr[*r], 0))
        {
            r--;
            continue;
        }

        ll a = *r; arr[*r] = s(arr[*r], 1);
        while (g(arr[*r], 0) && a * *r <= 10000000)
        {
            a *= *r;
            arr[*r] = s(arr[*r], 1);
        }
        while (g(arr[*l], 0) && a * *l <= 10000000)
        {
            a *= *l;
            arr[*l] = s(arr[*l], 1);
        }
        res.pb(a);

        if (!g(arr[*l], 0))
            l++;
    }
    if (res.empty())
        res.pb(1);

    return res;
}
#endif

#if 0
inline void compress(const vector<int>& data, vector<int>& res)
{
    int i = 0, j = sz(data)-1;

    res.reserve(100000);

    while (i <= j)
    {
        ll a = data[j]; j--;
        while (i < j && a*data[j] <= 10000000)
        {
            a *= data[j--];
        }
        while (i <= j && a*data[i] <= 10000000)
        {
            a *= data[i++];
        }
        res.pb(a);
    }
}
#endif

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;
            }
        }
    }

    int n, m; cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        int a; cin >> a;
        add<plus<int>>(a);
    }
    for (int i = 0; i < m; i++)
    {
        int b; cin >> b;
        add<minus<int>>(b);
    }

#if 1
    touched.insert(2);
    z = vector<int>(touched.begin(), touched.end());
    vector<int> num(compress<greater<int>, minus<int>>());
    vector<int> denom(compress<less<int>, plus<int>>());
#endif

#if 0
    vector<int> num_all, denom_all;
    num_all.reserve(2500000); denom_all.reserve(2500000);

    for (int i : touched)
    {
        while (arr[i] > 0)
        {
            num_all.pb(i);
            arr[i]--;
        }
        while (arr[i] < 0)
        {
            denom_all.pb(i);
            arr[i]++;
        }
    }
    if (num_all.empty())
        num_all.pb(1);
    if (denom_all.empty())
        denom_all.pb(1);

    vector<int> num, denom;
    compress(num_all, num);
    compress(denom_all, denom);
#endif

#if 0
    vector<int> num, denom;
    ll a = 1, b = 1;
    for (int i = 2; i < lim; !arr[i] ? i++ : 0)
    {
        if (arr[i] > 0)
        {
            if (a * i > 10000000)
            {
                num.pb(a);
                a = i;
            }
            else
            {
                a *= i;
            }
            arr[i]--;
        }
        else if (arr[i] < 0)
        {
            if (b * i > 10000000)
            {
                denom.pb(b);
                b = i;
            }
            else
            {
                b *= i;
            }
            arr[i]++;
        }
    }
    num.pb(a);
    denom.pb(b);
#endif

    cout << sz(num) << ' ' << sz(denom) << endl;
    cout << num[0];
    for (int i = 1; i < sz(num); i++)
        cout << ' '  << num[i];
    cout << endl;
    cout << denom[0];
    for (int i = 1; i < sz(denom); i++)
        cout << ' ' << denom[i];
    cout << endl;
}


//#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];

stack<int *> primes[664579];

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];
        stack<int *>& s = primes[sieve[q].a[0]];
        do
        {
            if (insert)
            {
                s.push(p);
            }
            else if (!s.empty())
            {
                *s.top() /= q; s.pop();
                *p /= q;
            }
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
    {
        stack<int *>& s = primes[sieve[y].a[0]];
        if (insert)
        {
            s.push(p);
        }
        else if (!s.empty())
        {
            *s.top() /= y; s.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;
            }
        }
    }
    int q = 0;
    for (int i = 2; i < lim; i++)
    {
        if (sieve[i].p == 0)
        {
            sieve[i].a[0] = q++;
        }
    }

    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');
}


//#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 *, vector<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(100000);

    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');
}


//#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(664579);

    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');
}


//#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 <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))
//}}}

const int lim = 10000001;

struct {
    short p;
    short a[8];
} sieve[lim];

int arr[lim];

template<typename O>
inline void add(int x)
{
    int y = x;
    for (int i = 0; i < sieve[x].p; i++)
    {
        int q = sieve[x].a[i];
        do
        {
            arr[q] = O()(arr[q], 1);
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
    {
        arr[y] = O()(arr[y], 1);
    }
}

inline void compress(const vector<int>& data, vector<int>& res)
{
    int i = 0, j = sz(data)-1;

    while (i <= j)
    {
        ll a = data[j];
        while (i < j && a*data[i] <= 10000000)
        {
            a *= data[i++];
        }
        res.pb(a);
        j--;
    }
}

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;
            }
        }
    }

    int n, m; cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        int a; cin >> a;
        add<plus<int>>(a);
    }
    for (int i = 0; i < m; i++)
    {
        int b; cin >> b;
        add<minus<int>>(b);
    }

    vector<int> num_all, denom_all;
    num_all.reserve(3000000); denom_all.reserve(3000000);

    for (int i = 2; i < lim; i++)
    {
        while (arr[i] > 0)
        {
            num_all.pb(i);
            arr[i]--;
        }
        while (arr[i] < 0)
        {
            denom_all.pb(i);
            arr[i]++;
        }
    }
    if (num_all.empty())
        num_all.pb(1);
    if (denom_all.empty())
        denom_all.pb(1);

    vector<int> num, denom;
    compress(num_all, num);
    compress(denom_all, denom);

#if 0
    vector<int> num, denom;
    ll a = 1, b = 1;
    for (int i = 2; i < lim; !arr[i] ? i++ : 0)
    {
        if (arr[i] > 0)
        {
            if (a * i > 10000000)
            {
                num.pb(a);
                a = i;
            }
            else
            {
                a *= i;
            }
            arr[i]--;
        }
        else if (arr[i] < 0)
        {
            if (b * i > 10000000)
            {
                denom.pb(b);
                b = i;
            }
            else
            {
                b *= i;
            }
            arr[i]++;
        }
    }
    num.pb(a);
    denom.pb(b);
#endif

    cout << sz(num) << " " << sz(denom) << endl;
    cout << num[0];
    for (int i = 1; i < sz(num); i++)
        cout << " " << num[i];
    cout << endl;
    cout << denom[0];
    for (int i = 1; i < sz(denom); i++)
        cout << " " << denom[i];
    cout << endl;
}


//#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 {
    short p;
    short a[8];
} sieve[lim];

int num[100000], denom[100000];

unordered_map<int, stack<int *, vector<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(664579);

    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');
}


//#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 <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))
//}}}

const int lim = 10000001;

struct {
    char p;
    short a[8];
} sieve[lim];

int num[100000], denom[100000];

struct mystruct {
    stack<int *> n, d;
};

unordered_map<int, mystruct> um;

template<stack<int *> mystruct::* A, stack<int *> mystruct::* B>
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 (!(um[q].*B).empty())
            {
                *((um[q].*B).top()) /= q; (um[q].*B).pop();
                *p /= q;
            }
            else
            {
                (um[q].*A).push(p);
            }
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
    {
        if (!(um[y].*B).empty())
        {
            *((um[y].*B).top()) /= y; (um[y].*B).pop();
            *p /= y;
        }
        else
        {
            (um[y].*A).push(p);
        }
    }
}

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(2000000);

    int n, m; cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> num[i];
        add<&mystruct::n, &mystruct::d>(num[i], num+i);
    }
    for (int i = 0; i < m; i++)
    {
        cin >> denom[i];
        add<&mystruct::d, &mystruct::n>(denom[i], denom+i);
    }

    cout << n << ' ' << m << endl;
    cout << num[0];
    for (int i = 1; i < n; i++)
        cout << ' '  << num[i];
    cout << endl;
    cout << denom[0];
    for (int i = 1; i < m; i++)
        cout << ' ' << denom[i];
    cout << endl;
}


//#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 <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))
//}}}

const int lim = 10000001;

struct {
    short p;
    short a[8];
} sieve[lim];

int num[100000], denom[100000];

struct mystruct {
    stack<int *> n, d;
};

unordered_map<int, mystruct> um;

template<stack<int *> mystruct::* A, stack<int *> mystruct::* B>
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 (!(um[q].*B).empty())
            {
                *((um[q].*B).top()) /= q; (um[q].*B).pop();
                *p /= q;
            }
            else
            {
                (um[q].*A).push(p);
            }
            y /= q;
        }
        while (y % q == 0);
    }
    if (y > 1)
    {
        if (!(um[y].*B).empty())
        {
            *((um[y].*B).top()) /= y; (um[y].*B).pop();
            *p /= y;
        }
        else
        {
            (um[y].*A).push(p);
        }
    }
}

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, m; cin >> n >> m;
    for (int i = 0; i < n; i++)
    {
        cin >> num[i];
        add<&mystruct::n, &mystruct::d>(num[i], num+i);
    }
    for (int i = 0; i < m; i++)
    {
        cin >> denom[i];
        add<&mystruct::d, &mystruct::n>(denom[i], denom+i);
    }

    cout << n << ' ' << m << endl;
    cout << num[0];
    for (int i = 1; i < n; i++)
        cout << ' '  << num[i];
    cout << endl;
    cout << denom[0];
    for (int i = 1; i < m; i++)
        cout << ' ' << denom[i];
    cout << endl;
}