from itertools import accumulate

from collections import deque



N, K, *P = list(map(int, open(0).read().split()))



def slide(A, K, cmp=lambda a, b: a > b):

    res = []

    Q = deque()

    for i, a in enumerate(A):

        while Q and cmp(A[Q[-1]], a):

            Q.pop()

        Q.append(i)

        if Q[0] == i - K:

            Q.popleft()

        res.append(Q[0])

    return res[K - 1:]



mi = slide(P, K)

ma = slide(P, K, lambda a, b: a < b)



B = [mi != i or ma != i + K for i, (mi, ma) in enumerate(zip(mi, ma[1:]))]



A = [0] + list(accumulate(x < y for x, y in zip(P, P[1:])))

S = [y - x != K - 1 for x, y in zip(A, A[K - 1:])]



ans = 1 + sum(b for b, s in zip(B, S[1:]) if s)

if S[0] and not all(S):

    ans += 1

print(ans)
from collections import deque

from itertools import accumulate



def slide_min(A, K):

    res = []

    Q = deque()

    for i, a in enumerate(A):

        while Q and A[Q[-1]] > a:

            Q.pop()

        Q.append(i)

        if Q[0] == i - K:

            Q.popleft()

        res.append(Q[0])

    return res[K - 1:]



N, K, *P = list(map(int, open(0).read().split()))



mi = slide_min(P, K)

ma = slide_min([-p for p in P], K)



B = [mi != i or ma != i + K for i, (mi, ma) in enumerate(zip(mi, ma[1:]))]



A = [0] + list(accumulate(x < y for x, y in zip(P, P[1:])))

S = [y - x != K - 1 for x, y in zip(A, A[K - 1:])]



ans = 1 + sum(b for b, s in zip(B, S[1:]) if s)

if S[0] and not all(S):

    ans += 1

print(ans)
from itertools import accumulate
from collections import deque

N, K, *P = list(map(int, open(0).read().split()))


def slide(A, K, cmp=lambda a, b: a > b):
    res = []
    Q = deque()
    for i, a in enumerate(A):
        while Q and cmp(A[Q[-1]], a):
            Q.pop()
        Q.append(i)
        if Q[0] == i - K:
            Q.popleft()
        res.append(Q[0])
    return res[K - 1 :]


mi = slide(P, K)
ma = slide(P, K, lambda a, b: a < b)
B = [mi != i or ma != i + K for i, (mi, ma) in enumerate(zip(mi, ma[1:]))]
A = [0] + list(accumulate(x < y for x, y in zip(P, P[1:])))
S = [y - x != K - 1 for x, y in zip(A, A[K - 1 :])]
ans = 1 + sum(b for b, s in zip(B, S[1:]) if s)
if S[0] and not all(S):
    ans += 1
print(ans)

from collections import deque
from itertools import accumulate


def slide_min(A, K):
    res = []
    Q = deque()
    for i, a in enumerate(A):
        while Q and A[Q[-1]] > a:
            Q.pop()
        Q.append(i)
        if Q[0] == i - K:
            Q.popleft()
        res.append(Q[0])
    return res[K - 1 :]


N, K, *P = list(map(int, open(0).read().split()))
mi = slide_min(P, K)
ma = slide_min([-p for p in P], K)
B = [mi != i or ma != i + K for i, (mi, ma) in enumerate(zip(mi, ma[1:]))]
A = [0] + list(accumulate(x < y for x, y in zip(P, P[1:])))
S = [y - x != K - 1 for x, y in zip(A, A[K - 1 :])]
ans = 1 + sum(b for b, s in zip(B, S[1:]) if s)
if S[0] and not all(S):
    ans += 1
print(ans)

+from collections import deque -from collections import deque - -N, K, *P = list(map(int, open(0).read().split())) -def slide(A, K, cmp=lambda a, b: a > b): +def slide_min(A, K): -        while Q and cmp(A[Q[-1]], a): +        while Q and A[Q[-1]] > a: -mi = slide(P, K) -ma = slide(P, K, lambda a, b: a < b) +N, K, *P = list(map(int, open(0).read().split())) +mi = slide_min(P, K) +ma = slide_min([-p for p in P], K)