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;
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  vector<vector<pair<int, int>>> p;
  vector<int> ord = {0, 1, 2};
  vector<vector<vector<pair<int, int>>>> v(3), v2(3);
  bool solveLine(int k) {
    int lst = -2e9;
    for (int i : ord) {
      bool found = false;
      for (int j = 0; j < p[i].size(); j++) {
        if (p[i][j].first > lst) {
          if (j + k - 1 >= p[i].size()) return false;
          lst = p[i][j + k - 1].first;
          found = true;
          break;
        }
      }
      if (!found) return false;
    }
    return true;
  }
  bool solveGroup(int k) {
    int to = p[ord[0]][k - 1].second;
    vector<int> a, b;
    for (int i = 0; i < p[1].size(); i++) {
      if (p[ord[1]][i].second > to) a.push_back(p[ord[1]][i].first);
      if (p[ord[2]][i].second > to) b.push_back(p[ord[2]][i].first);
    }
    int lst = -2e9;
    bool found = false;
    for (int j = 0; j < a.size(); j++) {
      if (a[j] > lst) {
        if (j + k - 1 >= a.size()) return false;
        lst = a[j + k - 1];
        found = true;
        break;
      }
    }
    if (!found) return false;
    for (int j = 0; j < b.size(); j++) {
      if (b[j] > lst) {
        if (j + k - 1 >= b.size()) return false;
        lst = b[j + k - 1];
        return true;
      }
    }
    return false;
  }
  bool can(int k) {
    sort((ord).begin(), (ord).end());
    do {
      int cnt = 0;
      for (int t = 0; t < 2; t++) {
        for (int i = 0; i < 2; i++) {
          for (int j = 0; j < 2; j++) {
            for (int x = 0; x < 3; x++) p[x] = v[x][cnt];
            bool res = solveLine(k);
            p[ord[0]] = v2[ord[0]][cnt];
            cnt++;
            res |= solveGroup(k);
            if (res) return true;
          }
        }
      }
    } while (next_permutation((ord).begin(), (ord).end()));
    return false;
  }
  int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    p.resize(3);
    for (int i = 0; i < n; i++) {
      int x, y, c;
      cin >> x >> y >> c;
      c--;
      p[c].push_back({x, y});
    }
    for (int t = 0; t < 2; t++) {
      for (int x = 0; x < 3; x++)
        for (int y = 0; y < p[x].size(); y++) swap(p[x][y].first, p[x][y].second);
      for (int i = 0; i < 2; i++) {
        for (int x = 0; x < 3; x++)
          for (int y = 0; y < p[x].size(); y++) p[x][y].first *= -1;
        for (int j = 0; j < 2; j++) {
          for (int x = 0; x < 3; x++)
            for (int y = 0; y < p[x].size(); y++) p[x][y].second *= -1;
          for (int x = 0; x < 3; x++) {
            v[x].push_back(p[x]);
            v2[x].push_back(p[x]);
            sort((v[x].back()).begin(), (v[x].back()).end());
            sort((v2[x].back()).begin(), (v2[x].back()).end(),
                 [](pair<int, int> a, pair<int, int> b) {
                   return a.second < b.second;
                 });
          }
        }
      }
    }
    int lo = 1, hi = n / 3, ans = 1;
    while (lo <= hi) {
      int md = (lo + hi) / 2;
      if (can(md)) {
        lo = md + 1;
        ans = md;
      } else
        hi = md - 1;
    }
    cout << ans * 3 << '\n';
  }
  