#![allow(unused_imports, unused_macros, dead_code)]

macro_rules! min {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| min!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = min!($($ys),*);
        if $x < t { $x } else { t }
    }}
}
macro_rules! max {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        it.next().map(|z| it.fold(z, |x, y| max!(x, y)))
    }};
    ($x:expr) => ($x);
    ($x:expr, $($ys:expr),*) => {{
        let t = max!($($ys),*);
        if $x > t { $x } else { t }
    }}
}


macro_rules! ewriteln {
    ($($args:expr),*) => { let _ = writeln!(&mut std::io::stderr(), $($args),*); };
}
macro_rules! trace {
    ($x:expr) => {};
    ($($xs:expr),*) => {}
    // ($x:expr) => { ewriteln!(">>> {} = {:?}", stringify!($x), $x) };
    // ($($xs:expr),*) => { trace!(($($xs),*)) }
}
macro_rules! put {
    (.. $x:expr) => {{
        let mut it = $x.iter();
        if let Some(x) = it.next() { print!("{}", x); }
        for x in it { print!(" {}", x); }
        println!("");
    }};
    ($x:expr) => { println!("{}", $x) };
    ($x:expr, $($xs:expr),*) => { print!("{} ", $x); put!($($xs),*) }
}

const M: i64 = 1_000_000_007;

use std::collections::BTreeSet;

fn main() {
    let mut sc = Scanner::new();
    let n: usize = sc.cin();
    let k: usize = sc.cin();
    let xs: Vec<i32> = (0..n).map(|_| sc.cin()).collect();

    let mut sorted = vec![0; n];  // i = sorted[j] := (i, j) is sorted.
    for i in 1..n {
        if xs[i - 1] < xs[i] {
            sorted[i] = sorted[i - 1];
        } else {
            sorted[i] = i;
        }
    }

    let mut tree = BTreeSet::new();
    let mut tree_rev = BTreeSet::new();

    for i in 0..k {
        tree.insert(xs[i]);
        tree_rev.insert(-xs[i]);
    }

    let mut any_sorted = false;
    let mut ans = 0;

    for i in 0..n-k+1 {
        trace!(i, i + k - 1);
        let this_is_sorted = sorted[i + k - 1] <= i;
        if this_is_sorted { any_sorted = true; }
        trace!(this_is_sorted);
        if i == 0 {
            if !this_is_sorted {
                ans += 1;
            }
        } else {
            tree.insert(xs[i + k - 1]);
            tree_rev.insert(-xs[i + k - 1]);
            let min = *tree.iter().next().unwrap();
            let max = -*tree_rev.iter().next().unwrap();
            trace!(&tree);
            if !this_is_sorted && (min != xs[i - 1] || max != xs[i + k - 1]) {
                ans += 1;
                trace!(ans);
            }
            tree.remove(&xs[i - 1]);
            tree_rev.remove(&-xs[i - 1]);
        }
    }

    if any_sorted {
        ans += 1;
    }
    put!(ans);

}

use std::io::{self, Write};
use std::str::FromStr;
use std::collections::VecDeque;

struct Scanner { stdin: io::Stdin, buffer: VecDeque<String>, }
impl Scanner {
    fn new() -> Self { Scanner { stdin: io::stdin(), buffer: VecDeque::new() } }
    fn cin<T: FromStr>(&mut self) -> T {
        while self.buffer.len() == 0 {
            let mut line = String::new();
            let _ = self.stdin.read_line(&mut line);
            for w in line.split_whitespace() {
                self.buffer.push_back(String::from(w));
            }
        }
        self.buffer.pop_front().unwrap().parse::<T>().ok().unwrap()
    }
}


Measured memory_bytes: 18644