description: On an endless checkered sheet of paper, n cells are chosen and colored in three colors, where n is divisible by 3. It turns out that there are exactly n/3 marked cells of each of three colors! 
  
  Find the largest such k that it's possible to choose k/3 cells of each color, remove all other marked cells, and then select three rectangles with sides parallel to the grid lines so that the following conditions hold:
  
    * No two rectangles can intersect (but they can share a part of the boundary). In other words, the area of intersection of any two of these rectangles must be 0.
    * The i-th rectangle contains all the chosen cells of the i-th color and no chosen cells of other colors, for i = 1, 2, 3. 
  
  Input
  
  The first line of the input contains a single integer n — the number of the marked cells (3 ≤ n ≤ 10^5, n is divisible by 3).
  
  The i-th of the following n lines contains three integers x_i, y_i, c_i (|x_i|,|y_i| ≤ 10^9; 1 ≤ c_i ≤ 3), where (x_i, y_i) are the coordinates of the i-th marked cell and c_i is its color.
  
  It's guaranteed that all cells (x_i, y_i) in the input are distinct, and that there are exactly n/3 cells of each color.
  
  Output
  
  Output a single integer k — the largest number of cells you can leave.
  
  Examples
  
  Input
  
  
  9
  2 3 1
  4 1 2
  2 1 3
  3 4 1
  5 3 2
  4 4 3
  2 4 1
  5 2 2
  3 5 3
  
  
  Output
  
  
  6
  
  
  Input
  
  
  3
  1 1 1
  2 2 2
  3 3 3
  
  
  Output
  
  
  3
  
  Note
  
  In the first sample, it's possible to leave 6 cells with indexes 1, 5, 6, 7, 8, 9.
  
  In the second sample, it's possible to leave 3 cells with indexes 1, 2, 3.
solution: #include <bits/stdc++.h>
  using namespace std;
  const int max_n = 100111, inf = 1000111222;
  struct segment_tree {
    int sum[4 * max_n][2];
    void clr() { memset(sum, 0, sizeof(sum)); }
    void update(int v, int l, int r, int x, int c) {
      if (l == r) {
        ++sum[v][c];
        return;
      }
      int mid = (l + r) / 2;
      if (x <= mid) {
        update(2 * v, l, mid, x, c);
      } else {
        update(2 * v + 1, mid + 1, r, x, c);
      }
      sum[v][0] = sum[2 * v][0] + sum[2 * v + 1][0];
      sum[v][1] = sum[2 * v][1] + sum[2 * v + 1][1];
    }
    int get(int v, int l, int r, int add0, int add1) {
      if (l == r) {
        return max(min(add0 + sum[v][0], add1), min(add0, add1 + sum[v][1]));
      }
      int mid = (l + r) / 2;
      if (add0 + sum[2 * v][0] <= add1 + sum[2 * v + 1][1]) {
        return get(2 * v + 1, mid + 1, r, add0 + sum[2 * v][0], add1);
      }
      return get(2 * v, l, mid, add0, add1 + sum[2 * v + 1][1]);
    }
  };
  struct Point {
    int x, y, c;
    bool operator<(const Point &p) const { return x < p.x; }
  };
  int n;
  Point p[max_n];
  vector<int> vx, vy;
  segment_tree st;
  int compress(const vector<int> &v, int x) {
    return lower_bound(v.begin(), v.end(), x) - v.begin();
  }
  int solve() {
    sort(p, p + n);
    vector<int> perm{0, 1, 2};
    int res = 0;
    do {
      st.clr();
      int c3 = n / 3;
      for (int i = 0; i < n;) {
        int pos = i;
        while (i < n && p[i].x == p[pos].x) {
          if (p[i].c == perm[2]) {
            --c3;
          } else {
            int tp = (p[i].c == perm[1]);
            st.update(1, 0, n - 1, p[i].x, tp);
          }
          ++i;
        }
        res = max(res, min(c3, st.get(1, 0, n - 1, 0, 0)));
      }
      for (int it = 0; it < 2; ++it) {
        st.clr();
        int c3 = n / 3;
        for (int i = 0; i < n;) {
          int pos = i;
          while (i < n && p[i].x == p[pos].x) {
            if (p[i].c == perm[2]) {
              --c3;
            } else {
              int tp = (p[i].c == perm[1]);
              st.update(1, 0, n - 1, p[i].y, tp);
            }
            ++i;
          }
          res = max(res, min(c3, st.get(1, 0, n - 1, 0, 0)));
        }
        for (int i = 0; i < n; ++i) {
          p[i].x = n - 1 - p[i].x;
        }
        reverse(p, p + n);
      }
    } while (next_permutation(perm.begin(), perm.end()));
    return res;
  }
  int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n;
    for (int i = 0; i < n; ++i) {
      cin >> p[i].x >> p[i].y >> p[i].c;
      --p[i].c;
      vx.push_back(p[i].x);
      vy.push_back(p[i].y);
    }
    sort(vx.begin(), vx.end());
    sort(vy.begin(), vy.end());
    vx.erase(unique(vx.begin(), vx.end()), vx.end());
    vy.erase(unique(vy.begin(), vy.end()), vy.end());
    for (int i = 0; i < n; ++i) {
      p[i].x = compress(vx, p[i].x);
      p[i].y = compress(vy, p[i].y);
    }
    int ans = 0;
    for (int it = 0; it < 2; ++it) {
      ans = max(ans, solve());
      for (int i = 0; i < n; ++i) {
        swap(p[i].x, p[i].y);
      }
    }
    cout << ans * 3 << "\n";
    return 0;
  }
  