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 MX = 100005, inf = 2e9;
  int n;
  vector<vector<int> > a, b;
  bool f(vector<vector<int> > &v, vector<int> &ord, int m, int d, int lvl = 0,
         int x1 = -inf, int y1 = -inf, int x2 = inf, int y2 = inf) {
    if (lvl == 3) return 1;
    int cn = 0;
    for (int i = (d == 1 ? 0 : n - 1); 0 <= i && i < n; i += d)
      if (v[i][2] == ord[lvl]) {
        if (v[i][0] < x1 || x2 < v[i][0]) continue;
        if (v[i][1] < y1 || y2 < v[i][1]) continue;
        cn++;
        if (cn >= m) {
          int nx1 = &v == &a && d == 1 ? v[i][0] + 1 : x1;
          int ny1 = &v == &b && d == 1 ? v[i][1] + 1 : y1;
          int nx2 = &v == &a && d == -1 ? v[i][0] - 1 : x2;
          int ny2 = &v == &b && d == -1 ? v[i][1] - 1 : y2;
          if (f(&v == &b ? a : b, ord, m, 1, lvl + 1, nx1, ny1, nx2, ny2))
            return 1;
          if (lvl) break;
          if (f(v, ord, m, 1, lvl + 1, nx1, ny1, nx2, ny2)) return 1;
          break;
        }
      }
    return 0;
  }
  void main_() {
    cin >> n;
    for (int i = 0; i < int(n); i++) {
      int x, y, c;
      cin >> x >> y >> c;
      a.push_back({x, y, c});
      b.push_back({x, y, c});
    }
    sort(a.begin(), a.end(),
         [&](const vector<int> &x, const vector<int> &y) { return x[0] < y[0]; });
    sort(b.begin(), b.end(),
         [&](const vector<int> &x, const vector<int> &y) { return x[1] < y[1]; });
    int i = 0, j = n / 3, rep = 18;
    while (rep--) {
      int m = (i + j + 1) / 2, esPos = 0;
      vector<int> ord = {1, 2, 3};
      do {
        esPos |= f(a, ord, m, 1);
        esPos |= f(a, ord, m, -1);
        esPos |= f(b, ord, m, 1);
        esPos |= f(b, ord, m, -1);
      } while (next_permutation(ord.begin(), ord.end()));
      if (esPos)
        i = m;
      else
        j = m;
    }
    cout << 3 * i << '\n';
  }
  int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int t = 1;
    while (t--) main_();
    return 0;
  }
  