def extgcd(a, b) if b == 0 {x: 1, y: 0, gcd: a} else prev = extgcd(b, a % b) { x: prev[:y], y: prev[:x] - (a / b) * prev[:y], gcd: prev[:gcd] } end end a, b, c, d = gets.split.map(&:to_i) swap = false if a == 0 && c == 0 swap = true a, b = b, a c, d = d, c end gx = a.gcd(c); dy0 = (c*b-a*d).abs / gx hash = extgcd(a/gx, c/gx) dy1 = hash[:x]*b + hash[:y]*d result = {} N = gets.to_i N.times do x, y = gets.split.map(&:to_i) x, y = y, x if swap x2 = x % gx y2 = y - (x-x2)/gx*dy1 y2 %= dy0 if dy0 > 0 result[[x2, y2]] = true end puts result.size