#define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii;
typedef long long ll; typedef vector<long long> vl; typedef pair<long long,long long> pll; typedef vector<pair<long long,long long> > vpll;
typedef vector<string> vs; typedef long double ld;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline void amax(T &x, U y) { if(x < y) x = y; }

vector<bool> isprime;
vector<int> primes;
void sieve(int n){
	if((int)isprime.size() >= n+1) return;
	isprime.assign(n+1, true);
	isprime[0] = isprime[1] = false;
	int sqrtn = (int)(sqrt(n * 1.) + .5);
	for(int i = 2; i <= sqrtn; i ++) if(isprime[i]) {
		for(int j = i * i; j <= n; j += i)
			isprime[j] = false;
	}
	primes.clear();
	for(int i = 2; i <= n; i ++) if(isprime[i])
		primes.push_back(i);
}

typedef vector<pair<ll,int> > Factors;
void primeFactors(ll x, Factors &out_v) {
	out_v.clear();
	int sqrtx = (int)(sqrt(x*1.) + 10.5);
	sieve(sqrtx);
	each(p, primes) {
		if(*p > sqrtx) break;
		if(x % *p == 0) {
			int t = 1;
			x /= *p;
			while(x % *p == 0) {
				t ++;
				x /= *p;
			}
			out_v.push_back(make_pair(*p, t));
		}
	}
	if(x != 1) out_v.push_back(make_pair(x, 1));
}

template<typename Op> inline Factors combineFactors(Op op, int id, const Factors &a, const Factors &b) {
	Factors c;
	int an = a.size(), bn = b.size();
	int ai = 0, bi = 0;
	while(ai < an || bi < bn) {
		typename Factors::value_type::first_type p; int q;
		if(ai < an && (bi >= bn || a[ai].first < b[bi].first)) {
			p = a[ai].first, q = op(a[ai].second, id);
			ai ++;
		}else if(bi < bn && (ai >= an || a[ai].first > b[bi].first)) {
			p = b[bi].first, q = op(id, b[bi].second);
			bi ++;
		}else {
			p = a[ai].first, q = op(a[ai].second, b[bi].second);
			ai ++, bi ++;
		}
		if(q != 0) c.push_back(mp(p, q));
	}
	return c;
}

Factors gcdFactors(const Factors &a, const Factors &b) {
	return combineFactors<const int&(*)(const int&,const int&)>(std::min<int>, 0, a, b);
}
Factors lcmFactors(const Factors &a, const Factors &b) {
	return combineFactors<const int&(*)(const int&,const int&)>(std::max<int>, 0, a, b);
}

int main() {
	sieve((int)1e7+20);
	int N, M;
	scanf("%d%d", &N, &M);
	Factors g, l;
	rep(i, N) {
		ll A;
		scanf("%lld", &A);
		Factors fs;
		primeFactors(A, fs);
		if(i == 0) g = fs;
		else g = gcdFactors(g, fs);
	}
	rep(i, M) {
		ll B;
		scanf("%lld", &B);
		Factors fs;
		primeFactors(B, fs);
		l = lcmFactors(l, fs);
	}
	map<ll,int> ga(all(g)), la(all(l));
	set<ll> ps;
	each(i, ga) ps.insert(i->first);
	each(i, la) ps.insert(i->first);
	ll ans = 1;
	each(i, ps) {
		ll upper = ga[*i], lower = la[*i];
		if(lower > upper) ans = 0;
		else ans *= upper - lower + 1;
	}
	printf("%lld\n", ans);
	return 0;
}


Measured memory_bytes: 8404