#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_imports)]
use std::io::stdin;

mod util {
    use std::io::stdin;
    use std::str::FromStr;
    use std::fmt::Debug;

    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }

    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
}

#[allow(unused_macros)]
macro_rules! get {
    ($t:ty) => {
        {
            let mut line: String = String::new();
            stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t:ty),*) => {
        {
            let mut line: String = String::new();
            stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            (
                $(iter.next().unwrap().parse::<$t>().unwrap(),)*
            )
        }
    };
    ($t:ty; $n:expr) => {
        (0..$n).map(|_|
                    get!($t)
                   ).collect::<Vec<_>>()
    };
    ($($t:ty),*; $n:expr) => {
        (0..$n).map(|_|
                    get!($($t),*)
                   ).collect::<Vec<_>>()
    };
    ($t:ty ;;) => {
        {
            let mut line: String = String::new();
            stdin().read_line(&mut line).unwrap();
            line.split_whitespace()
                .map(|t| t.parse::<$t>().unwrap())
                .collect::<Vec<_>>()
        }
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

struct Flow {
    // to, capacity, rev
    edges: Vec<Vec<(usize, usize, usize)>>,
}

impl Flow {
    fn new(max_size: usize) -> Flow {
        Flow {
            edges: vec![Vec::new(); max_size + 1],
        }
    }

    fn add_edge(&mut self, from: usize, to: usize, cap: usize) {
        let from_rev = self.edges[to].len();
        let to_rev = self.edges[from].len();

        self.edges[from].push((to, cap, from_rev));
        self.edges[to].push((from, 0, to_rev));
    }

    fn max_flow_dinic(&mut self, s: usize, t: usize) -> usize {
        let mut flow = 0;
        loop {
            let level = self.bfs_dinic(s);
            let mut iter = vec![0; self.edges.len()];
            if level[t].is_none() {
                return flow;
            }

            loop {
                let f = self.dfs_dinic(s, t, usize::max_value(), &level, &mut iter);
                if f == 0 {
                    break;
                }
                flow += f;
            }
        }
    }

    fn bfs_dinic(&self, s: usize) -> Vec<Option<usize>> {
        let mut level = vec![None; self.edges.len()];
        let mut que = VecDeque::new();

        level[s] = Some(0);
        que.push_back(s);

        while !que.is_empty() {
            let v = que.pop_front().unwrap();
            let l = Some(level[v].unwrap() + 1);

            for &(to, cap, _) in &self.edges[v] {
                if cap > 0 && level[to].is_none() {
                    level[to] = l;
                    que.push_back(to);
                }
            }
        }
        level
    }

    fn dfs_dinic(
        &mut self,
        v: usize,
        t: usize,
        f: usize,
        level: &[Option<usize>],
        iter: &mut [usize],
    ) -> usize {
        if v == t {
            return f;
        }

        for i in iter[v]..self.edges[v].len() {
            iter[v] = i;

            let (to, cap, rev) = self.edges[v][i];
            if cap > 0 && level[v].unwrap() < level[to].unwrap_or(0) {
                let d = self.dfs_dinic(to, t, min(f, cap), level, iter);
                if d > 0 {
                    self.edges[v][i].1 -= d;
                    self.edges[to][rev].1 += d;
                    return d;
                }
            }
        }
        0
    }

    #[allow(dead_code)]
    fn max_flow_ff(&mut self, s: usize, t: usize) -> usize {
        let mut flow = 0;
        let l = self.edges.len();
        loop {
            let f = self.dfs_ff(s, t, usize::max_value(), &mut vec![false; l]);
            if f == 0 {
                break;
            }
            flow += f;
        }
        flow
    }

    #[allow(dead_code)]
    fn dfs_ff(&mut self, v: usize, t: usize, f: usize, used: &mut [bool]) -> usize {
        if v == t {
            return f;
        }
        used[v] = true;

        for i in 0..self.edges[v].len() {
            let (to, cap, rev) = self.edges[v][i];
            if !used[to] && cap > 0 {
                let d = self.dfs_ff(to, t, min(f, cap), used);
                if d > 0 {
                    self.edges[v][i].1 -= d;
                    self.edges[to][rev].1 += d;
                    return d;
                }
            }
        }
        0
    }

    #[allow(dead_code)]
    fn cut(&self, s: usize) -> BTreeSet<usize> {
        let mut stack = Vec::new();
        let mut ss = BTreeSet::new();
        ss.insert(s);
        stack.push(s);

        while !stack.is_empty() {
            let v = stack.pop().unwrap();
            for &(to, cap, _) in &self.edges[v] {
                if cap > 0 && !ss.contains(&to) {
                    ss.insert(to);
                    stack.push(to);
                }
            }
        }

        ss
    }
}

fn gcd(a: u64, b: u64) -> u64 {
    if b == 0 {
        a
    } else {
        gcd(b, a % b)
    }
}

#[allow(dead_code)]
fn lcm(a: u64, b: u64) -> u64 {
    a / gcd(a, b) * b
}

// (gcd, x, y)
#[allow(dead_code)]
fn extgcd(a: i64, b: i64) -> (i64, i64, i64) {
    if b == 0 {
        (a, 1, 0)
    } else {
        let (gcd, x, y) = extgcd(b, a % b);
        (gcd, y, x - (a / b) * y)
    }
}

#[allow(dead_code)]
fn mod_pow(x: u64, n: u64, m: u64) -> u64 {
    let mut res = 1;
    let mut x = x;
    let mut n = n;
    while n > 0 {
        if n & 1 == 1 {
            res = (res * x) % m;
        }
        x = (x * x) % m;
        n = n >> 1;
    }
    res
}

#[allow(dead_code)]
fn mod_inverse(a: u64, m: u64) -> u64 {
    let (_, x, _) = extgcd(a as i64, m as i64);
    ((m as i64 + x) as u64 % m) % m
}

#[allow(dead_code)]
fn fact_table(len: usize, m: u64) -> Vec<u64> {
    let mut res = vec![1; len];
    for i in 1..len {
        res[i] = (i as u64 * res[i - 1]) % m;
    }
    res
}

// (a mod p, e when n! = a p^e)
fn mod_fact(n: u64, p: u64, fact: &Vec<u64>) -> (u64, u64) {
    if n == 0 {
        (1, 0)
    } else {
        let (a, b) = mod_fact(n / p, p, fact);
        let e = b + n / p;

        if n / p % 2 != 0 {
            (a * (p - fact[(n % p) as usize]) % p, e)
        } else {
            (a * fact[(n % p) as usize] % p, e)
        }
    }
}

#[allow(dead_code)]
fn mod_comb(n: u64, k: u64, p: u64, fact: &Vec<u64>) -> u64 {
    if n < k {
        0
    } else {
        let (a1, e1) = mod_fact(n, p, fact);
        let (a2, e2) = mod_fact(k, p, fact);
        let (a3, e3) = mod_fact(n - k, p, fact);

        if e1 > e2 + e3 {
            0
        } else {
            a1 * mod_inverse(a2 * a3 % p, p) % p
        }
    }
}

fn main() {
    loop {
        let (m, n) = get!(usize, usize);
        if m == 0 && n == 0 {
            break;
        }

        let b = {
            // suck
            let mut v = Vec::new();
            let mut rest = m;
            while rest > 0 {
                let mut input = util::gets::<u64>();
                rest -= input.len();
                v.append(&mut input);
            }
            v
        };

        let r = {
            // suck
            let mut v = Vec::new();
            let mut rest = n;
            while rest > 0 {
                let mut input = util::gets::<u64>();
                rest -= input.len();
                v.append(&mut input);
            }
            v
        };

        let s = m + n + 1;
        let t = s + 1;

        let mut flow = Flow::new(t);

        for i in 0..m {
            flow.add_edge(s, i, 1);
        }

        for i in 0..n {
            flow.add_edge(m + i, t, 1);
        }

        for (i, &x) in b.iter().enumerate() {
            for (k, &y) in r.iter().enumerate() {
                if gcd(x, y) != 1 {
                    flow.add_edge(i, m + k, 1);
                }
            }
        }

        println!("{}", flow.max_flow_dinic(s, t));
    }
}

Measured memory_bytes: 13248