import Data.List
import qualified Data.ByteString.Char8 as B
import Control.Monad
import Data.Ord 

main :: IO ()
main = readLn >>= main'
    where
        main' n = replicateM 2 (readf n) >>= print . solve 
        readf n = fmap edit <$> replicateM n breads
        edit [x, y] = (x, y)

solve :: [[(Int, Int)]] -> Int 
solve [xs, ys] = calc 0 (sf xs) $ sf ys
    where
        sf = sortBy (comparing fst)

calc :: Int -> [(Int, Int)] -> [(Int, Int)] -> Int 
calc acc xs ((ya, yb) : ys) = check bs $ sortBy (\(la, lb) (ra, rb) -> max ra rb `compare` max la lb) as
    where
        (as, bs) = span (\(a, _) -> a < ya) xs

        check cs (c@(_, b) : ds)
            | b < yb = calc (acc + 1) xs' ys
            | otherwise = check (c : cs) ds
            where
                xs' = sortBy (comparing fst) ds ++ cs
        check cs _ = calc acc xs ys

calc acc _ _ = acc


breads :: IO [Int]
breads = unfoldr uff <$> B.getLine
    where
        uff b = check <$> B.readInt b
        check (a, b)
            | B.null b = (a, b)
            | otherwise = (a, B.tail b)