from itertools import accumulate

from collections import deque



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



mi = []

Q = deque()

for i, p in enumerate(P):

    while Q and P[Q[-1]] > p:

        Q.pop()

    Q.append(i)

    if Q[0] == i - K:

        Q.popleft()

    mi.append(Q[0])



ma = []

Q = deque()

for i, p in enumerate(P):

    while Q and P[Q[-1]] < p:

        Q.pop()

    Q.append(i)

    if Q[0] == i - K:

        Q.popleft()

    ma.append(Q[0])



mi = mi[K - 1:]

ma = ma[K - 1:]



B = [mi[i] != i or ma[i + 1] != i + K for i in range(N - K)]



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 itertools import accumulate
from collections import deque

N, K, *P = list(map(int, open(0).read().split()))
mi = []
Q = deque()
for i, p in enumerate(P):
    while Q and P[Q[-1]] > p:
        Q.pop()
    Q.append(i)
    if Q[0] == i - K:
        Q.popleft()
    mi.append(Q[0])
ma = []
Q = deque()
for i, p in enumerate(P):
    while Q and P[Q[-1]] < p:
        Q.pop()
    Q.append(i)
    if Q[0] == i - K:
        Q.popleft()
    ma.append(Q[0])
mi = mi[K - 1 :]
ma = ma[K - 1 :]
B = [mi[i] != i or ma[i + 1] != i + K for i in range(N - K)]
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)

-mi = [] -Q = deque() -for i, p in enumerate(P): -    while Q and P[Q[-1]] > p: -        Q.pop() -    Q.append(i) -    if Q[0] == i - K: -        Q.popleft() -    mi.append(Q[0]) -ma = [] -Q = deque() -for i, p in enumerate(P): -    while Q and P[Q[-1]] < p: -        Q.pop() -    Q.append(i) -    if Q[0] == i - K: -        Q.popleft() -    ma.append(Q[0]) -mi = mi[K - 1 :] -ma = ma[K - 1 :] -B = [mi[i] != i or ma[i + 1] != i + K for i in range(N - K)] + + +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:]))]