#include <bits/stdc++.h>
 
#define int long long
 
#define pii pair<int, int>
 
#define x1 x1228
#define y1 y1228
 
#define left left228
#define right right228
 
#define pb push_back
#define eb emplace_back
 
#define mp make_pair                                                                
                                                                                                                                        
#define ff first                                                                  
#define ss second   
 
#define matr vector<vector<int> > 
 
#define all(x) x.begin(), x.end()
 
 
using namespace std;
typedef long long ll; 
typedef long double ld; 
                                                                                                   
const int maxn = 3e5 + 7, mod = 998244353, inf = 1e18, MAXN = 1e6 + 7;
const double eps = 1e-9;
mt19937 rnd(time(0));
int n, k; 
int cnt[maxn]; 
int len = 1; 
vector<pii> tree; 
 
void build() {
    while (len < n) len <<= 1; 
    tree.resize(len + len - 1, {inf, -inf}); 
    for (int i = 0; i < n; ++i) {
        tree[i + len - 1] = {cnt[i], cnt[i]};         
    }
    for (int i = len - 2; i >= 0; --i) {
        tree[i].ff = min(tree[i * 2 + 1].ff, tree[i * 2 + 2].ff); 
        tree[i].ss = max(tree[i * 2 + 1].ss, tree[i * 2 + 2].ss); 
    } 
}
 
pii get(int l, int r, int l1, int r1, int rt) {
    if (l >= l1 && r <= r1) return tree[rt];
    if (max(l, l1) > min(r, r1)) return mp(inf, -inf);
    int t = (l + r) / 2; 
    pii p1 = get(l, t, l1, r1, rt * 2 + 1); 
    pii p2 = get(t + 1, r, l1, r1, rt * 2 + 2); 
    pii ans = {min(p1.ff, p2.ff), max(p1.ss, p2.ss)};
    return ans; 
}
 
int len1[maxn]; 
int ln1[maxn]; 
int len2[maxn]; 
int ln2[maxn]; 
 
int binn(int idx) {
    int l = 0;
    int m;
    int r = min(k, len2[idx]) + 1;
    while (l + 1 < r) {
        m = (l + r) / 2; 
        int idd = idx - m + 1; 
        if (get(0, len - 1, idx - k + 1, idd - 1, 0).ss > cnt[idd]) r = m;
        else l = m; 
    }        
    return l; 
}
 
int binn1(int idx) {
    int l = 0;
    int m; 
    int r = min(k, len1[idx]) + 1; 
    while (l + 1 < r) {
        m = (l + r) / 2; 
        int idd = idx + m - 1; 
        if (get(0, len - 1, idd + 1, idx + k - 1, 0).ff < cnt[idd]) r = m;
        else l = m; 
    }
    return l; 
}
 
void solve() { 
    cin >> n >> k; 
    for (int i = 0; i < n; ++i) cin >> cnt[i];
    build(); 
    len1[n - 1] = 1; 
    for (int i = n - 2; i >= 0; --i) {
        if (cnt[i] < cnt[i + 1]) len1[i] = len1[i + 1] + 1; 
        else len1[i] = 1; 
    }
    for (int i = n - k; i >= 0; --i) {
        ln1[i + k - 1] = binn1(i); 
    }
    len2[0] = 1; 
    for (int i = 1; i < n; ++i) {
        if (cnt[i] > cnt[i - 1]) len2[i] = len2[i - 1] + 1; 
        else len2[i] = 1; 
    }
    for (int i = k - 1; i < n; ++i) {
        ln2[i] = binn(i);         
    }
    set<pii> have; 
    bool fl = false; 
    for (int i = k - 1; i < n; ++i) {
        if (ln1[i] == k) {
            fl = true;
            continue; 
        }
        have.insert(mp(i - k + ln1[i] + 1, i - ln2[i]));
    }    
    cout << have.size() + fl;     
}                                   
   
signed main() {
#ifdef LOCAL
    freopen("TASK.in", "r", stdin);
    freopen("TASK.out", "w", stdout);
#else 
    
#endif // LOCAL
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.precision(20); 
    cout << fixed; 
    int t = 1; 
    for (int i = 0; i < t; ++i) {              
        solve();
    }
    return 0;
}  

Measured memory_bytes: 31360