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' (lower ls) (higher ls) (mid 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' (lower l) (higher l) (mid l) k (c + 1)
                    | m < k = counter' (lower h) (higher h) (mid h) k (c + 1)
                  length :: [Int] -> Int
                  length [] = 0
                  length (_:xs) = (length xs) + 1
                  mid :: [Int] -> Int
                  mid [] = 0
                  mid [x] = x
                  mid xs = mid' [] xs
                    where
                      mid' :: [Int] -> [Int] -> Int
                      mid' l1 (x:xs)
                        | (length l1) >= (length (x:xs)) = (last l1)
                        | otherwise = mid' (l1 ++ [x]) xs
                  lower :: [Int] -> [Int]
                  lower [] = []
                  lower [x] = []
                  lower xs = lower' [] xs
                    where
                      lower' :: [Int] -> [Int] -> [Int]
                      lower' l1 (x:xs)
                        | (length l1) >= (length (x:xs)) = (init l1)
                        | otherwise = lower' (l1 ++ [x]) xs
                  higher :: [Int] -> [Int]
                  higher [] = []
                  higher [x] = [x]
                  higher xs = higher' [] xs
                    where
                      higher' :: [Int] -> [Int] -> [Int]
                      higher' l1 (x:xs)
                        | (length l1) >= (length (x:xs)) = (x:xs)
                        | otherwise = higher' (l1 ++ [x]) xs
      print ans
      main