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 CL = 3;
  const int INF = 1e9 + 123;
  vector<pair<int, int> > V[4][CL];
  bool checkrow(vector<int> perm, int need, int ind) {
    int rx = -INF;
    bool flag = false;
    for (int col : perm) {
      ++rx;
      int cnt = 0;
      for (auto [x, y] : V[ind][col]) {
        if (x >= rx) ++cnt;
        rx = max(x, rx);
        if (cnt == need) break;
      }
      if (cnt != need) return false;
    }
    return true;
  }
  bool check1(vector<int> perm, int need) {
    bool ans = false;
    for (int u = 0; u < 2; ++u) {
      ans = max(ans, checkrow(perm, need, u));
    }
    return ans;
  }
  bool mc(pair<int, int> a, pair<int, int> b) { return a.second < b.second; }
  bool checkT(vector<int> perm, int need, int ind) {
    int rx = -INF;
    int cnt = 0;
    for (auto [x, y] : V[ind][perm[0]]) {
      rx = max(rx, x);
      ++cnt;
      if (cnt == need) break;
    }
    if (cnt != need) return false;
    ++rx;
    int ry = -INF;
    for (int j = 1; j < CL; ++j) {
      int col = perm[j];
      ++ry;
      int cnt = 0;
      for (auto [y, x] : V[(ind & 1) ^ 1][col]) {
        int sign = (ind & 2) ? -1 : 1;
        if (x * sign < rx || y < ry) continue;
        ry = max(ry, y);
        ++cnt;
        if (cnt == need) break;
      }
      if (cnt != need) return false;
    }
    return true;
  }
  bool check2(vector<int> perm, int need) {
    bool ans = false;
    for (int u = 0; u < 2; ++u) {
      for (int u1 = 0; u1 < 2; ++u1) {
        ans = max(ans, checkT(perm, need, u + u1 * 2));
      }
    }
    return ans;
  }
  bool F(int need) {
    vector<int> perm(CL);
    for (int i = 0; i < CL; ++i) perm[i] = i;
    do {
      if (check1(perm, need)) return true;
      if (check2(perm, need)) return true;
    } while (next_permutation(perm.begin(), perm.end()));
    return false;
  }
  void solve() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; ++i) {
      int x, y, c;
      cin >> x >> y >> c;
      c--;
      V[0][c].push_back({x, y});
    }
    for (int c = 0; c < CL; ++c) {
      V[1][c] = V[0][c];
      V[2][c] = V[0][c];
      V[3][c] = V[0][c];
      int sz = V[0][c].size();
      for (int j = 0; j < sz; ++j)
        swap(V[1][c][j].first, V[1][c][j].second),
            swap(V[3][c][j].first, V[3][c][j].second);
      for (int j = 0; j < sz; ++j) V[2][c][j].first *= -1, V[3][c][j].first *= -1;
      for (int cl = 0; cl < 4; ++cl) sort(V[cl][c].begin(), V[cl][c].end());
    }
    int l = 0, r = n / 3;
    while (l != r) {
      int mid = (l + r + 1) >> 1;
      if (F(mid))
        l = mid;
      else
        r = mid - 1;
    }
    cout << l * 3 << '\n';
  }
  int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t = 1;
    while (t--) solve();
    return 0;
  }
  