#include <iostream>
#include <fstream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <bitset>
#include <algorithm>
#include <complex>
#include <array>
using namespace std;
 
#define REP(i,n) for(int i=0; i<n; ++i)
#define FOR(i,a,b) for(int i=a; i<=b; ++i)
#define FORR(i,a,b) for (int i=a; i>=b; --i)
#define ALL(c) (c).begin(), (c).end()
 
typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<double> VD;
typedef vector<VI> VVI;
typedef vector<VL> VVL;
typedef vector<VD> VVD;
typedef pair<int,int> P;
typedef pair<ll,ll> PL;

template<typename T> void chmin(T &a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T &a, T b) { if (a < b) a = b; }

int in() { int x; scanf("%d", &x); return x; }
ll lin() { ll x; scanf("%lld", &x); return x; }

struct SegmentTree{
    const int INF = 1e9;
    int n, width;
    VI dat;

    void init(int x){
        n = x;
        width = 1;
        while (width < n) width *= 2;
        dat.resize(2*width-1);
        REP(i,2*width-1) dat[i] = INF;
    }

    void update(int i, int x){
        i += width - 1;
        dat[i] = x;
        while (i > 0){
            i = (i - 1) / 2;
            dat[i] = min(dat[i*2+1], dat[i*2+2]);
        }
    }

    int query(int a, int b, int k, int l, int r){
        if (r <= a || b <= l) return INF;
        if (a <= l && r <= b) return dat[k];
        int vl = query(a, b, k*2+1, l, (l+r)/2);
        int vr = query(a, b, k*2+2, (l+r)/2, r);
        return min(vl, vr);
    }

    // [l, r)
    int query(int l, int r){
        return query(l, r, 0, 0, width);
    }
};

VI calc(VI p, int n, int k){
    SegmentTree seg;
    seg.init(n);
    REP(i,n){
        seg.update(i, p[i]);
    }
    deque<int> deq;
    VI left(n+1-k, -1);
    REP(i,n+1-k){
        if (deq.empty()){
            REP(j,k){
                if (seg.query(i + j, i + k) == p[i + j]){
                    deq.push_back(p[i + j]);
                }else{
                    break;
                }
            }
        }else{
            if (deq.size() == k - 1 && deq.back() < p[i + k - 1]) deq.push_back(p[i + k - 1]);
            else{
                while (!deq.empty() && deq.back() > p[i + k - 1]){
                    deq.pop_back();
                }
            }
        }
        if (deq.size() < k) left[i] = i + deq.size();
        if (!deq.empty()) deq.pop_front();
    }
    // REP(i,n) cout << p[i] << " ";
    // cout << endl;
    // REP(i,n+1-k) cout << left[i] << " ";
    // cout << endl;
    return left;
}

int main() {
    int n, k;
    cin >> n >> k;
    VI p(n);
    REP(i,n){
        p[i] = in();
    }
    VI left = calc(p, n, k);
    reverse(ALL(p));
    REP(i,n) p[i] *= -1;
    VI right = calc(p, n, k);
    reverse(ALL(right));
    REP(i,n+1-k) if (right[i] != -1) right[i] = n - 1 - right[i];
    set<P> st;
    REP(i,n+1-k) st.insert(P(left[i], right[i]));
    cout << st.size() << endl;

    // REP(i,n+1-k) cout << left[i] << " ";
    // cout << endl;
    // REP(i,n+1-k) cout << right[i] << " ";
    // cout << endl;

    return 0;
}


Measured memory_bytes: 12012