{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE OverloadedStrings #-}

import Control.Monad
import Control.Monad.Trans.State.Strict
import Data.List
import Data.Ord
import Data.Char (isSpace)
import Data.Maybe (fromJust)
import qualified Data.ByteString.Lazy.Char8 as B
--
type Parser a = StateT B.ByteString Maybe a

parseInt :: Parser Int
parseInt = StateT (B.readInt . B.dropWhile isSpace)

parseInt2 :: Parser (Int,Int)
parseInt2 = liftM2 (,) parseInt parseInt

parse :: Parser ([(Int,Int)], [(Int,Int)])
parse = do
    n <- parseInt
    liftM2 (,) (replicateM n parseInt2) (replicateM n parseInt2)

main=do
    (rs,bs) <- (\(xs,ys)->(sort xs, sort ys)).fromJust.evalStateT parse <$> B.getContents
--    print rs
--    print bs
    print.sum $ unfoldr f (rs,bs)

f :: ([(Int,Int)], [(Int,Int)]) -> Maybe(Int, ([(Int,Int)], [(Int,Int)]))
f ([], _ ) = Nothing
f (_ , []) = Nothing
f (rs, (b:bs)) = g (span (<b) rs) (b:bs)

g :: ([(Int,Int)],[(Int,Int)]) -> [(Int,Int)] -> Maybe(Int, ([(Int,Int)],[(Int,Int)]))
g ([],rs) (b:bs) = Just(0, (rs,bs))
g (rs1,rs2) (b:bs) = Just(1, ((filter(/=mr)rs1)++rs2, bs))
    where
        (mr:_) = sortBy (flip $ comparing snd) rs1
