#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
//#define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c),end(c)
//#define EXIST(s, e) ((s).find(e)!=(s).end())
//#define SORT(c) sort(begin(c),end(c))
//#define pb emplace_back
//#define MP make_pair
//#define SZ(a) int((a).size())

//#define LOCAL 0
//#ifdef LOCAL
//#define DEBUG(s) cout << (s) << endl
//#define dump(x)  cerr << #x << " = " << (x) << endl
//#define BR cout << endl;
//#else
//#define DEBUG(s) do{}while(0)
//#define dump(x) do{}while(0)
//#define BR
//#endif


//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007
//ここから編集する

class Flow {
public:
    struct edge {
        int to, cap, rev;
    };
    vector<vector<edge> > G;
    vector<bool> used;
    vector<int> level;//sからの距離
    vector<int> iter;//どこまで調べ終わったか

    //0 ~ maxSize-1 のフローを構築できるようにする
    explicit Flow(unsigned maxSize) {
        G.resize(maxSize, vector<edge>());
        used.resize(maxSize, false);
        level.resize(maxSize, 0);
        iter.resize(maxSize, 0);
    }

    void add_edge(int from, int to, int cap) {
        G[from].push_back((edge) {to, cap, (int) G[to].size()});
        G[to].push_back((edge) {from, 0, (int) G[from].size() - 1});
    }

    //sからの最短距離をDFSで探す
    void bfs(int s) {
//        memset(level, -1, sizeof(level));
        fill(level.begin(), level.end(), -1);
        queue<int> que;
        level[s] = 0;
        que.push(s);
        while (!que.empty()) {
            int v = que.front();
            que.pop();
            for (int i = 0; i < G[v].size(); i++) {
                edge &e = G[v][i];
                if (e.cap > 0 && level[e.to] < 0) {
                    level[e.to] = level[v] + 1;
                    que.push(e.to);
                }
            }
        }
    }

    //増加パスを探す
    int dfs(int v, int t, int f) {
        if (v == t) return f;
        for (int &i = iter[v]; i < G[v].size(); i++) {
            edge &e = G[v][i];
            if (e.cap > 0 && level[v] < level[e.to]) {
                int d = dfs(e.to, t, min(f, e.cap));
                if (d > 0) {
                    e.cap -= d;
                    G[e.to][e.rev].cap += d;
                    return d;
                }
            }
        }
        return 0;
    }

    //sからtへの最大流を求める
    int max_flow(int s, int t) {
        int flow = 0;
        for (;;) {
            bfs(s);
            if (level[t] < 0) return flow;
//            memset(iter, 0, sizeof(iter));
            fill(iter.begin(), iter.end(), 0);
            int f;
            while ((f = dfs(s, t, INF)) > 0) {
                flow += f;
            }
        }
    }

};

ll gcd(ll a, ll b) {
    if (a < b) swap(a, b);
    if (a % b == 0) return b;
    return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

bool calc() {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) return false;
    vector<int> b(n), r(m);
    rep(i, 0, n) cin >> b[i];
    rep(i, 0, m) cin >> r[i];

    int s = 0;
    int t = n + m + 1;
    Flow flow(n + m + 2);
    rep(i, 0, n) {
        flow.add_edge(s, i + 1, 1);
    }
    rep(i, 0, m) {
        flow.add_edge(n + i + 1, t, 1);
    }
    rep(i, 0, n) {
        rep(j, 0, m) {
            if (gcd(b[i], r[j]) > 1) {
                flow.add_edge(i + 1, j + n + 1, 1);
            }
        }
    }

    int ans = flow.max_flow(s, t);
    cout << ans << endl;


    return true;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    while (calc()) {}


    return 0;
}



Measured memory_bytes: 7364