import Data.Maybe
import qualified Data.ByteString.Char8 as B
import qualified Data.IntSet as IS
main=print.f.map(fst.fromJust.B.readInt).B.words=<<B.getContents
f(n:k:p)=n-k+1-g 0 0 0 0 n p IS.empty p where
  g x i a c v(w:p) s (u:q)=g(x+1)j b d w p t r where
    j=if v<w then i+1 else 1
    b=if j>=k then a+1 else a
    d=if x>=k && j<k && IS.findMin s==u && IS.findMax s<w then c+1 else c
    t=IS.insert w$if x<k then s else IS.delete u s
    r=if x<k then (u:q) else q
  g _ _ a c _ _ _ _= max 1 a-1+c