import Data.List (sortOn)
import qualified Data.Ord as Ord
import qualified Data.Set as Set
import Control.Monad.Writer.Strict

newtype DList a = DL { unDL :: [a] -> [a] }

dListFromList :: [a] -> DList a
dListFromList = DL . (++)
dListToList :: DList a -> [a]
dListToList = ($ []) . unDL
dListEmpty :: DList a
dListEmpty = DL id
dListSingleton :: a -> DList a
dListSingleton = DL . (:)
dListAppend :: DList a -> DList a -> DList a
dListAppend xs ys = DL (unDL xs . unDL ys)
instance Monoid (DList a) where
	mempty = dListEmpty
	mappend = dListAppend

list2Tuple (a:b:_) = (a, b)

data RangeTree = RangeTree {
	startpoint :: Set.Set Int,
	endpoint :: Set.Set Int
} deriving (Show)

emptyTree :: RangeTree
emptyTree = RangeTree Set.empty Set.empty

null :: RangeTree -> Bool
null tree = Set.null $ startpoint tree

length :: RangeTree -> Int
length t = max 0 length'
	where
		length' = ep - sp + 1
		sp = Set.findMax $ startpoint t
		ep = Set.findMin $ endpoint t

insert :: (Int, Int) -> RangeTree -> RangeTree
insert (l, r) tree = RangeTree newSp newEp
	where
		newSp = Set.insert l $ startpoint tree
		newEp = Set.insert r $ endpoint tree

delete ::  (Int, Int) -> RangeTree -> RangeTree
delete (l, r) tree = RangeTree newSp newEp
	where
		newSp = Set.delete l $ startpoint tree
		newEp = Set.delete r $ endpoint tree

fromList :: [(Int, Int)] -> RangeTree
fromList = foldr Main.insert emptyTree

move :: (Int, Int) -> Writer (DList Int) (RangeTree, RangeTree) -> Writer (DList Int) (RangeTree, RangeTree)
move (l, r) trees = do
	(tree1, tree2) <- trees
	let newTree1 = delete (l, r) tree1
	let newTree2 = insert (l, r) tree2
	when (not (Main.null newTree1) && not (Main.null newTree2)) $ do
		tell $ dListSingleton $ Main.length newTree1 + Main.length newTree2
		return ()
	return (newTree1, newTree2)

main :: IO ()
main = do
	n <- readLn
	lrs <- sortOn fst . map (list2Tuple . map read . words) . take n . lines <$> getContents :: IO [(Int, Int)]
	let initTree = writer ((fromList lrs, emptyTree), dListEmpty)
	let lengths = execWriter $ foldr move initTree lrs
	print $ maximum $ dListToList lengths
