#include <bits/stdc++.h>
//#define DEBUG
using namespace std;
using u64 = uint64_t;
using bs = bitset<500000>;
bs mask;
mt19937 rd(random_device{}());
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr), cout.tie(nullptr);
int n = 1000;
#ifndef DEBUG
cin >> n;
#endif
for (int i = 0; i <= n; i++) {
mask[i] = 1;
}
vector<vector<pair<int, int>>> p(n + 1);
for (int i = 1; i <= n; i++) {
int x, y, c;
#ifdef DEBUG
x = rd() % n + 1, y = rd() % n + 1, c = rd() % n + 1;
#else
cin >> x >> y >> c;
#endif
p[x].push_back({y, c});
}
struct Val {
bs s;
int x, n;
u64 sum;
void add(int c) {
bs ns = s | (s << c) | (s >> (n - c)), tmp = ns ^ s;
for (int i = tmp._Find_first(); i < n; i = tmp._Find_next(i)) {
sum += i;
}
s = ns;
}
bool full() const { return sum == n * (n - 1ull) / 2; }
};
vector<Val> f = {Val{1, 0, n, 0}};
auto calc = [&](int l, int r) { return (l + r) * (r - l + 1ll) / 2; };
auto query = [&]() -> u64 {
u64 res = 0;
for (int i = 0; i + 1 < (int)f.size(); i++) {
res += f[i].sum * calc(f[i].x, f[i + 1].x - 1);
}
res += f.back().sum * calc(f.back().x, n);
return res;
};
u64 ans = 0;
int tag = 0, mxc = 0;
for (int i = 1; i <= n; i++) {
sort(p[i].begin(), p[i].end());
for (auto [y, c] : p[i]) {
if (!f.back().full() || y < f.back().x) {
for (int j = 0; j < (int)f.size(); j++) {
if (j + 1 == (int)f.size() || f[j + 1].x > y) {
f.insert(f.begin() + j + 1, f[j]);
f[j + 1].x = y;
for (int k = j + 1; k < (int)f.size(); k++) {
f[k].add(c);
++tag;
}
while ((int)f.size() >= 2 && f.end()[-2].full()) {
f.pop_back();
}
break;
}
}
}
}
mxc = max(mxc, (int)f.size());
ans += query() * i;
}
cerr << tag << ' ' << mxc << '\n';
cout << ans << '\n';
return 0;
}