import Control.Monad

main :: IO ()
main = do
  num <- readLn
  if (num == 0)
    then (return ())
    else do
      is <- replicateM (num+1) readLn
      let ans = counter (init is) (last is)
            where
              counter :: [Int] -> Int -> Int
              counter ls k = counter' (tpl ls) k 0
                where
                  counter' :: ([Int],[Int],Int) -> Int -> Int -> Int
                  counter' (l,h,m) k c
                    | m == 0 = c
                    | m == k = c + 1
                    | m > k = counter' (tpl l) k (c + 1)
                    | m < k = counter' (tpl h) k (c + 1)
                  tpl :: [Int] -> ([Int],[Int],Int)
                  tpl [] = ([],[],0)
                  tpl xs = tpl' [] xs
                    where
                      length :: [Int] -> Int
                      length [] = 0
                      length (_:xs) = (length xs) + 1

                      tpl' l1 [] = ((init l1),[],(last l1))
                      tpl' l1 (x:xs)
                        | (length l1) >= (length (x:xs)) = ((init l1),(x:xs),(last l1))
                        | otherwise = tpl' (l1 ++ [x]) xs
      print ans
      main