#include <algorithm>
#include <iostream>
using namespace std;

int gcd(int a,int b){
	if(a<b) swap(a,b);
	if(a%b == 0) return b;
	else return gcd(b,a%b);
}
int m,n;
bool c[500][500],v[500];
int b[500],r[500],p[500];
bool match(int bc){
	if(bc < 0) return true;
	for(int i = 0; i < n; ++i){
		if(!c[bc][i] || v[i]) continue;
		v[i] = true;
		if(match(p[i])){
			p[i] = bc;
			return true;
		}
	}
	return false;
}
int main(){
	while(cin>>m>>n,n){
		for(int i = 0; i < m; ++i) cin >> b[i];
		for(int i = 0; i < n; ++i) cin >> r[i];
		for(int i = 0; i < m; ++i)for(int j = 0; j < n; ++j){
			c[i][j] = gcd(b[i],r[j]) > 1;
		}
		for(int i = 0; i < n; ++i)p[i] = -1;
		int ans = 0;
		for(int i = 0; i < m; ++i){
			for(int j = 0; j < n; ++j)v[j] = 0;
			if(match(i)) ++ans;
		}
		cout << ans << endl;
	}
}