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 N = 1e5 + 10;
  int n, a[N], b[N], c[N], ans, t[3], m, id[3], lim;
  bool vis[3];
  struct Node {
    int x, y;
    bool operator<(const Node tmp) const { return x < tmp.x; }
  } p[3][N], q[3][N];
  bool try_(int l, int r) {
    int cnt = 0, pos, x = id[1];
    for (int i = 1; i <= m; i++) {
      if (q[x][i].y > r || q[x][i].y < l) {
        continue;
      }
      cnt++;
      if (cnt == lim) {
        pos = q[x][i].x;
        break;
      }
    }
    if (cnt < lim) {
      return 0;
    }
    cnt = 0, x = id[2];
    for (int i = 1; i <= m; i++) {
      if (q[x][i].y > r || q[x][i].y < l || q[x][i].x <= pos) {
        continue;
      }
      cnt++;
      if (cnt >= lim) {
        return 1;
      }
    }
    return 0;
  }
  bool work_() {
    if (try_(-1e9, p[id[0]][m - lim + 1].x - 1)) {
      return 1;
    }
    if (try_(p[id[0]][lim].x + 1, 1e9)) {
      return 1;
    }
    int pos = -1e9 - 1;
    for (int i = 0; i < 3; i++) {
      int u = id[i], cnt = 0;
      for (int j = 1; j <= m; j++) {
        if (p[u][j].x <= pos) {
          continue;
        }
        cnt++;
        if (cnt == lim) {
          pos = p[u][j].x;
          break;
        }
      }
      if (cnt < lim) {
        return 0;
      }
    }
    return 1;
  }
  bool dfs_(int x) {
    if (x > 2) {
      return work_();
    }
    bool ok = 0;
    for (int i = 0; i < 3; i++) {
      if (!vis[i]) {
        vis[i] = 1, id[x] = i;
        ok = dfs_(x + 1);
        vis[i] = 0;
        if (ok) {
          break;
        }
      }
    }
    return ok;
  }
  bool check_() {
    memset(vis, 0, sizeof(vis));
    return dfs_(0);
  }
  void solve_() {
    t[0] = t[1] = t[2] = 0, m = n / 3;
    for (int i = 1; i <= n; i++) {
      t[c[i]]++;
      p[c[i]][t[c[i]]] = (Node){a[i], b[i]};
      q[c[i]][t[c[i]]] = (Node){b[i], a[i]};
    }
    for (int i = 0; i < 3; i++) {
      sort(p[i] + 1, p[i] + m + 1);
      sort(q[i] + 1, q[i] + m + 1);
    }
    int l = 1, r = m;
    while (l < r) {
      int mid = (l + r + 1) >> 1;
      lim = mid;
      if (check_()) {
        l = mid;
      } else {
        r = mid - 1;
      }
    }
    ans = max(ans, l);
  }
  int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
      scanf("%d%d%d", &a[i], &b[i], &c[i]);
      c[i]--;
    }
    solve_();
    for (int i = 1; i <= n; i++) {
      swap(a[i], b[i]);
    }
    solve_();
    printf("%d\n", ans * 3);
  }
  