import Control.Monad (replicateM)
import Data.Array (Array,array,(!),bounds)

main = do
    n <- readLn
    if n == 0 then return () else do
        ps <- replicateM n (getLine >>= return . (\[a,b]->(a,b)) . map read . words)
        print $ maxLength $ strings ps
        main

type Strings = Array Int [Int]

strings :: [(Int,Int)] -> Strings
strings ps = array (b0,b1) [(i,otherside i)|i<-[b0..b1]]
    where
    otherside a = [b|(a',b)<-ps,a'==a] ++ [b|(b,a')<-ps,a'==a]
    b0 = min (minimum (map fst ps)) (minimum (map snd ps))
    b1 = max (maximum (map fst ps)) (maximum (map snd ps))

maxLength strings = maxLength' initChain 1
    where
    (b0,b1) = bounds strings
    initChain = [[i]|i<-[b0..b1], length (strings!i) > 0]

    maxLength' []     n = n-1
    maxLength' chains n = maxLength' (chains >>= augment strings) (n+1)

    augment strings chain = map (:chain) newRings
        where
        newRings = filter (not . (`elem` chain)) (strings ! (head chain))