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;
  vector<vector<pair<int, int>>> xy(3, vector<pair<int, int>>());
  vector<vector<pair<int, int>>> xy_x(3, vector<pair<int, int>>());
  vector<vector<pair<int, int>>> xy_xr(3, vector<pair<int, int>>());
  vector<vector<pair<int, int>>> xy_y(3, vector<pair<int, int>>());
  vector<vector<pair<int, int>>> xy_yr(3, vector<pair<int, int>>());
  int n;
  int A[6] = {0, 0, 1, 1, 2, 2};
  int B[6] = {1, 2, 0, 2, 0, 1};
  int C[6] = {2, 1, 2, 0, 1, 0};
  const int inf = 1 << 30;
  bool yy(pair<int, int> &lhs, pair<int, int> &rhs) {
    return lhs.second < rhs.second;
  }
  bool yy_r(pair<int, int> &lhs, pair<int, int> &rhs) {
    return lhs.second > rhs.second;
  }
  bool ok(int z) {
    int a, b, c, x, y, max_x, max_y, flg, cnt;
    for (int i = 0; i < 6; i++) {
      a = A[i];
      b = B[i];
      c = C[i];
      max_x = -inf;
      flg = 0;
      cnt = 0;
      for (auto tmp : xy_x[a]) {
        x = tmp.first;
        y = tmp.second;
        if (x >= max_x) {
          max_x = x;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      cnt = 0;
      max_x++;
      for (auto tmp : xy_x[b]) {
        x = tmp.first;
        y = tmp.second;
        if (x >= max_x) {
          max_x = x;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      cnt = 0;
      max_x++;
      for (auto tmp : xy_x[c]) {
        x = tmp.first;
        y = tmp.second;
        if (x >= max_x) {
          max_x = x;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      if (flg == 3) return true;
      max_y = -inf;
      flg = 0;
      cnt = 0;
      for (auto tmp : xy_y[a]) {
        x = tmp.first;
        y = tmp.second;
        if (y >= max_y) {
          max_y = y;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      cnt = 0;
      max_y++;
      for (auto tmp : xy_y[b]) {
        x = tmp.first;
        y = tmp.second;
        if (y >= max_y) {
          max_y = y;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      cnt = 0;
      max_y++;
      for (auto tmp : xy_y[c]) {
        x = tmp.first;
        y = tmp.second;
        if (y >= max_y) {
          max_y = y;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      if (flg == 3) return true;
      max_x = -inf;
      flg = 0;
      cnt = 0;
      for (auto tmp : xy_x[a]) {
        x = tmp.first;
        y = tmp.second;
        if (x >= max_x) {
          max_x = x;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      cnt = 0;
      max_x++;
      max_y = -inf;
      for (auto tmp : xy_y[b]) {
        x = tmp.first;
        y = tmp.second;
        if (x >= max_x && y >= max_y) {
          max_y = y;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      cnt = 0;
      max_y++;
      for (auto tmp : xy_y[c]) {
        x = tmp.first;
        y = tmp.second;
        if (x >= max_x && y >= max_y) {
          max_y = y;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      if (flg == 3) return true;
      max_x = inf;
      flg = 0;
      cnt = 0;
      for (auto tmp : xy_xr[a]) {
        x = tmp.first;
        y = tmp.second;
        if (x <= max_x) {
          max_x = x;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      cnt = 0;
      max_x--;
      max_y = -inf;
      for (auto tmp : xy_y[b]) {
        x = tmp.first;
        y = tmp.second;
        if (x <= max_x && y >= max_y) {
          max_y = y;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      cnt = 0;
      max_y++;
      for (auto tmp : xy_y[c]) {
        x = tmp.first;
        y = tmp.second;
        if (x <= max_x && y >= max_y) {
          max_y = y;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      if (flg == 3) return true;
      max_y = -inf;
      flg = 0;
      cnt = 0;
      for (auto tmp : xy_y[a]) {
        x = tmp.first;
        y = tmp.second;
        if (y >= max_y) {
          max_y = y;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      cnt = 0;
      max_y++;
      max_x = -inf;
      for (auto tmp : xy_x[b]) {
        x = tmp.first;
        y = tmp.second;
        if (x >= max_x && y >= max_y) {
          max_x = x;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      cnt = 0;
      max_x++;
      for (auto tmp : xy_x[c]) {
        x = tmp.first;
        y = tmp.second;
        if (x >= max_x && y >= max_y) {
          max_x = x;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      if (flg == 3) return true;
      max_y = inf;
      flg = 0;
      cnt = 0;
      for (auto tmp : xy_yr[a]) {
        x = tmp.first;
        y = tmp.second;
        if (y <= max_y) {
          max_y = y;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      cnt = 0;
      max_y--;
      max_x = -inf;
      for (auto tmp : xy_x[b]) {
        x = tmp.first;
        y = tmp.second;
        if (x >= max_x && y <= max_y) {
          max_x = x;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      cnt = 0;
      max_x++;
      for (auto tmp : xy_x[c]) {
        x = tmp.first;
        y = tmp.second;
        if (x >= max_x && y <= max_y) {
          max_x = x;
          cnt++;
          if (cnt == z) {
            flg++;
            break;
          }
        }
      }
      if (flg == 3) return true;
    }
    return false;
  }
  int main(void) {
    cin >> n;
    int x, y, c;
    for (int i = 0; i < n; i++) {
      cin >> x >> y >> c;
      xy[c - 1].push_back({x, y});
    }
    for (int i = 0; i < 3; i++) {
      sort(xy[i].begin(), xy[i].end());
      xy_x[i] = xy[i];
      sort(xy[i].begin(), xy[i].end(), greater<pair<int, int>>());
      xy_xr[i] = xy[i];
      sort(xy[i].begin(), xy[i].end(), yy);
      xy_y[i] = xy[i];
      sort(xy[i].begin(), xy[i].end(), yy_r);
      xy_yr[i] = xy[i];
    }
    int l, r, mid;
    l = 1;
    r = n + 1;
    while (r - l > 1) {
      mid = (l + r) / 2;
      if (ok(mid))
        l = mid;
      else
        r = mid;
    }
    cout << 3 * l << "\n";
  }
  