#include <bits/stdc++.h>
#define rep(i, start, end) for (long long i = start; i < end; ++i)
#define repreverse(i, start, end) for (long long i = start; i >= end; --i)
#define all(x) (x).begin(), (x).end()
#define len(x) ((long long)(x).size())
#define lcm(a, b) ((a) / __gcd((a), (b)) * (b))
using namespace std;
using ll = long long;
using ld = long double;
using vll = vector<ll>;
using vllvll = vector<vll>;
using pll = pair<ll, ll>;
template<class T>void print1d(T x,ll n=-1){if(n==-1)n=x.size();rep(i,0,n){cout<<x[i]<<' ';}cout<<'\n';}
template<class T>void print2d(T x,ll r=-1,ll c=-1){if(r==-1)r=x.size();if(c==-1)c=x[0].size();rep(i,0,r)print1d(x[i],c);}
template<class T, class U>bool haskey(T mp, U key) { return mp.find(key) != mp.end(); }
template<class T, class U>bool isin(T el, U container) { return find(all(container), el) != container.end(); }
template<class T>bool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; }
template<class T>bool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; }
template<class T>bool even(T n) { return ! (n & 1); }
template<class T>bool odd(T n) { return n & 1; }
template<class T>ld deg2rad(T deg) { return M_PI * deg / 180.0; }
template<class T>ld rad2deg(T rad) { return 180.0 * rad / M_PI; }
ll intpow(ll a,ll n){ll p=1;while(n){if(n&1)p*=a;a*=a;n>>=1;}return p;}
const long double pi = M_PI;
const long long big = 1LL << 50;
const long long inf = 1LL << 60;
const long long mod = 1e9 + 7;

const int MAX = 1100;

struct Edge
{
        long long to, cap, rev;
};

std::vector<std::vector<Edge> > flowGraph(MAX);
std::vector<long long> level(MAX);
std::vector<long long> iter(MAX);

void init()
{
        std::fill(flowGraph.begin(), flowGraph.end(), vector<Edge>(0));
}

void add_edge(long long from, long long to, long long cap)
{
        flowGraph[from].push_back((Edge){to, cap, (long long)flowGraph[to].size()});
        flowGraph[to].push_back((Edge){from, 0, (long long)flowGraph[from].size()-1});
}

void _bfs(long long s)
{
        std::fill(level.begin(), level.end(), -1);
        std::queue<long long> q;
        level[s] = 0;
        q.push(s);
        while (! q.empty()) {
                long long v = q.front();
                q.pop();
                for (long long i = 0; i < (long long)flowGraph[v].size(); ++i) {
                        Edge &e = flowGraph[v][i];
                        if (e.cap > 0 and level[e.to] < 0) {
                                level[e.to] = level[v] + 1;
                                q.push(e.to);
                        }
                }
        }
}

long long _dfs(long long v, long long t, long long f)
{
        if (v == t) return f;

        for (long long& i = iter[v]; i < (long long)flowGraph[v].size(); ++i) {
                Edge &e = flowGraph[v][i];
                if (level[v] < level[e.to] and e.cap > 0) {
                        long long d = _dfs(e.to, t, min(f, e.cap));
                        if (d > 0) {
                                e.cap -= d;
                                flowGraph[e.to][e.rev].cap += d;
                                return d;
                        }
                }
        }
        return 0;
}

long long max_flow(long long s, long long t)
{
        long long flow = 0;
        while (true) {
                _bfs(s);
                if (level[t] < 0) return flow;
                std::fill(iter.begin(), iter.end(), 0);
                long long f;
                while ((f = _dfs(s, t, 1LL<<60)) > 0) {
                        flow += f;
                }
        }
}



int main()
{
        vll ans;
        while (true) {
                ll m, n;
                cin >> m >> n;
                if (m == 0 and n == 0) break;
                
                init();
                vll b(m), r(n);
                rep(i, 0, m) cin >> b[i];
                rep(i, 0, n) cin >> r[i];

                vllvll connected(m, vll(n));

                rep(i, 0, m) {
                        rep(j, 0, n) {
                                if (__gcd(b[i], r[j]) >= 2) {
                                        connected[i][j] = 1;
                                }
                        }
                }

                ll left = MAX-2;
                ll right = MAX-1;

                rep(i, 0, m) {
                        add_edge(left, i, 1);
                }

                rep(i, 0, m) {
                        rep(j, 0, n) {
                                if (connected[i][j]) {
                                        add_edge(i, m+j, 1);
                                }
                        }
                }

                rep(i, 0, n) {
                        add_edge(m+i, right, 1);
                }

                ans.push_back(max_flow(left, right));
        }
        rep(i, 0, len(ans)) {
                cout << ans[i] << endl;
        }
}


Measured memory_bytes: 17292