import           Control.Monad
import           Data.Maybe
import qualified Data.ByteString.Char8 as BS
import qualified Data.Vector.Unboxed         as V
import qualified Data.Vector.Unboxed.Mutable as VM

readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine

main = do
  [k, t] <- getIntList
  as <- do ts <- getIntList
           V.thaw $ V.fromList ts

  let lookupCands i c1 m1 c2 m2 | i == t = return (c1, c2)
                                | otherwise = do
                                    n <- VM.read as i
                                    if n > m1
                                      then lookupCands (i+1) i n c1 m1
                                      else if n > m2
                                           then lookupCands (i+1) c1 m1 i n
                                           else lookupCands (i+1) c1 m1 c2 m2

  v <- VM.new (k+1)
  VM.write v 0 t
  forM_ [1..k] $ \i -> do
    t0 <- VM.read v 0
    (c1, c2) <- lookupCands 0 (-1) 0 (-1) 0
    if c1 /= t0 || c2 < 0
      then do n <- VM.read as c1
              VM.write as c1 (n-1)
              VM.write v i c1
              VM.write v 0 c1
      else do n <- VM.read as c2
              VM.write as c2 (n-1)
              VM.write v i c2
              VM.write v 0 c2

  v' <- V.freeze v

  let countAns (x:y:zs) res = countAns (y:zs) (res + (if x==y then 1 else 0))
      countAns _        res = res

  print $ countAns (tail $ V.toList v') 0