import Control.Applicative ((<$>)) import qualified Data.ByteString.Lazy.Char8 as LC import Data.Maybe (fromJust) readInt :: LC.ByteString -> Int readInt = fst . fromJust . LC.readInt tuplify2 :: [a] -> (a, a) tuplify2 [x, y] = (x, y) generateJudge :: Integral a => a -> a -> ((a, a) -> Bool) generateJudge 0 0 = \(x, y) -> x == 0 && y == 0 generateJudge p q | odd s0 = f | otherwise = \(x, y) -> f (x, y) && even (div x g + div y g) where g = gcd p q s0 = sum $ map (`div` g) [p, q] f (x, y) = mod x g == 0 && mod y g == 0 solve :: Integral a => a -> a -> [(a, a)] -> Int solve p q = length . filter (generateJudge p q) main :: IO () main = do [p, q] <- map read . words <$> getLine _ <- getLine cs <- map (tuplify2 . map readInt . LC.words) . LC.lines <$> LC.getContents print $ solve p q cs