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;
  int n, sok = 1e9 + 1;
  vector<pair<int, int> > xr[3], yr[3];
  bool solve(int mid, int ba, int ja, int bf, int jf) {
    if (ba == ja || bf == jf) {
      int xa = -sok, xf = sok;
      if (ba == ja) {
        xa = xr[ba][mid - 1].first;
      }
      if (bf == jf) {
        xf = xr[jf][n / 3 - mid].first;
      }
      if (ba == ja && bf == jf) {
        int db = 0, h = 3 - ba - bf;
        for (auto ert : xr[h]) {
          if (xa < ert.first && ert.first < xf) db++;
        }
        return (db >= mid);
      } else {
        int bal = (ba == ja ? bf : ba), jobb = (ba == ja ? jf : ja);
        int bert = -sok, bdb = 0, jert = sok, jdb = 0;
        for (auto ert : yr[bal]) {
          if (xa < ert.second && ert.second < xf) {
            bdb++;
            bert = ert.first;
            if (bdb == mid) {
              break;
            }
          }
        }
        for (int i = n / 3 - 1; i >= 0; i--) {
          auto ert = yr[jobb][i];
          if (xa < ert.second && ert.second < xf) {
            jdb++;
            jert = ert.first;
            if (jdb == mid) {
              break;
            }
          }
        }
        if (bdb == mid && jdb == mid && bert < jert) {
          return true;
        }
        return false;
      }
    }
    if (ba == bf || ja == jf) {
      int ya = -sok, yf = sok;
      if (ba == bf) {
        ya = yr[ba][mid - 1].first;
      }
      if (ja == jf) {
        yf = yr[jf][n / 3 - mid].first;
      }
      if (ba == bf && ja == jf) {
        int db = 0, h = 3 - ba - ja;
        for (auto ert : yr[h]) {
          if (ya < ert.first && ert.first < yf) db++;
        }
        return (db >= mid);
      } else {
        int kis = (ba == bf ? ja : ba), nagy = (ba == bf ? jf : bf);
        int kert = -sok, kdb = 0, nert = sok, ndb = 0;
        for (auto ert : xr[kis]) {
          if (ya < ert.second && ert.second < yf) {
            kdb++;
            kert = ert.first;
            if (kdb == mid) {
              break;
            }
          }
        }
        for (int i = n / 3 - 1; i >= 0; i--) {
          auto ert = xr[nagy][i];
          if (ya < ert.second && ert.second < yf) {
            ndb++;
            nert = ert.first;
            if (ndb == mid) {
              break;
            }
          }
        }
        if (kdb == mid && ndb == mid && kert < nert) {
          return true;
        }
        return false;
      }
    }
  }
  int main() {
    ios_base::sync_with_stdio(false);
    cin >> n;
    for (int i = 1; i <= n; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      c--;
      xr[c].push_back({a, b});
      yr[c].push_back({b, a});
    }
    for (int i = 0; i < 3; i++) {
      sort(xr[i].begin(), xr[i].end());
      sort(yr[i].begin(), yr[i].end());
    }
    int lo = 0, hi = n / 3 + 1, mid;
    while (hi - lo > 1) {
      mid = (hi + lo) / 2;
      bool jo = 0;
      for (int ba = 0; ba < 3; ba++) {
        for (int ja = 0; ja < 3; ja++) {
          for (int bf = 0; bf < 3; bf++) {
            for (int jf = 0; jf < 3; jf++) {
              if (ba == jf || ja == bf) continue;
              if (solve(mid, ba, ja, bf, jf)) {
                jo = 1;
              }
            }
          }
        }
      }
      if (jo) {
        lo = mid;
      } else {
        hi = mid;
      }
    }
    cout << 3 * lo << "\n";
    return 0;
  }
  