#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();
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(); };
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);
}
for (int j = 0; j < i && a[j].x < a[i].x; ++j) { // x is satisfied
// either j dominates y or j dominates z
// if j dominates y
if (a[j].y > a[i].y) {
int z = st_y.query(0, compr_y(a[j].y - 1));
if (z > a[i].z && z > a[j].z) {
ans = max(ans, a[i].x + a[j].y + z);
}
}
// if j dominates z
if (a[j].z > a[i].z) {
int y = st_z.query(0, compr_z(a[j].z - 1));
if (y > a[i].y && y > a[j].y) {
ans = max(ans, a[i].x + y + a[j].z);
}
}
}
}
cout << ans << '\n';
}