-- rr : r -> r
rr f i = f ++ [i]

-- rl : r -> l
rl f i =
  let h = takeWhile (< i) f
      t = dropWhile (< i) f
  in
    if t == []
    then (i:f)
    else h ++ (i:(tail t))

-- lr : l -> r
lr f i =
  let h = takeWhile (<= i) f
      t = dropWhile (<= i) f
  in
    if h == []
    then f ++ [i]
    else (init h) ++ [i] ++ t

-- ll : l -> l
ll f i = (i:f)

play f _ [] = [f]
play f i0 (i:is) =
  let f' = if i == i0
           then f
           else if i > i0
                then if i0 == last f
                     then rr f i
                     else lr f i
                else if i0 == head f
                     then ll f i
                     else rl f i
  in
    f:(play f' i is)

ans (i:is) =
  let f = play [i] i is
  in
    length $ last f

main = do
  _ <- getLine
  c <- getContents
  let i = map read $ lines c :: [Int]
      o = ans i
  print o

