#include "bits/stdc++.h"
using namespace std;
const int N = 1e5 + 5;
const int SN = 1 << 20;
const int MAX = 4e7;
int curnode = 0;
int val[MAX];
int lft[MAX];
int rgt[MAX];
int maxx = 0;
int maxy = 0;
int value;
int ychild[2][SN];
int query(int l , int r , int node , int ql , int qr){
if(l > qr || r < ql || node == -1){
return 0;
}
if(l >= ql && r <= qr){
return val[node];
}
int mid = (l + r) >> 1;
return query(l , mid , lft[node] , ql , qr) + query(mid + 1 , r , rgt[node] , ql , qr);
}
int queryx(int l , int r , int tnode , bool k , int x1 , int y1 , int x2 , int y2){
if(l > x2 || r < x1 || x1 > x2 || y1 > y2 || ychild[k][tnode] == -1){
return 0;
}
if(l >= x1 && r <= x2){
return query(1 , maxx , ychild[k][tnode] , y1 , y2);
}
int mid = (l + r) >> 1;
return queryx(l , mid , tnode + tnode , k , x1 , y1 , x2 , y2) + queryx(mid + 1 , r , tnode + tnode + 1 , k , x1 , y1 , x2 , y2);
}
void update(int l , int r , int node , int idx){
val[node] += value;
if(l < r){
int mid = (l + r) >> 1;
if(idx <= mid){
if(lft[node] == -1){
lft[node] = ++curnode;
lft[curnode] = -1;
rgt[curnode] = -1;
val[curnode] = 0;
}
update(l , mid , lft[node] , idx);
}
else{
if(rgt[node] == -1){
rgt[node] = ++curnode;
lft[curnode] = -1;
rgt[curnode] = -1;
val[curnode] = 0;
}
update(mid + 1 , r , rgt[node] , idx);
}
}
}
void update(int l , int r , int tnode , bool k , int idx , int idy){
if(ychild[k][tnode] == -1){
ychild[k][tnode] = ++curnode;
val[curnode] = 0;
lft[curnode] = -1;
rgt[curnode] = -1;
}
update(1 , maxx , ychild[k][tnode] , idy);
if(l < r){
int mid = (l + r) >> 1;
if(idx <= mid){
update(l , mid , tnode + tnode , k , idx , idy);
}
else{
update(mid + 1 , r , tnode + tnode + 1 , k , idx , idy);
}
}
}
int n , t;
int x[N];
int y[N];
long long ans = 0;
map < int , int > m1 , m2;
void insert(int idx){
if(x[idx] == x[idx - 1]){//vertical line with same x
value = 1;
update(1 , maxy , 1 , 0 , m2[min(y[idx] , y[idx - 1]) + 1] , m1[x[idx]]);
value = -1;
update(1 , maxy , 1 , 0 , m2[max(y[idx] , y[idx - 1])] , m1[x[idx]]);
}
else{
value = 1;
update(1 , maxy , 1 , 1 , m2[y[idx]] , m1[min(x[idx] , x[idx - 1]) + 1]);
value = -1;
update(1 , maxy , 1 , 1 , m2[y[idx]] , m1[max(x[idx] , x[idx - 1])]);
}
}
int solve(int idx){
if(x[idx] == x[idx - 1]){
return queryx(1 , maxy , 1 , 1 , m2[min(y[idx] , y[idx - 1]) + 1] , m1[1] , m2[max(y[idx] , y[idx - 1]) - 1] , m1[x[idx]]);
}
else{
return queryx(1 , maxy , 1 , 0 , m2[1] , m1[min(x[idx] , x[idx - 1]) + 1] , m2[y[idx]] , m1[max(x[idx] , x[idx - 1]) - 1]);
}
}
int main(){
freopen("mowing.in" , "r" , stdin);
freopen("mowing.out" , "w" , stdout);
m1[1];
m2[1];
memset(ychild , -1 , sizeof(ychild));
scanf("%d %d" , &n , &t);
for(int i = 1 ; i <= n ; ++i){
scanf("%d %d" , x + i , y + i);
m1[x[i]];
m1[x[i] + 1];
m1[x[i] - 1];
m2[y[i]];
m2[y[i] + 1];
m2[y[i] - 1];
}
for(auto it : m1){
m1[it.first] = ++maxx;
}
for(auto it : m2){
m2[it.first] = ++maxy;
}
for(int i = 1 ; i <= n ; ++i){
if(i - t - 1 > 1){
insert(i - t - 1);
}
if(i > 1){
ans += solve(i);
}
}
printf("%lld\n" , ans);
}
I2luY2x1ZGUgImJpdHMvc3RkYysrLmgiCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CmNvbnN0IGludCBOID0gMWU1ICsgNTsKY29uc3QgaW50IFNOID0gMSA8PCAyMDsKY29uc3QgaW50IE1BWCA9IDRlNzsKaW50IGN1cm5vZGUgPSAwOwppbnQgdmFsW01BWF07CmludCBsZnRbTUFYXTsKaW50IHJndFtNQVhdOwppbnQgbWF4eCA9IDA7CmludCBtYXh5ID0gMDsKaW50IHZhbHVlOwppbnQgeWNoaWxkWzJdW1NOXTsKaW50IHF1ZXJ5KGludCBsICwgaW50IHIgLCBpbnQgbm9kZSAsIGludCBxbCAsIGludCBxcil7CiAgICBpZihsID4gcXIgfHwgciA8IHFsIHx8IG5vZGUgPT0gLTEpewogICAgICAgIHJldHVybiAwOwogICAgfQogICAgaWYobCA+PSBxbCAmJiByIDw9IHFyKXsKICAgICAgICByZXR1cm4gdmFsW25vZGVdOwogICAgfQogICAgaW50IG1pZCA9IChsICsgcikgPj4gMTsKICAgIHJldHVybiBxdWVyeShsICwgbWlkICwgbGZ0W25vZGVdICwgcWwgLCBxcikgKyBxdWVyeShtaWQgKyAxICwgciAsIHJndFtub2RlXSAsIHFsICwgcXIpOwp9CmludCBxdWVyeXgoaW50IGwgLCBpbnQgciAsIGludCB0bm9kZSAsIGJvb2wgayAsIGludCB4MSAsIGludCB5MSAsIGludCB4MiAsIGludCB5Mil7CiAgICBpZihsID4geDIgfHwgciA8IHgxIHx8IHgxID4geDIgfHwgeTEgPiB5MiB8fCB5Y2hpbGRba11bdG5vZGVdID09IC0xKXsKICAgICAgICByZXR1cm4gMDsKICAgIH0KICAgIGlmKGwgPj0geDEgJiYgciA8PSB4Mil7CiAgICAgICAgcmV0dXJuIHF1ZXJ5KDEgLCBtYXh4ICwgeWNoaWxkW2tdW3Rub2RlXSAsIHkxICwgeTIpOwogICAgfQogICAgaW50IG1pZCA9IChsICsgcikgPj4gMTsKICAgIHJldHVybiBxdWVyeXgobCAsIG1pZCAsIHRub2RlICsgdG5vZGUgLCBrICwgeDEgLCB5MSAsIHgyICwgeTIpICsgcXVlcnl4KG1pZCArIDEgLCByICwgdG5vZGUgKyB0bm9kZSArIDEgLCBrICwgeDEgLCB5MSAsIHgyICwgeTIpOwp9CnZvaWQgdXBkYXRlKGludCBsICwgaW50IHIgLCBpbnQgbm9kZSAsIGludCBpZHgpewogICAgdmFsW25vZGVdICs9IHZhbHVlOwogICAgaWYobCA8IHIpewogICAgICAgIGludCBtaWQgPSAobCArIHIpID4+IDE7CiAgICAgICAgaWYoaWR4IDw9IG1pZCl7CiAgICAgICAgICAgIGlmKGxmdFtub2RlXSA9PSAtMSl7CiAgICAgICAgICAgICAgICBsZnRbbm9kZV0gPSArK2N1cm5vZGU7CiAgICAgICAgICAgICAgICBsZnRbY3Vybm9kZV0gPSAtMTsKICAgICAgICAgICAgICAgIHJndFtjdXJub2RlXSA9IC0xOwogICAgICAgICAgICAgICAgdmFsW2N1cm5vZGVdID0gMDsKICAgICAgICAgICAgfQogICAgICAgICAgICB1cGRhdGUobCAsIG1pZCAsIGxmdFtub2RlXSAsIGlkeCk7CiAgICAgICAgfQogICAgICAgIGVsc2V7CiAgICAgICAgICAgIGlmKHJndFtub2RlXSA9PSAtMSl7CiAgICAgICAgICAgICAgICByZ3Rbbm9kZV0gPSArK2N1cm5vZGU7CiAgICAgICAgICAgICAgICBsZnRbY3Vybm9kZV0gPSAtMTsKICAgICAgICAgICAgICAgIHJndFtjdXJub2RlXSA9IC0xOwogICAgICAgICAgICAgICAgdmFsW2N1cm5vZGVdID0gMDsKICAgICAgICAgICAgfQogICAgICAgICAgICB1cGRhdGUobWlkICsgMSAsIHIgLCByZ3Rbbm9kZV0gLCBpZHgpOwogICAgICAgIH0KICAgIH0KfQp2b2lkIHVwZGF0ZShpbnQgbCAsIGludCByICwgaW50IHRub2RlICwgYm9vbCBrICwgaW50IGlkeCAsIGludCBpZHkpewogICAgaWYoeWNoaWxkW2tdW3Rub2RlXSA9PSAtMSl7CiAgICAgICAgeWNoaWxkW2tdW3Rub2RlXSA9ICsrY3Vybm9kZTsKICAgICAgICB2YWxbY3Vybm9kZV0gPSAwOwogICAgICAgIGxmdFtjdXJub2RlXSA9IC0xOwogICAgICAgIHJndFtjdXJub2RlXSA9IC0xOwogICAgfQogICAgdXBkYXRlKDEgLCBtYXh4ICwgeWNoaWxkW2tdW3Rub2RlXSAsIGlkeSk7CiAgICBpZ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AgaWYoeFtpZHhdID09IHhbaWR4IC0gMV0pewogICAgICAgIHJldHVybiBxdWVyeXgoMSAsIG1heHkgLCAxICwgMSAsIG0yW21pbih5W2lkeF0gLCB5W2lkeCAtIDFdKSArIDFdICwgbTFbMV0gLCBtMlttYXgoeVtpZHhdICwgeVtpZHggLSAxXSkgLSAxXSAsIG0xW3hbaWR4XV0pOwogICAgfQogICAgZWxzZXsKICAgICAgICByZXR1cm4gcXVlcnl4KDEgLCBtYXh5ICwgMSAsIDAgLCBtMlsxXSAsIG0xW21pbih4W2lkeF0gLCB4W2lkeCAtIDFdKSArIDFdICwgbTJbeVtpZHhdXSAsIG0xW21heCh4W2lkeF0gLCB4W2lkeCAtIDFdKSAtIDFdKTsKICAgIH0KfQppbnQgbWFpbigpewogICAgZnJlb3BlbigibW93aW5nLmluIiAsICJyIiAsIHN0ZGluKTsKICAgIGZyZW9wZW4oIm1vd2luZy5vdXQiICwgInciICwgc3Rkb3V0KTsKICAgIG0xWzFdOwogICAgbTJbMV07CiAgICBtZW1zZXQoeWNoaWxkICwgLTEgLCBzaXplb2YoeWNoaWxkKSk7CiAgICBzY2FuZigiJWQgJWQiICwgJm4gLCAmdCk7CiAgICBmb3IoaW50IGkgPSAxIDsgaSA8PSBuIDsgKytpKXsKICAgICAgICBzY2FuZigiJWQgJWQiICwgeCArIGkgLCB5ICsgaSk7CiAgICAgICAgbTFbeFtpXV07CiAgICAgICAgbTFbeFtpXSArIDFdOwogICAgICAgIG0xW3hbaV0gLSAxXTsKICAgICAgICBtMlt5W2ldXTsKICAgICAgICBtMlt5W2ldICsgMV07CiAgICAgICAgbTJbeVtpXSAtIDFdOwogICAgfQogICAgZm9yKGF1dG8gaXQgOiBtMSl7CiAgICAgICAgbTFbaXQuZmlyc3RdID0gKyttYXh4OwogICAgfQogICAgZm9yKGF1dG8gaXQgOiBtMil7CiAgICAgICAgbTJbaXQuZmlyc3RdID0gKyttYXh5OwogICAgfQogICAgZm9yKGludCBpID0gMSA7IGkgPD0gbiA7ICsraSl7CiAgICAgICAgaWYoaSAtIHQgLSAxID4gMSl7CiAgICAgICAgICAgIGluc2VydChpIC0gdCAtIDEpOwogICAgICAgIH0KICAgICAgICBpZihpID4gMSl7CiAgICAgICAgICAgIGFucyArPSBzb2x2ZShpKTsKICAgICAgICB9CiAgICB9CiAgICBwcmludGYoIiVsbGRcbiIgLCBhbnMpOwp9