module Main where

import Control.Applicative
import Control.Monad
import Data.List
import qualified Data.Text as T
import qualified Data.Text.IO as T
import Data.Maybe

main :: IO()
main = do
    n <- read <$> getLine
    inputs <- map ((\[a, b] -> (read a, read b)) . words) <$> replicateM n getLine
    print $ solve inputs

    
solve :: [(Int, Int)] -> Int
solve i = sub i 0 where
    sub :: [(Int, Int)] -> Int -> Int
    sub i' ame
        | isFinish i' = ame
        | otherwise = sub (gezan . tozan $ i') (ame + 1)

updateList :: (Eq a) => [a] -> a -> a -> [a]
updateList l pre after = takeWhile (/= pre) l ++ (after : (tail $ dropWhile (/= pre) l))

findALighterThanB :: [(Int, Int)] -> Maybe (Int, Int)
findALighterThanB [] = Nothing
findALighterThanB ((a, b):abs) = if a < b
    then Just (a, b)
    else findALighterThanB abs

findBLighterThanA :: [(Int, Int)] -> Maybe (Int, Int)
findBLighterThanA [] = Nothing
findBLighterThanA ((a, b):abs) = if a > b
    then Just (a, b)
    else findBLighterThanA abs

findEqual :: [(Int, Int)] -> Maybe (Int, Int)
findEqual [] = Nothing
findEqual ((a, b):abs) = if a == b
    then Just (a, b)
    else findEqual abs

tozan :: [(Int, Int)] -> [(Int, Int)]
tozan i = case findALighterThanB filterdI of
    Nothing -> case findEqual filterdI of
        Just (a, b) -> updateList i (a, b) (a - 1, b)
        Nothing -> case findBLighterThanA filterdI of
            Just (a, b) -> updateList i (a, b) (a - 1, b)
            Nothing     -> error "error"
    Just (a, b) -> updateList i (a, b) (a - 1, b)
    where
        filterdI = filter (\(a_, _) -> a_ /= 0) i

gezan :: [(Int, Int)] -> [(Int, Int)]
gezan i = case findALighterThanB filterdI of
    Nothing -> case findEqual filterdI of
        Just (a, b) -> updateList i (a, b) (a, b - 1)
        Nothing -> case findBLighterThanA filterdI of
            Just (a, b) -> updateList i (a, b) (a, b - 1)
            Nothing     -> error "error"
    Just (a, b) -> updateList i (a, b) (a, b - 1)
    where
        filterdI = filter (\(_, b_) -> b_ /= 0) i

isFinish :: [(Int, Int)] -> Bool
isFinish [] = True
isFinish ((a, b):abs) = if a == b
    then isFinish abs
    else False