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;
  bool home = true;
  struct Cell {
    int x;
    int y;
    int type;
  };
  bool cmp1(Cell a, Cell b) { return a.x < b.x; }
  bool cmp2(Cell a, Cell b) { return a.y < b.y; }
  const int N = (int)1e5 + 7;
  int n;
  bool okLR(vector<Cell> &cells, int need, vector<int> ord) {
    int first = ord[0], second = ord[1], third = ord[2];
    int cntFirst = 0;
    for (int i = 0; i < n; i++) {
      if (cells[i].type == first) {
        cntFirst++;
        if (cntFirst == need) {
          int cntSecond = 0;
          for (int j = i + 1; j < n; j++) {
            if (cells[j].x > cells[i].x && cells[j].type == second) {
              cntSecond++;
              if (cntSecond == need) {
                int cntThird = 0;
                for (int k = j + 1; k < n; k++) {
                  if (cells[k].x > cells[j].x && cells[k].type == third) {
                    cntThird++;
                    if (cntThird == need) {
                      return true;
                    }
                  }
                }
                return false;
              }
            }
          }
          return false;
        }
      }
    }
    assert(false);
  }
  struct Bbox {
    int xmin = (int)1e9, ymin = (int)1e9;
    int xmax = -(int)1e9, ymax = -(int)1e9;
    int ind;
    void add(int x, int y) {
      xmin = min(xmin, x);
      xmax = max(xmax, x);
      ymin = min(ymin, y);
      ymax = max(ymax, y);
    }
  };
  bool okUD(vector<Cell> &cells, vector<Cell> &cells2, int need,
            vector<int> ord) {
    int first = ord[0], second = ord[1], third = ord[2];
    int cntFirst = 0;
    for (int i = 0; i < n; i++) {
      if (cells[i].type == first) {
        cntFirst++;
        if (cntFirst == need) {
          int cntSecond = 0;
          for (int j = 0; j < n; j++) {
            if (cells2[j].x > cells[i].x && cells2[j].type == second) {
              cntSecond++;
              if (cntSecond == need) {
                int cntThird = 0;
                for (int k = j + 1; k < n; k++) {
                  if (cells2[k].y > cells2[j].y && cells2[k].x > cells[i].x &&
                      cells2[k].type == third) {
                    cntThird++;
                    if (cntThird == need) {
                      return true;
                    }
                  }
                }
                return false;
              }
            }
          }
          return false;
        }
      }
    }
    assert(false);
  }
  void upd(int invx, int invy, vector<int> ord, int &bestK, vector<Cell> cells,
           vector<Cell> cells2) {
    int low = bestK + 1, high = n / 3;
    while (low <= high) {
      int mid = (low + high) / 2;
      if (okLR(cells, mid, ord)) {
        bestK = mid;
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
    low = bestK + 1;
    high = n / 3;
    while (low <= high) {
      int mid = (low + high) / 2;
      if (okUD(cells, cells2, mid, ord)) {
        bestK = mid;
        low = mid + 1;
      } else {
        high = mid - 1;
      }
    }
  }
  signed main() {
    if (!home) {
      ios::sync_with_stdio(0);
      cin.tie(0);
    }
    cin >> n;
    vector<Cell> cells(n);
    for (auto &cell : cells) {
      cin >> cell.x >> cell.y >> cell.type;
    }
    int bestK = 0;
    for (int swp = 0; swp < 2; swp++) {
      for (int invx = 0; invx < 2; invx++) {
        for (int invy = 0; invy < 2; invy++) {
          vector<Cell> cells2;
          if (invx) {
            for (auto &cell : cells) {
              cell.x *= -1;
            }
          }
          if (invy) {
            for (auto &cell : cells) {
              cell.y *= -1;
            }
          }
          sort(cells.begin(), cells.end(), cmp1);
          cells2 = cells;
          sort(cells2.begin(), cells2.end(), cmp2);
          vector<int> ord = {1, 2, 3};
          upd(invx, invy, ord, bestK, cells, cells2);
          while (next_permutation(ord.begin(), ord.end())) {
            upd(invx, invy, ord, bestK, cells, cells2);
          }
          if (invx) {
            for (auto &cell : cells) {
              cell.x *= -1;
            }
          }
          if (invy) {
            for (auto &cell : cells) {
              cell.y *= -1;
            }
          }
        }
      }
      for (auto &cell : cells) {
        swap(cell.x, cell.y);
      }
    }
    cout << 3 * bestK << "\n";
  }
  