#include <bits/stdc++.h>
using namespace std;
struct triplet {
int x, y, z;
triplet() : x(0), y(0), z(0) {}
auto operator<=>(const triplet &r) const = default;
};
const int inf = 1e9;
class segment_tree {
int n;
vector<int> seg;
public:
segment_tree(int n) : n(n), seg(2 * n, -inf) {}
void apply(int i, int x) {
i += n;
for (seg[i] = max(seg[i], x), i >>= 1; i > 0; i >>= 1) {
seg[i] = max(seg[2 * i], seg[2 * i + 1]);
}
}
int query(int l, int r) {
int ans = -inf;
for (l += n, r += n + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1)
ans = max(ans, seg[l++]);
if (r & 1)
ans = max(ans, seg[--r]);
}
return ans;
}
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
vector<triplet> a(n);
vector<int> buff_y, buff_z;
for (auto &[x, y, z] : a) {
cin >> x >> y >> z;
buff_y.push_back(y), buff_y.push_back(y - 1);
buff_z.push_back(z), buff_z.push_back(z - 1);
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
n = a.size();
// list is sorted by x.
// now, we consider picking each x. then maintain a ds of values < x. for these, we only care ab y and z
// for the values in the set, maintain the best pair. add by y, maintain a segtree indexed by y of the best z values
// for each new pair added, query the segtree from 0 to y-1
sort(buff_y.begin(), buff_y.end());
sort(buff_z.begin(), buff_z.end());
buff_y.erase(unique(buff_y.begin(), buff_y.end()), buff_y.end());
buff_z.erase(unique(buff_z.begin(), buff_z.end()), buff_z.end());
auto compr_y = [&](int i) { return lower_bound(buff_y.begin(), buff_y.end(), i) - buff_y.begin(); };
auto compr_z = [&](int i) { return lower_bound(buff_z.begin(), buff_z.end(), i) - buff_z.begin(); };
int best_pair = -inf;
segment_tree st_y(buff_y.size()), st_z(buff_z.size());
auto add = [&](int y, int z) {
st_y.apply(compr_y(y), z);
st_z.apply(compr_z(z), y);
best_pair = max(best_pair, y + st_y.query(0, compr_y(y - 1))); // we dominate someone on y
// but what if someone else dominates us on y and we dominate z?
best_pair = max(best_pair, z + st_z.query(0, compr_z(z - 1)));
};
int ans = -1;
queue<triplet> q;
for (int i = 0; i < n; ++i) {
q.push(a[i]);
while (!q.empty() && q.front().x < a[i].x) {
triplet t = q.front();
q.pop();
add(t.y, t.z);
}
ans = max(ans, a[i].x + best_pair);
}
cout << ans << '\n';
}