/// @file ss1.cc
/// @brief ss1 の実装ファイル
/// @author Yusuke Matsunaga (松永 裕介)
///
/// Copyright (C) 2019 Yusuke Matsunaga
/// All rights reserved.


#include <iostream>
#include <vector>

using namespace std;

bool debug_deq = false;

class Deq
{
public:

  Deq(int max_num,
      const vector<int>& P);

  ~Deq();

  void
  put(int i);

  int
  min();

  int
  max();

  void
  del(int i);

private:

  const vector<int>& mP;

  vector<int> mMinQueue;

  int mMinTop;

  int mMinTail;

  vector<int> mMaxQueue;

  int mMaxTop;

  int mMaxTail;

};


Deq::Deq(int max_num,
	 const vector<int>& P) :
  mP{P},
  mMinQueue(max_num),
  mMinTop{0},
  mMinTail{0},
  mMaxQueue(max_num),
  mMaxTop{0},
  mMaxTail{0}
{
}

Deq::~Deq()
{
}

void
Deq::put(int i)
{
  int val = mP[i];

  if ( debug_deq ) {
    cout << "put(P[" << i << "] = " << val << ")" << endl;
  }

  while ( mMinTop <= (mMinTail - 1) && mP[mMinQueue[mMinTail - 1]] > val ) {
    -- mMinTail;
  }
  mMinQueue[mMinTail] = i;
  ++ mMinTail;

  if ( debug_deq ) {
    cout << " min_queue = ";
    for ( int i = mMinTop; i < mMinTail; ++ i ) {
      cout << " " << mMinQueue[i];
    }
    cout << endl;
  }

  while ( mMaxTop <= (mMaxTail - 1) && mP[mMaxQueue[mMaxTail - 1]] < val ) {
    -- mMaxTail;
  }
  mMaxQueue[mMaxTail] = i;
  ++ mMaxTail;

  if ( debug_deq ) {
    cout << " max_queue = ";
    for ( int i = mMaxTop; i < mMaxTail; ++ i ) {
      cout << " " << mMinQueue[i];
    }
    cout << endl;
  }

  if ( debug_deq ) {
    cout << " min = " << min() << endl
	 << " max = " << max() << endl
	 << endl;
  }
}


int
Deq::min()
{
  if ( mMinTop >= mMinTail ) {
    abort();
  }
  return mP[mMinQueue[mMinTop]];
}

int
Deq::max()
{
  if ( mMaxTop >= mMaxTail ) {
    abort();
  }
  return mP[mMaxQueue[mMaxTop]];
}

void
Deq::del(int i)
{
  if ( debug_deq ) {
    cout << "del(P[" << i << "] = " << mP[i] << ")" << endl;
  }

  while ( mMinQueue[mMinTop] <= i ) {
    ++ mMinTop;
  }

  if ( debug_deq ) {
    cout << " min_queue = ";
    for ( int i = mMinTop; i < mMinTail; ++ i ) {
      cout << " " << mMinQueue[i];
    }
    cout << endl;
  }

  while ( mMaxQueue[mMaxTop] <= i ) {
    ++ mMaxTop;
  }

  if ( debug_deq ) {
    cout << " max_queue = ";
    for ( int i = mMaxTop; i < mMaxTail; ++ i ) {
      cout << " " << mMinQueue[i];
    }
    cout << endl;
  }

  if ( debug_deq ) {
    cout << " min = " << min() << endl
	 << " max = " << max() << endl
	 << endl;
  }
}

int
main(int argc,
     char** argv)
{
  bool debug = false;

  int n, k;
  cin >> n >> k;

  vector<int> P(n);
  for ( int i = 0; i < n; ++ i ) {
    cin >> P[i];
  }

  Deq deq(n, P);

  // 最初の K 個を調べる．
  int last_gap = -1;
  for ( int i = 0; i < k - 1; ++ i ) {
    if ( P[i] > P[i + 1] ) {
      last_gap = i;
    }
    deq.put(i + 1);
  }

  // とにかく最初のパタンは登録する．
  int num = 1;

  // 原順列と等しいパタンが有った時 true にするフラグ
  bool ident = false;
  if ( last_gap == -1 ) {
    // 先頭の K 個は整列していた．
    ident = true;
  }

  for ( int i = 1; i <= n - k; ++ i ) {
    // last_gap を更新する
    if ( P[i + k - 2] > P[i + k - 1] ) {
      last_gap = i + k - 2;
    }

    if ( last_gap < i ) {
      // 対象の区間に変化がない．
      if ( !ident ) {
	ident = true;
	++ num;
      }
    }
    else {
      bool reverse = false;
      if ( P[i - 1] > deq.min() || P[i + k - 1] < deq.max() ) {
	++ num;
      }
    }
    deq.del(i);
    deq.put(i + k - 1);
  }

  cout << num << endl;
}


Measured memory_bytes: 2560