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 = 100000;
  struct nd {
    int x, y, c;
  } a[max_n];
  int x[max_n], y[max_n], c[max_n], tmp[max_n], n, ans = 0;
  priority_queue<int> q1;
  priority_queue<int, vector<int>, greater<int> > q2;
  void lsh(int *x) {
    for (int i = 0; i < n; i++) tmp[i] = x[i];
    sort(tmp, tmp + n);
    int nn = unique(tmp, tmp + n) - tmp;
    for (int i = 0; i < n; i++) {
      x[i] = lower_bound(tmp, tmp + nn, x[i]) - tmp;
    }
  }
  int solve(int *p, int ca) {
    while (!q1.empty()) q1.pop();
    while (!q2.empty()) q2.pop();
    for (int i = 0, j = 0; i < n; i = j) {
      while (j < n && a[j].x == a[i].x) {
        if (a[j].c == p[0]) {
          ca--;
          while (q1.size() > ca) q1.pop();
          while (q2.size() > ca) q2.pop();
        } else if (a[j].c == p[1]) {
          q1.push(a[j].y);
          while (q1.size() > ca) q1.pop();
        } else {
          q2.push(a[j].y);
          while (q2.size() > ca) q2.pop();
        }
        j++;
      }
      if (ca && q1.size() == ca && q2.size() == ca && q1.top() < q2.top())
        return ca;
    }
    return 0;
  }
  inline void chmax(int &a, int b) {
    if (a < b) a = b;
  }
  void bigsolve(int *x, int *y, int ty = 1) {
    int p[3] = {0, 1, 2}, cc[3] = {0, 0, 0};
    for (int i = 0; i < n; i++)
      a[i].x = x[i], a[i].y = y[i], cc[a[i].c = c[i] - 1]++;
    sort(a, a + n, [](const nd &a, const nd &b) { return a.x < b.x; });
    do {
      chmax(ans, solve(p, cc[p[0]]));
    } while (next_permutation(p, p + 3));
    if (ty) {
      reverse(a, a + n);
      do {
        chmax(ans, solve(p, cc[p[0]]));
      } while (next_permutation(p, p + 3));
    }
  }
  signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cin >> n;
    for (int i = 0; i < n; i++) cin >> x[i] >> y[i] >> c[i];
    lsh(x), lsh(y);
    bigsolve(x, y), bigsolve(y, x);
    bigsolve(x, x, 0), bigsolve(y, y, 0);
    cout << ans * 3 << endl;
    return 0;
  }
  