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;
  void read(int& x) {
    static char c;
    int f = 0;
    while (!isdigit(c = getchar()))
      if (c == '-') f = 1;
    x = c ^ 48;
    while (isdigit(c = getchar())) x = (x * 10) + (c ^ 48);
    if (f) x = -x;
  }
  void read(long long& x) {
    static char c;
    int f = 0;
    while (!isdigit(c = getchar()))
      if (c == '-') f = 1;
    x = c ^ 48;
    while (isdigit(c = getchar())) x = (x * 10) + (c ^ 48);
    if (f) x = -x;
  }
  void read(unsigned int& x) {
    static char c;
    while (!isdigit(c = getchar()))
      ;
    x = c ^ 48;
    while (isdigit(c = getchar())) x = (x * 10) + (c ^ 48);
  }
  void read(unsigned long long& x) {
    static char c;
    while (!isdigit(c = getchar()))
      ;
    x = c ^ 48;
    while (isdigit(c = getchar())) x = (x * 10) + (c ^ 48);
  }
  void chkmax(int& x, int y) {
    if (y > x) x = y;
  }
  void chkmin(int& x, int y) {
    if (y < x) x = y;
  }
  void chkmax(long long& x, long long y) {
    if (y > x) x = y;
  }
  void chkmin(long long& x, long long y) {
    if (y < x) x = y;
  }
  void chkmax(unsigned int& x, unsigned int y) {
    if (y > x) x = y;
  }
  void chkmin(unsigned int& x, unsigned int y) {
    if (y < x) x = y;
  }
  void chkmax(unsigned long long& x, unsigned long long y) {
    if (y > x) x = y;
  }
  void chkmin(unsigned long long& x, unsigned long long y) {
    if (y < x) x = y;
  }
  const int N = 2e5 + 10;
  int n, m;
  int X[N], Y[N], col[N];
  int srt[N * 2];
  int xPre[4][N], yPre[4][N];
  int p[4];
  bool case1(int mid) {
    int pos1, pos2;
    for (pos1 = 1; pos1 <= m; ++pos1)
      if (xPre[p[1]][pos1] >= mid) break;
    for (pos2 = pos1 + 1; pos2 <= m; ++pos2)
      if (xPre[p[2]][pos2] - xPre[p[2]][pos1] >= mid) break;
    if (pos2 > m) return false;
    return n / 3 - xPre[p[3]][pos2] >= mid;
  }
  bool case2(int mid) {
    int pos1, pos2;
    for (pos1 = 1; pos1 <= m; ++pos1)
      if (yPre[p[1]][pos1] >= mid) break;
    for (pos2 = pos1 + 1; pos2 <= m; ++pos2)
      if (yPre[p[2]][pos2] - yPre[p[2]][pos1] >= mid) break;
    if (pos2 > m) return false;
    return n / 3 - yPre[p[3]][pos2] >= mid;
  }
  int Buc[4][N];
  bool case3(int mid) {
    int pos1, pos2;
    for (pos1 = 1; pos1 <= m; ++pos1)
      if (xPre[p[1]][pos1] >= mid) break;
    memset(Buc, 0, sizeof Buc);
    for (int i = 1; i <= n; ++i)
      if (X[i] > pos1) ++Buc[col[i]][Y[i]];
    for (int c : {1, 2, 3})
      for (int i = 1; i <= m; ++i) Buc[c][i] += Buc[c][i - 1];
    for (pos2 = 1; pos2 <= m; ++pos2)
      if (Buc[p[2]][pos2] >= mid) break;
    if (pos2 > m) return false;
    return Buc[p[3]][m] - Buc[p[3]][pos2] >= mid;
  }
  bool case5(int mid) {
    int pos1, pos2;
    for (pos1 = m; pos1; --pos1)
      if (xPre[p[1]][m] - xPre[p[1]][pos1 - 1] >= mid) break;
    memset(Buc, 0, sizeof Buc);
    for (int i = 1; i <= n; ++i)
      if (X[i] < pos1) ++Buc[col[i]][Y[i]];
    for (int c : {1, 2, 3})
      for (int i = 1; i <= m; ++i) Buc[c][i] += Buc[c][i - 1];
    for (pos2 = 1; pos2 <= m; ++pos2)
      if (Buc[p[2]][pos2] >= mid) break;
    if (pos2 > m) return false;
    return Buc[p[3]][m] - Buc[p[3]][pos2] >= mid;
  }
  bool case4(int mid) {
    int pos1, pos2;
    for (pos1 = 1; pos1 <= m; ++pos1)
      if (yPre[p[1]][pos1] >= mid) break;
    memset(Buc, 0, sizeof Buc);
    for (int i = 1; i <= n; ++i)
      if (Y[i] > pos1) ++Buc[col[i]][X[i]];
    for (int c : {1, 2, 3})
      for (int i = 1; i <= m; ++i) Buc[c][i] += Buc[c][i - 1];
    for (pos2 = 1; pos2 <= m; ++pos2)
      if (Buc[p[2]][pos2] >= mid) break;
    if (pos2 > m) return false;
    return Buc[p[3]][m] - Buc[p[3]][pos2] >= mid;
  }
  bool case6(int mid) {
    int pos1, pos2;
    for (pos1 = m; pos1; --pos1)
      if (yPre[p[1]][m] - yPre[p[1]][pos1 - 1] >= mid) break;
    memset(Buc, 0, sizeof Buc);
    for (int i = 1; i <= n; ++i)
      if (Y[i] < pos1) ++Buc[col[i]][X[i]];
    for (int c : {1, 2, 3})
      for (int i = 1; i <= m; ++i) Buc[c][i] += Buc[c][i - 1];
    for (pos2 = 1; pos2 <= m; ++pos2)
      if (Buc[p[2]][pos2] >= mid) break;
    if (pos2 > m) return false;
    return Buc[p[3]][m] - Buc[p[3]][pos2] >= mid;
  }
  bool check(int mid) {
    p[1] = 1, p[2] = 2, p[3] = 3;
    do {
      if (case1(mid)) return true;
      if (case2(mid)) return true;
      if (case3(mid)) return true;
      if (case5(mid)) return true;
      if (case4(mid)) return true;
      if (case6(mid)) return true;
    } while (next_permutation(p + 1, p + 4));
    return false;
  }
  void Work() {
    read(n);
    for (int i = 1; i <= n; ++i)
      read(X[i]), read(Y[i]), read(col[i]), srt[2 * i - 1] = X[i],
                                                        srt[2 * i] = Y[i];
    sort(srt + 1, srt + 2 * n + 1),
        m = unique(srt + 1, srt + 2 * n + 1) - srt - 1;
    for (int i = 1; i <= n; ++i)
      X[i] = lower_bound(srt + 1, srt + m + 1, X[i]) - srt,
      Y[i] = lower_bound(srt + 1, srt + m + 1, Y[i]) - srt;
    for (int i = 1; i <= n; ++i) ++xPre[col[i]][X[i]], ++yPre[col[i]][Y[i]];
    for (int c : {1, 2, 3})
      for (int i = 1; i <= m; ++i)
        xPre[c][i] += xPre[c][i - 1], yPre[c][i] += yPre[c][i - 1];
    int l = 1, r = n / 3, mid, ans = 1;
    while (l <= r) {
      mid = (l + r) >> 1;
      if (check(mid))
        ans = mid, l = mid + 1;
      else
        r = mid - 1;
    }
    printf("%d\n", 3 * ans);
  }
  int main() { Work(); }
  