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 + 100;
  int n, p[4], bx[N], wx, by[N], wy, sum[N][4];
  vector<int> wh[N];
  struct node {
    int x, y, c;
  } sh[N];
  bool cmp(node a, node b) { return a.x < b.x; }
  inline int read() {
    int f = 1, x = 0;
    char s = getchar();
    while (s < '0' || s > '9') {
      if (s == '-') f = -1;
      s = getchar();
    }
    while (s >= '0' && s <= '9') {
      x = x * 10 + s - '0';
      s = getchar();
    }
    return x * f;
  }
  struct bit {
    int sh[N];
    void init() { memset(sh, 0, sizeof(sh)); }
    int lowbit(int x) { return x & (-x); }
    void change(int x, int v) {
      while (x <= n) sh[x] += v, x += lowbit(x);
    }
    int query(int x) {
      int ans = 0;
      while (x) ans += sh[x], x -= lowbit(x);
      return ans;
    }
    int ask(int l, int r) { return query(r) - query(l - 1); }
  } T[4];
  int g(int l, int r, int c) { return sum[r][c] - sum[l - 1][c]; }
  void init() {
    memset(sum, 0, sizeof(sum));
    wx = wy = 0;
    for (int i = 1; i <= n; i++) bx[++wx] = sh[i].x;
    sort(bx + 1, bx + 1 + wx);
    wx = unique(bx + 1, bx + 1 + wx) - bx - 1;
    for (int i = 1; i <= n; i++)
      sh[i].x = lower_bound(bx + 1, bx + 1 + wx, sh[i].x) - bx;
    for (int i = 1; i <= n; i++) by[++wy] = sh[i].y;
    sort(by + 1, by + 1 + wy);
    wy = unique(by + 1, by + 1 + wy) - by - 1;
    for (int i = 1; i <= n; i++)
      sh[i].y = lower_bound(by + 1, by + 1 + wy, sh[i].y) - by;
    sort(sh + 1, sh + 1 + n, cmp);
    for (int i = 1; i <= n; i++) sum[sh[i].x][sh[i].c]++;
    for (int i = 1; i <= wx; i++)
      for (int j = 1; j <= 3; j++) sum[i][j] += sum[i - 1][j];
  }
  int solve() {
    for (int i = 1; i <= wx; i++) wh[i].clear();
    for (int i = 1; i <= n; i++) wh[sh[i].x].push_back(i);
    for (int i = 1; i <= 3; i++) p[i] = i;
    int ans = 1;
    do {
      for (int i = 2; i <= wx; i++) {
        int A = sum[i - 1][p[1]], l = i, r = wx - 1;
        while (l < r) {
          int mid = l + ((r - l + 1) >> 1);
          if (g(i, mid, p[2]) <= g(mid + 1, wx, p[3]))
            l = mid;
          else
            r = mid - 1;
        }
        ans = max(ans, min({A, g(i, l, p[2]), g(l + 1, wx, p[3])}));
        l++;
        ans = max(ans, min({A, g(i, l, p[2]), g(l + 1, wx, p[3])}));
      }
    } while (next_permutation(p + 1, p + 4));
    for (int i = 1; i <= 3; i++) p[i] = i;
    auto find = [](int A, int &ans) {
      int l = 1, r = wy - 1;
      while (l < r) {
        int mid = l + ((r - l + 1) >> 1);
        if (T[p[2]].ask(1, mid) <= T[p[3]].ask(mid + 1, wy))
          l = mid;
        else
          r = mid - 1;
      }
      ans = max(ans, min({A, T[p[2]].ask(1, l), T[p[3]].ask(l + 1, wy)}));
      l++;
      ans = max(ans, min({A, T[p[2]].ask(1, l), T[p[3]].ask(l + 1, wy)}));
    };
    do {
      T[p[2]].init();
      T[p[3]].init();
      for (int i = wx; i >= 1; i--) {
        int A = g(1, i, p[1]);
        find(A, ans);
        for (int j : wh[i])
          if (sh[j].c != p[1]) T[sh[j].c].change(sh[j].y, 1);
      }
    } while (next_permutation(p + 1, p + 4));
    for (int i = 1; i <= 3; i++) p[i] = i;
    do {
      T[p[2]].init();
      T[p[3]].init();
      for (int i = 1; i <= wx; i++) {
        int A = g(i, wx, p[1]);
        find(A, ans);
        for (int j : wh[i])
          if (sh[j].c != p[1]) T[sh[j].c].change(sh[j].y, 1);
      }
    } while (next_permutation(p + 1, p + 4));
    return ans;
  }
  signed main() {
    n = read();
    for (int i = 1; i <= n; i++) sh[i] = (node){read(), read(), read()};
    init();
    int ans = solve();
    for (int i = 1; i <= n; i++) swap(sh[i].x, sh[i].y);
    init();
    ans = max(ans, solve());
    printf("%d\n", ans * 3);
  }
  