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;
  vector<pair<int, int> > P[3];
  void compress(vector<int> &V) {
    sort(V.begin(), V.end());
    V.erase(unique(V.begin(), V.end()), V.end());
  }
  int C[3][100005];
  int solve() {
    memset(C, 0, sizeof(C));
    int i0 = 0, i1 = 0, i2 = 0;
    int y = 0, up = 0, dn = 0;
    int ret = 0;
    for (int x = 0; x <= n; ++x) {
      while (i0 < n / 3 && P[0][i0].first <= x) ++i0;
      while (i1 < n / 3 && P[1][i1].first <= x) {
        int py = P[1][i1++].second;
        ++C[1][py];
        if (py > y) ++up;
      }
      while (i2 < n / 3 && P[2][i2].first <= x) {
        int py = P[2][i2++].second;
        ++C[2][py];
        if (py <= y) ++dn;
      }
      ret = max(ret, min({up, dn, n / 3 - i0}));
      while (up < dn) {
        up += C[1][y];
        dn -= C[2][y];
        --y;
        ret = max(ret, min({up, dn, n / 3 - i0}));
      }
      while (up > dn) {
        up -= C[1][y + 1];
        dn += C[2][y + 1];
        ++y;
        ret = max(ret, min({up, dn, n / 3 - i0}));
      }
    }
    return ret;
  }
  int solve2() {
    memset(C, 0, sizeof(C));
    int i0 = 0, i1 = 0, i2 = 0;
    int y = 0, up = 0, dn = 0;
    int ret = 0;
    for (int x = 0; x <= n; ++x) {
      while (i0 < n / 3 && P[0][i0].first <= x) ++i0;
      while (i1 < n / 3 && P[1][i1].first <= x) {
        int py = P[1][i1++].first;
        ++C[1][py];
        if (py > y) ++up;
      }
      while (i2 < n / 3 && P[2][i2].first <= x) {
        int py = P[2][i2++].first;
        ++C[2][py];
        if (py <= y) ++dn;
      }
      ret = max(ret, min({up, dn, n / 3 - i0}));
      while (up < dn) {
        up += C[1][y];
        dn -= C[2][y];
        --y;
        ret = max(ret, min({up, dn, n / 3 - i0}));
      }
      while (up > dn) {
        up -= C[1][y + 1];
        dn += C[2][y + 1];
        ++y;
        ret = max(ret, min({up, dn, n / 3 - i0}));
      }
    }
    return ret;
  }
  int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(30);
    cout << fixed;
    cin >> n;
    vector<int> X, Y;
    for (int i = 1; i <= n; ++i) {
      int x, y, c;
      cin >> x >> y >> c;
      P[c - 1].emplace_back(x, y);
      X.push_back(x);
      Y.push_back(y);
    }
    compress(X);
    compress(Y);
    for (int i = 0; i < 3; ++i) {
      for (auto &[x, y] : P[i]) {
        x = upper_bound(X.begin(), X.end(), x) - X.begin();
        y = upper_bound(Y.begin(), Y.end(), y) - Y.begin();
      }
    }
    int ans = 0;
    for (int xy = 0; xy < 2; ++xy) {
      for (int i = 0; i < 3; ++i) sort(P[i].begin(), P[i].end());
      for (int f = 0; f < 2; ++f) {
        ans = max(ans, solve());
        swap(P[0], P[1]);
        ans = max(ans, solve());
        swap(P[0], P[2]);
        ans = max(ans, solve());
        swap(P[1], P[2]);
        ans = max(ans, solve());
        swap(P[0], P[1]);
        ans = max(ans, solve());
        swap(P[0], P[2]);
        ans = max(ans, solve());
        swap(P[1], P[2]);
        for (int i = 0; i < 3; ++i) {
          for (auto &[x, y] : P[i]) x = n + 1 - x;
          reverse(P[i].begin(), P[i].end());
        }
      }
      ans = max(ans, solve2());
      swap(P[0], P[1]);
      ans = max(ans, solve2());
      swap(P[0], P[2]);
      ans = max(ans, solve2());
      swap(P[1], P[2]);
      ans = max(ans, solve2());
      swap(P[0], P[1]);
      ans = max(ans, solve2());
      swap(P[0], P[2]);
      ans = max(ans, solve2());
      swap(P[1], P[2]);
      for (int i = 0; i < 3; ++i) {
        for (auto &[x, y] : P[i]) swap(x, y);
      }
    }
    printf("%d\n", ans * 3);
    return 0;
  }
  