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 gi() {
    int x = 0, c = getchar();
    bool f = 0;
    for (; !isdigit(c); c = getchar())
      if (c == '-') f = 1;
    for (; isdigit(c); c = getchar()) x = x * 10 + (c & 15);
    return f ? -x : x;
  }
  constexpr int N = (int)1e5 + 5;
  int n, x[N], y[N], c[N], _n, _m;
  vector<int> _x, _y, hang[3][N], lie[3][N];
  bool _check1(int k, int a, int b, int c) {
    int cnt = 0;
    for (int i = 1; i <= _n - 2; ++i) {
      cnt += hang[a][i].size();
      if (cnt >= k) {
        cnt = 0;
        for (int j = i + 1; j < _n; ++j) {
          cnt += hang[b][j].size();
          if (cnt >= k) {
            cnt = 0;
            for (int l = j + 1; l <= _n; ++l) {
              cnt += hang[c][l].size();
            }
            return cnt >= k;
          }
        }
        return 0;
      }
    }
    return 0;
  }
  bool _check2(int k, int a, int b, int c) {
    int cnt = 0;
    for (int i = 1; i <= _m - 2; ++i) {
      cnt += lie[a][i].size();
      if (cnt >= k) {
        cnt = 0;
        for (int j = i + 1; j < _m; ++j) {
          cnt += lie[b][j].size();
          if (cnt >= k) {
            cnt = 0;
            for (int l = j + 1; l <= _m; ++l) {
              cnt += lie[c][l].size();
            }
            return cnt >= k;
          }
        }
        return 0;
      }
    }
    return 0;
  }
  bool _check3(int k, int a, int b, int c) {
    int cnt = 0;
    for (int i = 1; i < _n; ++i) {
      cnt += hang[a][i].size();
      if (cnt >= k) {
        cnt = 0;
        for (int j = 1; j < _m; ++j) {
          for (int x : lie[b][j]) {
            if (x >= i + 1) cnt++;
          }
          if (cnt >= k) {
            cnt = 0;
            for (int l = j + 1; l <= _m; ++l) {
              for (int x : lie[c][l]) {
                if (x >= i + 1) cnt++;
              }
            }
            return cnt >= k;
          }
        }
        return 0;
      }
    }
    return 0;
  }
  bool _check4(int k, int a, int b, int c) {
    int cnt = 0;
    for (int i = 1; i < _m; ++i) {
      cnt += lie[a][i].size();
      if (cnt >= k) {
        cnt = 0;
        for (int j = 1; j < _n; ++j) {
          for (int x : hang[b][j]) {
            if (x >= i + 1) cnt++;
          }
          if (cnt >= k) {
            cnt = 0;
            for (int l = j + 1; l <= _n; ++l) {
              for (int x : hang[c][l]) {
                if (x >= i + 1) cnt++;
              }
            }
            return cnt >= k;
          }
        }
        return 0;
      }
    }
    return 0;
  }
  bool _check5(int k, int a, int b, int c) {
    int cnt = 0;
    for (int i = _n; i > 1; --i) {
      cnt += hang[a][i].size();
      if (cnt >= k) {
        cnt = 0;
        for (int j = 1; j < _m; ++j) {
          for (int x : lie[b][j]) {
            if (x <= i - 1) cnt++;
          }
          if (cnt >= k) {
            cnt = 0;
            for (int l = j + 1; l <= _m; ++l) {
              for (int x : lie[c][l]) {
                if (x <= i - 1) cnt++;
              }
            }
            return cnt >= k;
          }
        }
        return 0;
      }
    }
    return 0;
  }
  bool _check6(int k, int a, int b, int c) {
    int cnt = 0;
    for (int i = _m; i > 1; --i) {
      cnt += lie[a][i].size();
      if (cnt >= k) {
        cnt = 0;
        for (int j = 1; j < _n; ++j) {
          for (int x : hang[b][j]) {
            if (x <= i - 1) cnt++;
          }
          if (cnt >= k) {
            cnt = 0;
            for (int l = j + 1; l <= _n; ++l) {
              for (int x : hang[c][l]) {
                if (x <= i - 1) cnt++;
              }
            }
            return cnt >= k;
          }
        }
        return 0;
      }
    }
    return 0;
  }
  bool _check(int k, int a, int b, int c) {
    return _check1(k, a, b, c) || _check2(k, a, b, c) || _check3(k, a, b, c) ||
           _check4(k, a, b, c) || _check5(k, a, b, c) || _check6(k, a, b, c);
  }
  bool check(int k) {
    return _check(k, 0, 1, 2) || _check(k, 0, 2, 1) || _check(k, 1, 0, 2) ||
           _check(k, 1, 2, 0) || _check(k, 2, 0, 1) || _check(k, 2, 1, 0);
  }
  int main() {
    n = gi();
    for (int i = 1; i <= n; ++i) {
      x[i] = gi(), y[i] = gi(), c[i] = gi();
      _x.push_back(x[i]);
      _y.push_back(y[i]);
    }
    sort(_x.begin(), _x.end());
    sort(_y.begin(), _y.end());
    _x.resize(unique(_x.begin(), _x.end()) - _x.begin());
    _y.resize(unique(_y.begin(), _y.end()) - _y.begin());
    _n = _x.size(), _m = _y.size();
    for (int i = 1; i <= n; ++i) {
      x[i] = lower_bound(_x.begin(), _x.end(), x[i]) - _x.begin() + 1;
      y[i] = lower_bound(_y.begin(), _y.end(), y[i]) - _y.begin() + 1;
      hang[c[i] - 1][x[i]].push_back(y[i]);
      lie[c[i] - 1][y[i]].push_back(x[i]);
    }
    int l = 2, r = n / 3;
    while (l <= r) {
      int mid = (l + r) >> 1;
      if (check(mid))
        l = mid + 1;
      else
        r = mid - 1;
    }
    printf("%d\n", 3 * r);
    return 0;
  }
  