#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(), x.end()
#define IMAX INT_MAX
#define IMIN INT_MIN
#define LMAX LLONG_MAX
#define LMIN LLONG_MIN
#define MOD 1000000007
#define SIR 1000000009
struct pt {
int x = 0, y = 0, z = 0;
pt() {};
pt(int _x) : x(_x) {};
pt(int _x, int _y) : x(_x), y(_y) {};
pt(int _x, int _y, int _z) : x(_x), y(_y), z(_z) {};
void rotate() {
int ox = x, oy = y;
x = ox - oy;
y = ox + oy;
}
};
struct Fenwick {
int n, off = 0;
vi vals;
Fenwick(int _n, int _off) : n(_n) {
vals = vi(n, 0);
}
void update(int i, int d) {
i += off;
while (i < n) {
vals[i] += d;
i |= (i + 1);
}
}
int sum(int i) {
if (i < 0) return 0;
i += off;
int res = 0;
while (i >= 0) {
res += vals[i];
i = (i & (i + 1)) - 1;
}
return res;
}
int sum(int a, int b) { // sum[a,..b]
return sum(b) - sum(a - 1);
}
};
bool operator<(pt a, pt b) {
auto A = make_tuple(a.x, a.y, a.z);
auto B = make_tuple(b.x, b.y, b.z);
return A < B;
}
int dist(pt a, pt b) {
return abs(a.x - b.x) + abs(a.y - b.y) + abs(a.z - b.z);
}
int hori(pt a, pt b) {
return abs(a.x - b.x);
}
int verti(pt a, pt b) {
return abs(a.y - b.y);
}
int t, n, d, m;
vector<pt> pts;
void d1() {
ll res = 0;
deque<int> akt;
sort(all(pts));
for (int i = 0; i < n; i++) {
pt& cur = pts[i];
while (!akt.empty() && dist(cur, pts[akt.front()]) > d) {
akt.pop_front();
}
res += akt.size();
akt.pb(i);
}
cout << res << '\n';
}
void d2() {
for (auto& p : pts) p.rotate();
sort(all(pts));
int MX = 75000;
Fenwick F(MX + 1, MX);
int l = 0, r = 0;
ll res = 0;
while (l < n) {
if ((r != n) && (hori(pts[l], pts[r]) <= d)) {
res += F.sum(pts[r].y - d, pts[r].y + d);
F.update(pts[r].y, 1);
r++;
} else {
F.update(pts[l].y, -1);
l++;
}
}
cout << res << '\n';
}
int main() {
cin >> t >> n >> d >> m;
pts.resize(n);
for (int i = 0; i < n; i++) {
int x = 0, y = 0, z = 0;
if (t == 1) cin >> x;
if (t == 2) cin >> x >> y;
if (t == 3) cin >> x >> y >> z;
pts[i] = pt(x, y, z);
}
if (t == 1)
d1();
else if (t == 2)
d2();
return 0;
}