p,q = map(int,input().split()) N = int(input()) def gcd(a,b): if b == 0:return a while True: r =a % b a = b b = r if r == 0:return a ans = 0 d = gcd(p,q) for _ in range(N): x,y = map(int,input().split()) if x % d == 0 and y % d == 0: ans += 1 print(ans)