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 BS = 1 << 17;
  char buf[BS], *S, *T;
  inline char gc() {
    if (S == T) T = (S = buf) + fread(buf, 1, BS, stdin);
    return S != T ? *(S++) : EOF;
  }
  inline int in() {
    int x = 0, f = 1;
    char c = gc();
    while (c < '0' || c > '9') {
      if (c == '-') f = -1;
      c = gc();
    }
    while (c >= '0' && c <= '9') x = x * 10 + c - 48, c = gc();
    return x * f;
  }
  const int N = 1e5 + 5, inf = 1e9;
  int n;
  int c[4];
  struct point {
    int x, y;
    point(int _x = 0, int _y = 0) { x = _x, y = _y; }
  };
  vector<point> v1[4], v2[4];
  inline bool cmp1(const point &a, const point &b) { return a.x < b.x; }
  inline bool cmp2(const point &a, const point &b) { return a.y < b.y; }
  bool check1(int x, int xx, int y, int yy, int k) {
    int cnt = 0, l = inf, r = -inf;
    for (point p : v1[c[2]]) {
      if (p.x >= x && p.x <= xx && p.y >= y && p.y <= yy) {
        cnt++;
        if (cnt == k) {
          l = p.x;
          break;
        }
      }
    }
    cnt = 0;
    for (int i = n / 3 - 1; i >= 0; i--) {
      point p = v1[c[3]][i];
      if (p.x >= x && p.x <= xx && p.y >= y && p.y <= yy) {
        cnt++;
        if (cnt == k) {
          r = p.x;
          break;
        }
      }
    }
    if (l < r) return 1;
    l = inf, r = -inf, cnt = 0;
    for (point p : v2[c[2]]) {
      if (p.x >= x && p.x <= xx && p.y >= y && p.y <= yy) {
        cnt++;
        if (cnt == k) {
          l = p.y;
          break;
        }
      }
    }
    cnt = 0;
    for (int i = n / 3 - 1; i >= 0; i--) {
      point p = v2[c[3]][i];
      if (p.x >= x && p.x <= xx && p.y >= y && p.y <= yy) {
        cnt++;
        if (cnt == k) {
          r = p.y;
          break;
        }
      }
    }
    if (l < r) return 1;
    return 0;
  }
  bool check(int k) {
    c[1] = 1, c[2] = 2, c[3] = 3;
    do {
      int x = v1[c[1]][k - 1].x;
      if (check1(x + 1, inf, -inf, inf, k)) return 1;
      int y = v2[c[1]][k - 1].y;
      if (check1(-inf, inf, y + 1, inf, k)) return 1;
      x = v1[c[1]][n / 3 - k].x;
      if (check1(-inf, x - 1, -inf, inf, k)) return 1;
      y = v2[c[1]][n / 3 - k].y;
      if (check1(-inf, inf, -inf, y - 1, k)) return 1;
    } while (next_permutation(c + 1, c + 4));
    return 0;
  }
  int main() {
    n = in();
    for (int i = 1; i <= n; i++) {
      int x = in(), y = in(), c = in();
      v1[c].push_back(point(x, y));
      v2[c].push_back(point(x, y));
    }
    for (int i = 1; i <= 3; i++) {
      sort(v1[i].begin(), v1[i].end(), cmp1);
      sort(v2[i].begin(), v2[i].end(), cmp2);
    }
    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;
    }
    cout << ans * 3;
    return 0;
  }
  