#include<iostream>
#include<sstream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cfloat>
#include<functional>
#include<map>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<deque>
#include<set>
#include<bitset>
#include<list>
#include<numeric>
#include<complex>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<long long, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<long long, long long> ll_ll;
typedef pair<double, double> d_d;
typedef vector<int> Vint;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define rep2d(i, j, n) for(i = 0;i < n;++i)for(j = i + 1;j < n;++j)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define scn(n, a) rep(i, n)cin >> a[i]
#define sc2n(n, a, b) rep(i, n)cin >> a[i] >> b[i]
#define pri(x) cout << x << "\n"
#define pri2(x, y) cout << x << " " << y << "\n"
#define pri3(x, y, z) cout << x << " " << y << " " << z << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define endl "\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << x << "\n"
#define debug2(x, y) cout << " --- " << x << " " << y << "\n"
#define debug3(x, y, z) cout << " --- " << x << " " << y << " " << z << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

struct edge{
  int to, cap, inv;
};

vector<edge> G[1002];
bool vsted[1002];
int inf = 123456789; //???????°´??????

int dfs(int v, int now, int t, int w){
  if(now == t)return w;
  vsted[now] = true;
  for(int i = 0;i < G[now].size();++i){
    edge tmp = G[now][i];
    if(!vsted[tmp.to] && tmp.cap > 0){
      int d = dfs(v, tmp.to, t, min(w, tmp.cap));
      if(d > 0){
        G[now][i].cap -= d;
        G[tmp.to][tmp.inv].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int edmondskarp(int v, int s, int t){
  int res = 0;
  for(;;){
    for(int i = 0;i < v;++i)vsted[i] = false;
    int x = dfs(v, s, t, inf);
    if(x == 0)return res;
    res += x;
  }
  return res;
}

#define N 10000001
int p[N] = {0}, b[501], r[501];

bool isedge(int b, int r){
  if(b == 1)return false;
  if(b % r == 0 || r % b == 0)return true;
  int x = p[b];
  if(x == 0)return false;
  if(r % x == 0)return true;
  return isedge(b / x, r);
}

signed main(void){
  int i, j, k;
  for(i = 2;i * i <= N;++i)for(j = i;j < N;j+=i)if(p[j] == 0)p[j] = i;

  for(;;){
    int2(m, n);
    if(!(m|n))break;
    int v = m + n + 2;
    scn(m, b); scn(n, r);
    rep(i, m)rep(j, n)if(isedge(b[i], r[j])){
      int l = i, r = j; r += m;
      G[l].pb((edge){r, 1, G[r].size()});
      G[r].pb((edge){l, 0, G[l].size() - 1});
    }
    rep(i, m){
      int r = i, l = m + n;
      G[l].pb((edge){r, 1, G[r].size()});
      G[r].pb((edge){l, 0, G[l].size() - 1});
    }
    rep(i, n){
      int l = i + m, r = m + n + 1;
      G[l].pb((edge){r, 1, G[r].size()});
      G[r].pb((edge){l, 0, G[l].size() - 1});
    }

    cout << edmondskarp(v, v - 2, v - 1) << endl;
    rep(i, v)G[i].clear();
  }

  return 0;
}

Measured memory_bytes: 46684