#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
//#define cerr if(false) cerr
#ifdef DEBUG
#define show(...) cerr << #__VA_ARGS__ << " = ", debug(__VA_ARGS__);
#else
#define show(...) 42
#endif
using namespace std;
using ll = long long;
using pii = pair<int, int>;
template <typename T, typename S>
ostream& operator<<(ostream& os, pair<T, S> a) {
    os << '(' << a.first << ',' << a.second << ')';
    return os;
}
template <typename T>
ostream& operator<<(ostream& os, vector<T> v) {
    for (auto x : v) os << x << ' ';
    return os;
}
void debug() {
    cerr << '\n';
}
template <typename H, typename... T>
void debug(H a, T... b) {
    cerr << a;
    if (sizeof...(b)) cerr << ", ";
    debug(b...);
}
template <typename T>
vector<T> divisor(T n) {
    vector<T> res;
    for (T i = 1; i * i <= n; i++) {
        if (n % i == 0) {
            res.push_back(i);
            if (i * i != n) {
                res.push_back(n / i);
            }
        }
    }
    sort(res.begin(), res.end());
    return res;
}
template <typename T>
map<T, int> prime_factor(T n) {
    map<T, int> res;
    for (T i = 2; i * i <= n; i++) {
        while (n % i == 0) {
            res[i]++;
            n /= i;
        }
    }
    if (n != 1) {
        res[n]++;
    }
    return res;
}
template <typename T>
T gcd(T a, T b) {
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}
template <typename T>
T lcm(T a, T b) {
    return a / gcd(a, b) * b;
}

//const ll mod = 998244353;
//ll check(vector<ll> a){
//    ll ans = 0;
//    int n = (int) a.size();
//    rep(i,n)rep(j,n){
//        if(i < j){
//            ans += lcm(a[i],a[j]);
//            ans %= mod;
//        }
//    }
//    return ans;
//}
//int main(){
//    cin.tie(0);
//    ios::sync_with_stdio(false);
//    int n;
//    cin >> n;
//    vector<ll>a(n);
//    rep(i,n)cin >> a[i];
//    vector<int>num(1000000+5,0);
//    rep(i,n)num[a[i]]++;
//    for(int k = 1; k <= 1000000; k++){
//        vector<pair<ll,int>> v;
//        for(int i = k; i <= 1000000; i += k){
//            
//        }
//    }
//}
int solve(int n, int k,vector<int>a){
    set<vector<int>>st;
    for(int i = 0; i < n - k + 1; i++){
        vector<int>b,c;
        rep(j,i)b.push_back(a[j]);
        for(int j = i; j < i + k; j++){
            c.push_back(a[j]);
        }
        sort(c.begin(),c.end());
        assert(c.size() == k);
        for(int x: c)b.push_back(x);
        for(int j = i + k; j < n; j++)b.push_back(a[j]);
        assert(b.size() == n);
        st.insert(b);
    }
    return (int)st.size();
}

template<typename T> class BIT {
private:
    int n;
    vector<T> bit;
public:
    // 0_indexed で i 番目の要素に x を加える
    void add(int i, T x){
        i++;
        while(i < n){
            bit[i] += x, i += i & -i;
        }
    }
    // 0_indexed で [0,i] の要素の和(両閉区間！！)
    T sum(int i){
        i++;
        T s = 0;
        while(i > 0){
            s += bit[i], i -= i & -i;
        }
        return s;
    }
    BIT(){}
    //初期値がすべて0の場合
    BIT(int sz) : n(sz+1), bit(n, 0){}
    BIT(vector<T>& v) : n((int)v.size()+1), bit(n, 0){
        for(int i = 0; i < n-1; i++){
            add(i,v[i]);
        }
    }
    void print(){
        for(int i = 0; i < n-1; i++){
            cout<<sum(i)-sum(i-1)<< " ";
        }
        cout<<endl;
    }
    //-1スタート
    void print_sum(){
        for(int i = 0; i < n; i++){
            cout<<sum(i-1)<<" ";
        }
        cout<<endl;
    }
};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    vector<int>a(n);
    rep(i,n)cin >> a[i];
    BIT<int> bit(n);
    ll inv = 0;
    for(int i = 0; i < k; i++){
        inv += i - bit.sum(a[i]-1);
        bit.add(a[i],1);
    }
    vector<ll>b;
    b.push_back(inv);
    int zero = 0;
    if(b.back() == 0)zero++;
    int ans = 1;
    for(int i = k; i < n; i++){
        int diff = 0;
        diff +=bit.sum(n-1) - bit.sum(a[i]);
        inv += bit.sum(n-1) - bit.sum(a[i]);
        bit.add(a[i],1);
        inv -= bit.sum(a[i-k]-1);
        bit.add(a[i-k],-1);
        if(b.back() != inv)ans++;
        else if(diff > 0)ans++;
        b.push_back(inv);
        if(b.back() == 0){
            zero++;
            if(b[b.size()-2]==0)zero--;
        }
    }
    if(zero >= 2){
        ans -= zero-1;
    }
    cout << ans << endl;
}

Measured memory_bytes: 3956