// yukicoder: No.497 入れ子の箱 // 2019.7.14 bal4u #include #include #if 1 #define gc() getchar_unlocked() #else #define gc() getchar() #endif int in() { // 非負整数の入力 int n = 0, c = gc(); do n = 10 * n + (c & 0xf); while ((c = gc()) >= '0'); return n; } #define MAX 1005 #define CMAX 100000000 typedef struct { int x, y, z, id; } T; T t[MAX+5], s[MAX+5]; int N; int dp[MAX+5]; int c[2][CMAX+5]; void update(int f, int x, int v) { while (x <= CMAX) { if (c[f][x] < v) c[f][x] = v; x += x & (-x); } } int getmax(int f, int x) { int a = 0; while (x > 0) { if (a < c[f][x]) a = c[f][x]; x -= x & (-x); } return a; } void clear(int f, int x) { while (x <= CMAX) { c[f][x] = 0; x += x & (-x); } } int cmp(const void *a, const void *b) { return ((T *)a)->x - ((T *)b)->x; } int cmp2(const void *a, const void *b) { int t = ((T *)a)->y - ((T *)b)->y; if (t) return t; return ((T *)b)->z - ((T *)a)->z; } void cdq(int l, int r) { int i, k, a, tx, m; if (l == r) { if (dp[l] == 0) dp[l] = 1; return; } m = (l + r) >> 1; tx = t[m + 1].x; cdq(l, m); k = 0; for (i = l; i <= r; i++) s[k] = t[i], s[k++].id = i; qsort(s, k, sizeof(T), cmp2); for (i = 0; i < k; i++) { if (s[i].id <= m) { update(1, s[i].z, dp[s[i].id]); if (s[i].x != tx) update(0, s[i].z, dp[s[i].id]); } else { if (s[i].x == tx) a = getmax(0, s[i].z - 1); else a = getmax(1, s[i].z - 1); if (dp[s[i].id] < a + 1) dp[s[i].id] = a + 1; } } for (i = 0; i < k; i++) { if (s[i].id <= m) { clear(1, s[i].z); if (s[i].x != tx) clear(0, s[i].z); } } cdq(m + 1, r); } int main() { int i, ans; N = in(); for (i = 1; i <= N; i++) { int tmp, x = in(), y = in(), z = in(); if (x > y) tmp = x, x = y, y = tmp; if (y > z) { tmp = y, y = z, z = tmp; if (x > y) tmp = x, x = y, y = tmp; } t[i].x = x, t[i].y = y, t[i].z = z; } qsort(t+1, N, sizeof(T), cmp); if (t[1].x == t[N].x) ans = 1; else { cdq(1, N); ans = 0; for (i = 1; i <= N; i++) if (dp[i] > ans) ans = dp[i]; } printf("%d\n", ans); return 0; }