#include <bits/stdc++.h>

#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());

typedef long long LL;
template<typename T> using V = std::vector<T>;
template<typename T> using VV = std::vector<std::vector<T>>;
template<typename T> using VVV = std::vector<std::vector<std::vector<T>>>;
template<class T> inline T ceil(T a, T b) { return (a + b - 1) / b; }
template<class T> inline void print(T x) { std::cout << x << std::endl; }
template<class T> inline bool inside(T y, T x, T H, T W) {return 0 <= y and y < H and 0 <= x and x < W; }

const int INF = 1L << 30;
const double EPS = 1e-9;
const std::string YES = "YES", Yes = "Yes", NO = "NO", No = "No";
const std::vector<int> dy = { 0, 1, 0, -1 }, dx = { 1, 0, -1, 0 };    // 4近傍（右, 下, 左, 上）

using namespace std;

struct Edge {
    int to, cap, rev;
    Edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {
        assert(this->cap >= 0);
    }
};

// 最大流問題をO(F|E|)で解く
class FordFulkerson {
public:
    unsigned int num_of_node = 0;   // ノード数
    map<int, vector<Edge>> G;       // グラフの隣接リスト表現
    vector<bool> used;              // DFSですでに調べたかのフラグ

    FordFulkerson(unsigned int num_of_node) : num_of_node(num_of_node){
    }

    // fromからtoへ向かう容量capの辺とその逆辺をグラフに追加する
    void add_edge(int from, int to, int cap) {
        this->G[from].push_back(Edge(to, cap, (int)this->G[to].size()));        // 辺
        this->G[to].push_back(Edge(from, 0, (int)this->G[from].size() - 1));    // 逆辺
    }

    // sからtへの最大流を求める O(F|E|)
    int max_flow(int s, int t) {
        int flow = 0;
        while (true) {
            this->used = vector<bool>(this->num_of_node, 0);
            int f = dfs(s, t, INT_MAX);
            if (f == 0) { return flow; }
            flow += f;
        }
    }

private:
    // vからtへf流したときの流れた量
    int dfs(int v, int t, int f) {
        if (v == t) { return f; }
        used[v] = true;
        for (Edge &e : G[v]) {
            if (!used[e.to] && e.cap > 0) {
                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;
    }

};

// aとbの最大公約数
int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

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

    while (true) {
        int M, N;
        cin >> M >> N;
        if (M == 0 and N == 0) { break; }

        V<int> blue(M), red(N);
        FOR(i, 0, M) {
            cin >> blue[i];
        }
        FOR(i, 0, N) {
            cin >> red[i];
        }

        int source = M + N;
        int sink = source + 1;
        FordFulkerson ff(M + N + 2);
        FOR(i, 0, blue.size()) {
            FOR(j, 0, red.size()) {
                if (gcd(blue[i], red[j]) != 1) {
                    ff.add_edge(i, blue.size() + j, 1);
                }
            }
        }

        FOR(i, 0, blue.size()) {
            ff.add_edge(source, i, 1);
        }
        FOR(j, 0, red.size()) {
            ff.add_edge(blue.size() + j, sink, 1);
        }

        print(ff.max_flow(source, sink));
    }

    return 0;
}


Measured memory_bytes: 7492