#include <bits/stdc++.h>   using namespace std;   namespace my_template {   const long double PI =       3.14159265358979323846264338327950288419716939937510582097494L;   template <typename T>   pair<T, T> operator+(const pair<T, T> &a, const pair<T, T> &b) {     return pair<T, T>(a.first + b.first, a.second + b.second);   }   template <typename T>   pair<T, T> operator-(const pair<T, T> &a, const pair<T, T> &b) {     return pair<T, T>(a.first - b.first, a.second - b.second);   }   template <typename T>   T operator*(const pair<T, T> &a, const pair<T, T> &b) {     return (a.first * b.first + a.second * b.second);   }   template <typename T>   T operator^(const pair<T, T> &a, const pair<T, T> &b) {     return (a.first * b.second - a.second * b.first);   }   template <typename T>   void print(vector<T> vec, string name =   ) {     cout << name;     for (auto u : vec) cout << u <<    ;     cout <<   n ;   }   }  // namespace my_template   using namespace my_template;   const int MOD = 1000000007;   const long long INF = std::numeric_limits<long long>::max();   const int MX = 500101;   int N;   vector<pair<int, int>> sk;   vector<int> color;   struct node {     int l, r;     int did;     int lazy;     int dp, seg;     node *left = nullptr;     node *right = nullptr;     node(int a, int b) : l(a), r(b), did(0), lazy(0), dp(0), seg(0) {       if (l != r) {         left = new node(l, (l + r) / 2);         right = new node((l + r) / 2 + 1, r);       }     }     inline void push() {       if (!lazy) return;       did += lazy;       if (l != r) {         left->lazy += lazy;         right->lazy += lazy;       } else {         seg += lazy;         assert(did == dp + seg);       }       lazy = 0;     }     void upd(int pos, int val) {       push();       if (l == r) {         dp = max(dp, val);         did = dp + seg;         return;       } else if (pos <= (l + r) / 2)         left->upd(pos, val);       else         right->upd(pos, val);       left->push();       right->push();       did = max(left->did, right->did);     }     void add(int a, int b, int val) {       push();       if (r < a or b < l)         return;       else if (a <= l and r <= b) {         lazy += val;         push();         return;       } else {         left->add(a, b, val);         right->add(a, b, val);         left->push();         right->push();         did = max(left->did, right->did);       }     }     int get(int a, int b) {       push();       if (r < a or b < l)         return 0;       else if (a <= l and r <= b) {         return did;       } else {         return max(left->get(a, b), right->get(a, b));       }     }   };   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> N;     {       vector<pair<pair<int, int>, int>> temp(N);       for (int i = 0; i < N; ++i) {         cin >> temp[i].first.first >> temp[i].first.second >> temp[i].second;       }       sort(temp.begin(), temp.end(),            [](const pair<pair<int, int>, int> &a,               const pair<pair<int, int>, int> &b) {              return a.first.second < b.first.second or                     (a.first.second == b.first.second and                      a.first.first > b.first.first);            });       sk.resize(N);       color.resize(N);       for (int i = 0; i < N; ++i) {         sk[i] = temp[i].first;         color[i] = temp[i].second - 1;       }     }     int big = 0;     {       map<int, int> rem;       for (int i = 0; i < N; ++i) {         rem[sk[i].first] = 1;         rem[sk[i].second] = 1;       }       int piv = 1;       for (auto &&u : rem) u.second = piv++;       for (int i = 0; i < N; ++i) {         sk[i].first = rem[sk[i].first];         sk[i].second = rem[sk[i].second];         big = max(big, sk[i].second);       }     }     node *dp[2];     dp[0] = new node(0, big + 10);     dp[1] = new node(0, big + 10);     int ats = 0;     for (int i = 0; i < N; ++i) {       int c = color[i];       int first = sk[i].first;       int second = sk[i].second;       int curr = dp[1 - c]->get(0, first - 1) + 1;       dp[1 - c]->add(0, first - 1, 1);       dp[c]->upd(second, curr);       ats = max(ats, curr);     }     printf( %d n , ats);   }