#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#define ll long long
#define all(v) v.begin(),v.end()
#define sz(v) ((ll)v.size())
#define V vector
#define vi V<int>
#define vll V<ll>
#define eb emplace_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
#define A array
#define pb push_back
#define mset multiset
#define gpumap __gnu_pbds::gp_hash_table
#define ccumap __gnu_pbds::cc_hash_table
#define ui unsigned int
#define ull unsigned ll
#define i128 __int128
#define cerr if (test) cerr
#define freopen if (test) freopen
#define whtest if (test)
using namespace std;
const int test = 0;
namespace jhsy {
template<class F,class Cmp>
void solve(vi &vec,int d,vi &cnt,int m,F &&f,Cmp &&cmp) {
if (sz(vec) == 1) {
return;
}
if (d == m) {
return f(vec,cnt);
}
const int mid = sz(vec)/2;
vi vl(vec.begin(),vec.begin()+mid),vr(vec.begin()+mid,vec.end());
solve(vl,d,cnt,m,f,cmp); solve(vr,d,cnt,m,f,cmp);
merge(all(vl),all(vr),vec.begin(),cmp(d));
for (auto x:vr) {
cnt[x]++;
}
auto tmp = vec;
solve(tmp,d+1,cnt,m,f,cmp);
for (auto x:vr) {
cnt[x]--;
}
}
template<class F,class Cmp>
void solve(int n,int m,F &&f,Cmp &&cmp) {
if (!n || !m) {
return;
}
vi vec(n),cnt(n);
iota(all(vec),0);
sort(all(vec),cmp(0));
solve(vec,1,cnt,m,f,cmp);
}
const int D = 4;
void main() {
int n;
cin >> n;
V<A<int,D>> a(n);
for (auto &p:a) {
for (auto &x:p) {
cin >> x;
}
}
ll ans = 0;
solve(n,D,[&](const vi &vec,const vi &cnt) {
int k = 0;
for (auto x:vec) {
if (!cnt[x]) {
k++;
}
if (cnt[x] == D-1) {
ans += k;
for (auto y:vec) {
if (y == x) {
break;
}
if (!cnt[y]) {
c[y][x]++;
}
}
}
}
},[&](int d) {
return [&,d](int x,int y) {
return a[x][d] < a[y][d];
};
});
cout << ans << '\n';
}
}
int main() {
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout << fixed << setprecision(20);
// jhsy::init();
int T = 1;
// cin >> T;
while (T--) {
jhsy::main();
}
return 0;
}