#include <bits/stdc++.h>
using i64 = long long;
using u64 = unsigned long long;
using u32 = unsigned;
using u128 = unsigned __int128;
template<class Info, class Tag>
struct LazySegmentTree {
int n;
std::vector<Info> info;
std::vector<Tag> tag;
LazySegmentTree() : n(0) {}
LazySegmentTree(int n_, Info v_ = Info()) {
init(n_, v_);
}
template<class T>
LazySegmentTree(std::vector<T> init_) {
init(init_);
}
void init(int n_, Info v_ = Info()) {
init(std::vector(n_, v_));
}
template<class T>
void init(std::vector<T> init_) {
n = init_.size();
info.assign(4 << std::__lg(n), Info());
tag.assign(4 << std::__lg(n), Tag());
std::function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
info[p] = init_[l];
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
pull(p);
};
build(1, 0, n);
}
void pull(int p) {
info[p] = info[2 * p] + info[2 * p + 1];
}
void apply(int p, const Tag &v) {
info[p].apply(v);
tag[p].apply(v);
}
void push(int p) {
apply(2 * p, tag[p]);
apply(2 * p + 1, tag[p]);
tag[p] = Tag();
}
void modify(int p, int l, int r, int x, const Info &v) {
if (r - l == 1) {
info[p] = v;
return;
}
int m = (l + r) / 2;
push(p);
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(1, 0, n, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if (l >= y || r <= x) {
return Info();
}
if (l >= x && r <= y) {
return info[p];
}
int m = (l + r) / 2;
push(p);
return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
}
Info rangeQuery(int l, int r) {
return rangeQuery(1, 0, n, l, r);
}
void rangeApply(int p, int l, int r, int x, int y, const Tag &v) {
if (l >= y || r <= x) {
return;
}
if (l >= x && r <= y) {
apply(p, v);
return;
}
int m = (l + r) / 2;
push(p);
rangeApply(2 * p, l, m, x, y, v);
rangeApply(2 * p + 1, m, r, x, y, v);
pull(p);
}
void rangeApply(int l, int r, const Tag &v) {
return rangeApply(1, 0, n, l, r, v);
}
template<class F>
int findFirst(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findFirst(2 * p, l, m, x, y, pred);
if (res == -1) {
res = findFirst(2 * p + 1, m, r, x, y, pred);
}
return res;
}
template<class F>
int findFirst(int l, int r, F &&pred) {
return findFirst(1, 0, n, l, r, pred);
}
template<class F>
int findLast(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
push(p);
int res = findLast(2 * p + 1, m, r, x, y, pred);
if (res == -1) {
res = findLast(2 * p, l, m, x, y, pred);
}
return res;
}
template<class F>
int findLast(int l, int r, F &&pred) {
return findLast(1, 0, n, l, r, pred);
}
};
constexpr int inf = 1E9;
struct Tag {
bool s = false;
int v = 0;
int a = 0;
void apply(const Tag &t) & {
if (t.s) {
s = true;
v = t.v;
a = 0;
}
a += t.a;
}
};
struct Info {
int x = -inf;
int id = -1;
void apply(const Tag &t) & {
if (t.s) {
x = t.v;
id = -1;
}
x += t.a;
}
};
Info operator+(const Info &a, const Info &b) {
if (a.x >= b.x) {
return a;
} else {
return b;
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n;
std::cin >> n;
std::vector<int> a(n), b(n), c(n);
for (int i = 0; i < n; i++) {
std::cin >> a[i] >> b[i] >> c[i];
}
auto vb = b;
std::sort(vb.begin(), vb.end());
for (int i = 0; i < n; i++) {
b[i] = std::lower_bound(vb.begin(), vb.end(), b[i]) - vb.begin();
}
auto val = c;
std::sort(val.begin(), val.end());
val.erase(std::unique(val.begin(), val.end()), val.end());
for (int i = 0; i < n; i++) {
c[i] = std::lower_bound(val.begin(), val.end(), c[i]) - val.begin();
}
std::vector<int> p(n);
std::iota(p.begin(), p.end(), 0);
i64 ans = 0;
std::vector<int> g(n);
int m = val.size();
LazySegmentTree<Info, Tag> seg(n);
auto work = [&](this auto &&self, std::vector<int> p, int vl, int vr) {
std::sort(p.begin(), p.end());
if (vl == vr) {
for (auto i : p) {
ans += std::abs(val[c[i]] - val[vl]);
}
return;
}
int vm = (vl + vr) / 2;
std::sort(p.begin(), p.end(),
[&](int i, int j) {
if (a[i] != a[j]) {
return a[i] < a[j];
}
return b[i] < b[j];
});
seg.rangeApply(0, n, {true, -inf});
for (auto i : p) {
int v = c[i] <= vm ? 1 : -1;
auto [x, id] = seg.rangeQuery(b[i], n);
if (x < 0) {
x = 0;
id = -1;
}
g[i] = id;
seg.modify(b[i], {x, i});
seg.rangeApply(0, b[i] + 1, {.a = v});
}
auto [x, lst] = seg.rangeQuery(0, n);
if (x < 0) {
x = 0;
lst = -1;
}
std::vector<int> pl, pr;
std::reverse(p.begin(), p.end());
for (auto i : p) {
if (lst != -1 && b[i] >= b[lst]) {
pl.push_back(i);
} else {
pr.push_back(i);
}
if (i == lst) {
lst = g[i];
}
}
self(pl, vl, vm);
self(pr, vm + 1, vr);
};
work(p, 0, m - 1);
std::cout << ans << "\n";
return 0;
}