Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
from fractions import gcd

INF = 2 << 30
MAX_V = 1002
G = [[] for _ in xrange(MAX_V)]
visited = [False]*MAX_V

def add_edge(fro, to, cap):
    G[fro].append([to,cap,len(G[to])])
    G[to].append([fro,0,len(G[fro])-1])

def dfs(v,t,f):
    global visited
    if v == t:
        return f
    visited[v] = True
    for i,(to,cap,rev) in enumerate((G[v])):
        if visited[to] or cap == 0:            
            continue        
        d = dfs(to,t,min(f,cap))
        if d > 0:
            G[v][i][1] -= d
            G[to][rev][1] += d
            return d
    return 0

def max_flow(s,t):
    global visited
    flow = 0
    while True:
        visited = [False]*MAX_V
        f = dfs(s,t,INF)
        if f == 0:
            return flow
        flow += f

while True:
    m,n = map(int,raw_input().split())
    if m|n == 0:
        break
    G = [[] for _ in xrange(MAX_V)]
    data = []
    while len(data) < n+m:
        data += map(int,raw_input().split())    
    blue = data[:m]
    red = data[m:]
    for i,b in enumerate(blue):
        add_edge(MAX_V-2,i,1)
    for i,r in enumerate(red):
        add_edge(i+m,MAX_V-1,1)
    for i in xrange(m):
        for j in xrange(n):
            if gcd(blue[i],red[j]) != 1:
                add_edge(i,j+m,1)
    print max_flow(MAX_V-2,MAX_V-1)

Fixed submission:
from fractions import gcd

V = 1001
G = []
visited = []
match = []

def add_edge(u,v):
    G[u].append(v)
    G[v].append(u)

def dfs(v):
    visited[v] = True
    for u in G[v]:
        w = match[u]
        if w < 0 or not visited[w] and dfs(w):
            match[v] = u
            match[u] = v
            return True

def bipartite_matching():
    global match,visited
    res = 0
    match = [-1]*V
    for v in xrange(V):
        if match[v] < 0:
            visited = [False]*V
            if dfs(v):
                res += 1
    return res

while True:
    m,n = map(int,raw_input().split())
    V = m+n
    if V == 0:
        break
    G = [[] for _ in xrange(V)]
    data = []
    while len(data) < V:
        data += map(int,raw_input().split())
    for i,b in enumerate(data[:m]):
        for j,r in enumerate(data[m:],m):
            if gcd(b,r) != 1:
                add_edge(i,j)
    print bipartite_matching()

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
from fractions import gcd

INF = 2 << 30
MAX_V = 1003
G = [[] for _ in xrange(MAX_V)]
visited = [False]*MAX_V

def add_edge(fro, to, cap):
    G[fro].append([to,cap,len(G[to])])
    G[to].append([fro,0,len(G[fro])-1])

def dfs(v,t,f):
    global visited
    if v == t:
        return f
    visited[v] = True
    for i,(to,cap,rev) in enumerate((G[v])):
        if visited[to] or cap == 0:            
            continue        
        d = dfs(to,t,min(f,cap))
        if d > 0:
            G[v][i][1] -= d
            G[to][rev][1] += d
            return d
    return 0

def max_flow(s,t):
    global visited
    flow = 0
    while True:
        visited = [False]*MAX_V
        f = dfs(s,t,INF)
        if f == 0:
            return flow
        flow += f

while True:
    m,n = map(int,raw_input().split())
    if m|n == 0:
        break
    G = [[] for _ in xrange(MAX_V)]
    data = []
    while len(data) < n+m:
        data += map(int,raw_input().split())    
    blue = data[:m]
    red = data[m:]
    for i,b in enumerate(blue):
        add_edge(MAX_V-2,i,1)
    for i,r in enumerate(red):
        add_edge(i+m,MAX_V-1,1)
    for i in xrange(m):
        for j in xrange(n):
            if gcd(blue[i],red[j]) != 1:
                add_edge(i,j+m,1)
    print max_flow(MAX_V-2,MAX_V-1)

Fixed submission:
from fractions import gcd

V = 1001
G = []
visited = []
match = []

def add_edge(u,v):
    G[u].append(v)
    G[v].append(u)

def dfs(v):
    visited[v] = True
    for u in G[v]:
        w = match[u]
        if w < 0 or not visited[w] and dfs(w):
            match[v] = u
            match[u] = v
            return True

def bipartite_matching():
    global match,visited
    res = 0
    match = [-1]*V
    for v in xrange(V):
        if match[v] < 0:
            visited = [False]*V
            if dfs(v):
                res += 1
    return res

while True:
    m,n = map(int,raw_input().split())
    V = m+n
    if V == 0:
        break
    G = [[] for _ in xrange(V)]
    data = []
    while len(data) < V:
        data += map(int,raw_input().split())
    for i,b in enumerate(data[:m]):
        for j,r in enumerate(data[m:],m):
            if gcd(b,r) != 1:
                add_edge(i,j)
    print bipartite_matching()

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
from fractions import gcd

INF = 2 << 30
MAX_V = 1002
G = [[] for _ in xrange(MAX_V)]
visited = [False]*MAX_V

def add_edge(fro, to, cap):
    G[fro].append([to,cap,len(G[to])])
    G[to].append([fro,0,len(G[fro])-1])

def dfs(v,t,f):
    global visited
    if v == t:
        return f
    visited[v] = True
    for i,(to,cap,rev) in enumerate((G[v])):
        if visited[to] or cap == 0:            
            continue        
        d = dfs(to,t,min(f,cap))
        if d > 0:
            G[v][i][1] -= d
            G[to][rev][1] += d
            return d
    return 0

def max_flow(s,t):
    global visited
    flow = 0
    while True:
        visited = [False]*MAX_V
        f = dfs(s,t,INF)
        if f == 0:
            return flow
        flow += f

while True:
    m,n = map(int,raw_input().split())
    if m|n == 0:
        break
    G = [[] for _ in xrange(MAX_V)]
    data = []
    while len(data) < n+m:
        data += map(int,raw_input().split())    
    blue = data[:m]
    red = data[m:]
    for i,b in enumerate(blue):
        add_edge(MAX_V-2,i,1)
    for i,r in enumerate(red):
        add_edge(i+m,MAX_V-1,1)
    for i in xrange(m):
        for j in xrange(n):
            if gcd(blue[i],red[j]) != 1:
                add_edge(i,j+m,1)
    print max_flow(MAX_V-2,MAX_V-1)

Fixed submission:
from fractions import gcd

V = 1001
G = []
visited = []
match = []

def add_edge(u,v):
    G[u].append(v)
    G[v].append(u)

def dfs(v):
    visited[v] = True
    for u in G[v]:
        w = match[u]
        if w < 0 or not visited[w] and dfs(w):
            match[v] = u
            match[u] = v
            return True

def bipartite_matching():
    global match,visited
    res = 0
    match = [-1]*V
    for v in xrange(V):
        if match[v] < 0:
            visited = [False]*V
            if dfs(v):
                res += 1
    return res

while True:
    m,n = map(int,raw_input().split())
    V = m+n
    if V == 0:
        break
    G = [[] for _ in xrange(V)]
    data = []
    while len(data) < V:
        data += map(int,raw_input().split())
    for i,b in enumerate(data[:m]):
        for j,r in enumerate(data[m:],m):
            if gcd(b,r) != 1:
                add_edge(i,j)
    print bipartite_matching()

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
from fractions import gcd

INF = 2 << 30
MAX_V = 1002
G = [[] for _ in xrange(MAX_V)]
visited = [False]*MAX_V

def add_edge(fro, to, cap):
    G[fro].append([to,cap,len(G[to])])
    G[to].append([fro,0,len(G[fro])-1])

def dfs(v,t,f):
    global visited
    if v == t:
        return f
    visited[v] = True
    for i,(to,cap,rev) in enumerate((G[v])):
        if visited[to] or cap == 0:            
            continue        
        d = dfs(to,t,min(f,cap))
        if d > 0:
            G[v][i][1] -= d
            G[to][rev][1] += d
            return d
    return 0

def max_flow(s,t):
    global visited
    flow = 0
    while True:
        visited = [False]*MAX_V
        f = dfs(s,t,INF)
        if f == 0:
            return flow
        flow += f

while True:
    m,n = map(int,raw_input().split())
    if m|n == 0:
        break
    G = [[] for _ in xrange(MAX_V)]
    data = []
    while len(data) < n+m:
        data += map(int,raw_input().split())    
    blue = data[:m]
    red = data[m:]
    for i,b in enumerate(blue):
        add_edge(MAX_V-2,i,1)
    for i,r in enumerate(red):
        add_edge(i+m,MAX_V-1,1)
    for i in xrange(m):
        for j in xrange(n):
            if gcd(blue[i],red[j]) != 1:
                add_edge(i,j+m,1)
    print max_flow(MAX_V-2,MAX_V-1)

Fixed submission:
from fractions import gcd

V = 1001
G = []
visited = []
match = []

def add_edge(u,v):
    G[u].append(v)
    G[v].append(u)

def dfs(v):
    visited[v] = True
    for u in G[v]:
        w = match[u]
        if w < 0 or not visited[w] and dfs(w):
            match[v] = u
            match[u] = v
            return True

def bipartite_matching():
    global match,visited
    res = 0
    match = [-1]*V
    for v in xrange(V):
        if match[v] < 0:
            visited = [False]*V
            if dfs(v):
                res += 1
    return res

while True:
    m,n = map(int,raw_input().split())
    V = m+n
    if V == 0:
        break
    G = [[] for _ in xrange(V)]
    data = []
    while len(data) < V:
        data += map(int,raw_input().split())
    for i,b in enumerate(data[:m]):
        for j,r in enumerate(data[m:],m):
            if gcd(b,r) != 1:
                add_edge(i,j)
    print bipartite_matching()

Buggy python submission (verdict: Runtime Error):
from fractions import gcd

INF = 2 << 30
V = 1003
G = [[] for _ in xrange(V)]
visited = [False]*V

def add_edge(fro, to, cap):
    G[fro].append([to,cap,len(G[to])])
    G[to].append([fro,0,len(G[fro])-1])

def dfs(v,t,f):
    if v == t:
        return f
    visited[v] = True
    for i,(to,cap,rev) in enumerate((G[v])):
        if visited[to] or cap <= 0:            
            continue        
        d = dfs(to,t,min(f,cap))
        if d > 0:
            G[v][i][1] -= d
            G[to][rev][1] += d
            return d
    return 0

def max_flow(s,t):
    flow = 0
    while True:
        for i in xrange(V):
            visited[i] = False
        f = dfs(s,t,INF)
        if f == 0:
            return flow
        flow += f

while True:
    m,n = map(int,raw_input().split())
    if m|n == 0:
        break
    V = m+n+2
    G = [[] for _ in xrange(V)]
    data = []
    while len(data) < n+m:
        data += map(int,raw_input().split())            
    blue = data[:m]
    red = data[m:]
    for i,b in enumerate(blue):
        add_edge(V-2,i,1)
    for i,r in enumerate(red):
        add_edge(i+m,V-1,1)
    for i in xrange(m):
        for j in xrange(n):
            if gcd(blue[i],red[j]) != 1:
                add_edge(i,j+m,1)
    print max_flow(V-2,V-1)

Fixed submission:
from fractions import gcd

V = 1001
G = []
visited = []
match = []

def add_edge(u,v):
    G[u].append(v)
    G[v].append(u)

def dfs(v):
    visited[v] = True
    for u in G[v]:
        w = match[u]
        if w < 0 or not visited[w] and dfs(w):
            match[v] = u
            match[u] = v
            return True

def bipartite_matching():
    global match,visited
    res = 0
    match = [-1]*V
    for v in xrange(V):
        if match[v] < 0:
            visited = [False]*V
            if dfs(v):
                res += 1
    return res

while True:
    m,n = map(int,raw_input().split())
    V = m+n
    if V == 0:
        break
    G = [[] for _ in xrange(V)]
    data = []
    while len(data) < V:
        data += map(int,raw_input().split())
    for i,b in enumerate(data[:m]):
        for j,r in enumerate(data[m:],m):
            if gcd(b,r) != 1:
                add_edge(i,j)
    print bipartite_matching()

Buggy python submission (verdict: Runtime Error):
from fractions import gcd

INF = 2 << 30
V = 1002
G = [[] for _ in xrange(V)]
visited = [False]*V

def add_edge(fro, to, cap):
    G[fro].append([to,cap,len(G[to])])
    G[to].append([fro,0,len(G[fro])-1])

def dfs(v,t,f):
    if v == t:
        return f
    visited[v] = True
    for i,(to,cap,rev) in enumerate((G[v])):
        if visited[to] or cap == 0:            
            continue        
        d = dfs(to,t,min(f,cap))
        if d > 0:
            G[v][i][1] -= d
            G[to][rev][1] += d
            return d
    return 0

def max_flow(s,t):
    flow = 0
    while True:
        for i in xrange(V):
            visited[i] = False
        f = dfs(s,t,INF)
        if f == 0:
            return flow
        flow += f

while True:
    m,n = map(int,raw_input().split())
    if m|n == 0:
        break
    V = m+n+2
    G = [[] for _ in xrange(V)]
    data = []
    while len(data) < n+m:
        data += map(int,raw_input().split())            
    blue = data[:m]
    red = data[m:]
    for i in xrange(m):
        add_edge(V-2,i,1)
    for i in xrange(m,m+n):
        add_edge(i,V-1,1)
    for i,b in enumerate(blue):
        for j,r in enumerate(red,m):
            if gcd(b,r) != 1:
                add_edge(i,j,1)
    print max_flow(V-2,V-1)

Fixed submission:
from fractions import gcd

V = 1001
G = []
visited = []
match = []

def add_edge(u,v):
    G[u].append(v)
    G[v].append(u)

def dfs(v):
    visited[v] = True
    for u in G[v]:
        w = match[u]
        if w < 0 or not visited[w] and dfs(w):
            match[v] = u
            match[u] = v
            return True

def bipartite_matching():
    global match,visited
    res = 0
    match = [-1]*V
    for v in xrange(V):
        if match[v] < 0:
            visited = [False]*V
            if dfs(v):
                res += 1
    return res

while True:
    m,n = map(int,raw_input().split())
    V = m+n
    if V == 0:
        break
    G = [[] for _ in xrange(V)]
    data = []
    while len(data) < V:
        data += map(int,raw_input().split())
    for i,b in enumerate(data[:m]):
        for j,r in enumerate(data[m:],m):
            if gcd(b,r) != 1:
                add_edge(i,j)
    print bipartite_matching()

Buggy python submission (verdict: Runtime Error):
from fractions import gcd

INF = 2 << 30
V = 1002
G = [[] for _ in xrange(V)]
visited = [False]*V

def add_edge(fro, to, cap):
    G[fro].append([to,cap,len(G[to])])
    G[to].append([fro,0,len(G[fro])-1])

def dfs(v,t,f):
    if v == t:
        return f
    visited[v] = True
    for i,(to,cap,rev) in enumerate((G[v])):
        if visited[to] or cap == 0:            
            continue        
        d = dfs(to,t,min(f,cap))
        if d > 0:
            G[v][i][1] -= d
            G[to][rev][1] += d
            return d
    return 0

def max_flow(s,t):
    flow = 0
    while True:
        for i in xrange(V):
            visited[i] = False
        f = dfs(s,t,INF)
        if f == 0:
            return flow
        flow += f

while True:
    m,n = map(int,raw_input().split())
    if m|n == 0:
        break
    V = m+n+2
    G = [[] for _ in xrange(V)]
    visited = [True] * V
    data = []
    dbg = 0
    while len(data) < n+m:
        dbg += 1
        data += map(int,raw_input().split())            
        if dbg >= 1500:
            break
    blue = data[:m]
    red = data[m:]
    for i in xrange(m):
        add_edge(V-2,i,1)
    for i in xrange(m,m+n):
        add_edge(i,V-1,1)
    for i,b in enumerate(blue):
        for j,r in enumerate(red,m):
            if gcd(b,r) != 1:
                add_edge(i,j,1)
    print max_flow(V-2,V-1)

Fixed submission:
from fractions import gcd

V = 1001
G = []
visited = []
match = []

def add_edge(u,v):
    G[u].append(v)
    G[v].append(u)

def dfs(v):
    visited[v] = True
    for u in G[v]:
        w = match[u]
        if w < 0 or not visited[w] and dfs(w):
            match[v] = u
            match[u] = v
            return True

def bipartite_matching():
    global match,visited
    res = 0
    match = [-1]*V
    for v in xrange(V):
        if match[v] < 0:
            visited = [False]*V
            if dfs(v):
                res += 1
    return res

while True:
    m,n = map(int,raw_input().split())
    V = m+n
    if V == 0:
        break
    G = [[] for _ in xrange(V)]
    data = []
    while len(data) < V:
        data += map(int,raw_input().split())
    for i,b in enumerate(data[:m]):
        for j,r in enumerate(data[m:],m):
            if gcd(b,r) != 1:
                add_edge(i,j)
    print bipartite_matching()

Buggy python submission (verdict: Runtime Error):
from fractions import gcd

INF = 2 << 30
V = 1002
G = [[] for _ in xrange(V)]
visited = [False]*V

def add_edge(fro, to, cap):
    G[fro].append([to,cap,len(G[to])])
    G[to].append([fro,0,len(G[fro])-1])

def dfs(v,t,f):
    if v == t:
        return f
    visited[v] = True
    for i,(to,cap,rev) in enumerate((G[v])):
        if visited[to] or cap == 0:
            continue        
        d = dfs(to,t,min(f,cap))
        if d > 0:
            G[v][i][1] -= d
            G[to][rev][1] += d
            return d
    return 0

def max_flow(s,t):
    flow = 0
    dbg = 0
    while True:
        for i in xrange(V):
            visited[i] = False
        f = dfs(s,t,INF)
        if f == 0:
            return flow
        flow += f
        dbg += 1
        if dbg > 600:
            return -1

while True:
    m,n = map(int,raw_input().split())
    if m|n == 0:
        break
    V = m+n+2
    G = [[] for _ in xrange(V)]
    visited = [True] * V
    data = []
    while len(data) < n+m:
        data += map(int,raw_input().split())
    blue = data[:m]
    red = data[m:]
    for i in xrange(m):
        add_edge(V-2,i,1)
    for i in xrange(m,m+n):
        add_edge(i,V-1,1)
    for i,b in enumerate(blue):
        for j,r in enumerate(red,m):
            if gcd(b,r) != 1:
                add_edge(i,j,1)
    print max_flow(V-2,V-1)

Fixed submission:
from fractions import gcd

V = 1001
G = []
visited = []
match = []

def add_edge(u,v):
    G[u].append(v)
    G[v].append(u)

def dfs(v):
    visited[v] = True
    for u in G[v]:
        w = match[u]
        if w < 0 or not visited[w] and dfs(w):
            match[v] = u
            match[u] = v
            return True

def bipartite_matching():
    global match,visited
    res = 0
    match = [-1]*V
    for v in xrange(V):
        if match[v] < 0:
            visited = [False]*V
            if dfs(v):
                res += 1
    return res

while True:
    m,n = map(int,raw_input().split())
    V = m+n
    if V == 0:
        break
    G = [[] for _ in xrange(V)]
    data = []
    while len(data) < V:
        data += map(int,raw_input().split())
    for i,b in enumerate(data[:m]):
        for j,r in enumerate(data[m:],m):
            if gcd(b,r) != 1:
                add_edge(i,j)
    print bipartite_matching()

Buggy python submission (verdict: Runtime Error):
from fractions import gcd

INF = 2 << 30
V = 1002
G = [[] for _ in xrange(V)]
visited = [False]*V

def add_edge(fro, to, cap):
    G[fro].append([to,cap,len(G[to])])
    G[to].append([fro,0,len(G[fro])-1])

def dfs(v,t,f):
    if v == t:
        return f
    visited[v] = True
    for i,(to,cap,rev) in enumerate((G[v])):
        if visited[to] or cap == 0:
            continue        
        d = dfs(to,t,min(f,cap))
        if d > 0:
            G[v][i][1] -= d
            G[to][rev][1] += d
            return d
    return 0

def max_flow(s,t):
    flow = 0
    while True:
        for i in xrange(V):
            visited[i] = False
        f = dfs(s,t,INF)
        if f == 0:
            return flow
        flow += f

while True:
    m,n = map(int,raw_input().split())
    if (m,n) == (0,0):
        break
    V = m+n+2
    G = [[] for _ in xrange(V)]
    visited = [True] * V
    data = []
    while len(data) < n+m:
        data += map(int,raw_input().split())
    blue = data[:m]
    red = data[m:]
    for i in xrange(m):
        add_edge(V-2,i,1)
    for i in xrange(m,m+n):
        add_edge(i,V-1,1)
    for i,b in enumerate(blue):
        for j,r in enumerate(red,m):
            if gcd(b,r) != 1:
                add_edge(i,j,1)
    print max_flow(V-2,V-1)

Fixed submission:
from fractions import gcd

V = 1001
G = []
visited = []
match = []

def add_edge(u,v):
    G[u].append(v)
    G[v].append(u)

def dfs(v):
    visited[v] = True
    for u in G[v]:
        w = match[u]
        if w < 0 or not visited[w] and dfs(w):
            match[v] = u
            match[u] = v
            return True

def bipartite_matching():
    global match,visited
    res = 0
    match = [-1]*V
    for v in xrange(V):
        if match[v] < 0:
            visited = [False]*V
            if dfs(v):
                res += 1
    return res

while True:
    m,n = map(int,raw_input().split())
    V = m+n
    if V == 0:
        break
    G = [[] for _ in xrange(V)]
    data = []
    while len(data) < V:
        data += map(int,raw_input().split())
    for i,b in enumerate(data[:m]):
        for j,r in enumerate(data[m:],m):
            if gcd(b,r) != 1:
                add_edge(i,j)
    print bipartite_matching()