#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V>& x) {
  cerr << '{';
  __print(x.first);
  cerr << ", ";
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T& x) {
  int first = 0;
  cerr << '{';
  for (auto& i : x) cerr << (first++ ? ", " : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 100001;
int N;
vector<int> A[2][3][2];
bool ok(int K) {
  for (int i = 0; i < (2); i++) {
    for (int a = 0; a < (3); a++) {
      for (int b = 0; b < (3); b++) {
        if (a == b) continue;
        int c = 3 - a - b;
        int lstA = A[i][a][i][K - 1];
        int pB = upper_bound(A[i][b][i].begin(), A[i][b][i].end(), lstA) -
                 A[i][b][i].begin();
        pB += K - 1;
        if (pB < N / 3) {
          int lstB = A[i][b][i][pB];
          int pC = upper_bound(A[i][c][i].begin(), A[i][c][i].end(), lstB) -
                   A[i][c][i].begin();
          pC += K - 1;
          if (pC < N / 3) {
            return true;
          }
        }
        vector<int> okA, okB;
        int capOff = A[1 - i][c][1 - i][K - 1];
        for (int x = 0; x < (N / 3); x++) {
          if (A[i][a][1 - i][x] > capOff) {
            okA.push_back(A[i][a][i][x]);
          }
          if (A[i][b][1 - i][x] > capOff) {
            okB.push_back(A[i][b][i][x]);
          }
        }
        if ((int)(okA).size() >= K && (int)(okB).size() >= K &&
            okA[K - 1] < okB[(int)(okB).size() - K]) {
          return true;
        }
        okA.clear();
        okB.clear();
        capOff = A[1 - i][c][1 - i][N / 3 - K];
        for (int x = 0; x < (N / 3); x++) {
          if (A[i][a][1 - i][x] < capOff) {
            okA.push_back(A[i][a][i][x]);
          }
          if (A[i][b][1 - i][x] < capOff) {
            okB.push_back(A[i][b][i][x]);
          }
        }
        if ((int)(okA).size() >= K && (int)(okB).size() >= K &&
            okA[K - 1] < okB[(int)(okB).size() - K]) {
          return true;
        }
      }
    }
  }
  return false;
}
void solve() {
  cin >> N;
  int X[N], Y[N], C[N];
  for (int i = 0; i < (N); i++) cin >> X[i] >> Y[i] >> C[i];
  set<int> vX, vY;
  for (int i = 0; i < (N); i++) {
    vX.insert(X[i]), vY.insert(Y[i]);
  }
  map<int, int> cX, cY;
  for (auto& a : vX) {
    cX[a] = (int)(cX).size() + 1;
  }
  for (auto& a : vY) {
    cY[a] = (int)(cY).size() + 1;
  }
  vector<pair<int, int>> xSor[3], ySor[3];
  for (int i = 0; i < (N); i++) {
    pair<int, int> cur = {cX[X[i]], cY[Y[i]]};
    xSor[C[i] - 1].push_back({cur.first, cur.second});
    ySor[C[i] - 1].push_back({cur.second, cur.first});
  }
  for (int i = 0; i < (3); i++) {
    sort(xSor[i].begin(), xSor[i].end());
    sort(ySor[i].begin(), ySor[i].end());
    for (int j = 0; j < (N / 3); j++) {
      A[0][i][0].push_back(xSor[i][j].first);
      A[0][i][1].push_back(xSor[i][j].second);
      A[1][i][0].push_back(ySor[i][j].second);
      A[1][i][1].push_back(ySor[i][j].first);
    }
  }
  int lo = 1, hi = N / 3;
  while (lo < hi) {
    int mid = (lo + hi + 1) / 2;
    if (ok(mid)) {
      lo = mid;
    } else
      hi = mid - 1;
  }
  cout << lo * 3 << nl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T = 1;
  while (T--) {
    solve();
  }
  return 0;
}

### Prompt

Generate a Cpp solution to the following problem:

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.

### Response

```cpp
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
using namespace std;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
void __print(int x) { cerr << x; }
void __print(long x) { cerr << x; }
void __print(long long x) { cerr << x; }
void __print(unsigned x) { cerr << x; }
void __print(unsigned long x) { cerr << x; }
void __print(unsigned long long x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\'' << x << '\''; }
void __print(const char* x) { cerr << '\"' << x << '\"'; }
void __print(const string& x) { cerr << '\"' << x << '\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }
template <typename T, typename V>
void __print(const pair<T, V>& x) {
  cerr << '{';
  __print(x.first);
  cerr << ", ";
  __print(x.second);
  cerr << '}';
}
template <typename T>
void __print(const T& x) {
  int first = 0;
  cerr << '{';
  for (auto& i : x) cerr << (first++ ? ", " : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\n"; }
template <typename T, typename... V>
void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v)) cerr << ", ";
  _print(v...);
}
const int MOD = 1000000007;
const char nl = '\n';
const int MX = 100001;
int N;
vector<int> A[2][3][2];
bool ok(int K) {
  for (int i = 0; i < (2); i++) {
    for (int a = 0; a < (3); a++) {
      for (int b = 0; b < (3); b++) {
        if (a == b) continue;
        int c = 3 - a - b;
        int lstA = A[i][a][i][K - 1];
        int pB = upper_bound(A[i][b][i].begin(), A[i][b][i].end(), lstA) -
                 A[i][b][i].begin();
        pB += K - 1;
        if (pB < N / 3) {
          int lstB = A[i][b][i][pB];
          int pC = upper_bound(A[i][c][i].begin(), A[i][c][i].end(), lstB) -
                   A[i][c][i].begin();
          pC += K - 1;
          if (pC < N / 3) {
            return true;
          }
        }
        vector<int> okA, okB;
        int capOff = A[1 - i][c][1 - i][K - 1];
        for (int x = 0; x < (N / 3); x++) {
          if (A[i][a][1 - i][x] > capOff) {
            okA.push_back(A[i][a][i][x]);
          }
          if (A[i][b][1 - i][x] > capOff) {
            okB.push_back(A[i][b][i][x]);
          }
        }
        if ((int)(okA).size() >= K && (int)(okB).size() >= K &&
            okA[K - 1] < okB[(int)(okB).size() - K]) {
          return true;
        }
        okA.clear();
        okB.clear();
        capOff = A[1 - i][c][1 - i][N / 3 - K];
        for (int x = 0; x < (N / 3); x++) {
          if (A[i][a][1 - i][x] < capOff) {
            okA.push_back(A[i][a][i][x]);
          }
          if (A[i][b][1 - i][x] < capOff) {
            okB.push_back(A[i][b][i][x]);
          }
        }
        if ((int)(okA).size() >= K && (int)(okB).size() >= K &&
            okA[K - 1] < okB[(int)(okB).size() - K]) {
          return true;
        }
      }
    }
  }
  return false;
}
void solve() {
  cin >> N;
  int X[N], Y[N], C[N];
  for (int i = 0; i < (N); i++) cin >> X[i] >> Y[i] >> C[i];
  set<int> vX, vY;
  for (int i = 0; i < (N); i++) {
    vX.insert(X[i]), vY.insert(Y[i]);
  }
  map<int, int> cX, cY;
  for (auto& a : vX) {
    cX[a] = (int)(cX).size() + 1;
  }
  for (auto& a : vY) {
    cY[a] = (int)(cY).size() + 1;
  }
  vector<pair<int, int>> xSor[3], ySor[3];
  for (int i = 0; i < (N); i++) {
    pair<int, int> cur = {cX[X[i]], cY[Y[i]]};
    xSor[C[i] - 1].push_back({cur.first, cur.second});
    ySor[C[i] - 1].push_back({cur.second, cur.first});
  }
  for (int i = 0; i < (3); i++) {
    sort(xSor[i].begin(), xSor[i].end());
    sort(ySor[i].begin(), ySor[i].end());
    for (int j = 0; j < (N / 3); j++) {
      A[0][i][0].push_back(xSor[i][j].first);
      A[0][i][1].push_back(xSor[i][j].second);
      A[1][i][0].push_back(ySor[i][j].second);
      A[1][i][1].push_back(ySor[i][j].first);
    }
  }
  int lo = 1, hi = N / 3;
  while (lo < hi) {
    int mid = (lo + hi + 1) / 2;
    if (ok(mid)) {
      lo = mid;
    } else
      hi = mid - 1;
  }
  cout << lo * 3 << nl;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int T = 1;
  while (T--) {
    solve();
  }
  return 0;
}

```