#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 200;
const int mod = 1e9 + 7;
const int inf = 1e9;
int n, c[3], ans, fen[2][maxn];
pair<pair<int, int>, int> a[maxn];
vector<int> vec;
void upd(int f, int x, int y) {
  for (x += 5; x < maxn; x += x & -x) {
    fen[f][x] += y;
  }
  return;
}
int get(int f, int x) {
  int y = 0;
  for (x += 5; x > 0; x -= x & -x) {
    y += fen[f][x];
  }
  return y;
}
void solve() {
  fill(fen[0], fen[0] + n + 100, 0);
  fill(fen[1], fen[1] + n + 100, 0);
  sort(a, a + n);
  int x0 = 0, x1 = 0;
  for (int i = 0; i < n; i++) {
    if (a[i].second == c[0]) x0++;
  }
  for (int i = 0; i < n; i++) {
    if (i == 0 || a[i].first.first != a[i - 1].first.first) {
      int l = -1, r = n, mid;
      while (r - l > 1) {
        mid = (l + r) >> 1;
        int p1 = get(1, mid), p2 = x1 - get(0, mid);
        if (p1 <= p2)
          l = mid;
        else
          r = mid;
      }
      ans = max(ans, min({x0, get(1, l), x1 - get(0, l)}));
      ans = max(ans, min({x0, get(1, r), x1 - get(0, r)}));
    }
    if (a[i].second == c[0]) {
      x0--;
    }
    if (a[i].second == c[1]) {
      upd(0, a[i].first.second, 1);
      x1++;
    }
    if (a[i].second == c[2]) {
      upd(1, a[i].first.second, 1);
    }
  }
  fill(fen[0], fen[0] + n + 100, 0);
  fill(fen[1], fen[1] + n + 100, 0);
  x0 = 0, x1 = 0;
  for (int i = 0; i < n; i++) {
    if (a[i].second == c[0]) x0++;
  }
  for (int i = 0; i < n; i++) {
    if (i == 0 || a[i].first.first != a[i - 1].first.first) {
      int l = -1, r = n, mid;
      while (r - l > 1) {
        mid = (l + r) >> 1;
        int p1 = get(1, mid), p2 = x1 - get(0, mid);
        if (p1 <= p2)
          l = mid;
        else
          r = mid;
      }
      ans = max(ans, min({x0, get(1, l), x1 - get(0, l)}));
      ans = max(ans, min({x0, get(1, r), x1 - get(0, r)}));
    }
    if (a[i].second == c[0]) {
      x0--;
    }
    if (a[i].second == c[1]) {
      upd(0, a[i].first.first, 1);
      x1++;
    }
    if (a[i].second == c[2]) {
      upd(1, a[i].first.first, 1);
    }
  }
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i].first.first >> a[i].first.second >> a[i].second;
    vec.push_back(a[i].first.first);
  }
  sort((vec).begin(), (vec).end());
  vec.resize(unique((vec).begin(), (vec).end()) - vec.begin());
  for (int i = 0; i < n; i++) {
    a[i].first.first =
        lower_bound((vec).begin(), (vec).end(), a[i].first.first) - vec.begin();
  }
  vec.clear();
  for (int i = 0; i < n; i++) {
    vec.push_back(a[i].first.second);
  }
  sort((vec).begin(), (vec).end());
  vec.resize(unique((vec).begin(), (vec).end()) - vec.begin());
  for (int i = 0; i < n; i++) {
    a[i].first.second =
        lower_bound((vec).begin(), (vec).end(), a[i].first.second) -
        vec.begin();
  }
  c[0] = 1;
  c[1] = 2;
  c[2] = 3;
  do {
    for (int t = 0; t < 2; t++) {
      for (int o = 0; o < 2; o++) {
        solve();
        for (int i = 0; i < n; i++) {
          a[i].first.first = n - 1 - a[i].first.first;
        }
      }
      for (int i = 0; i < n; i++) {
        swap(a[i].first.first, a[i].first.second);
      }
    }
  } while (next_permutation(c, c + 3));
  cout << ans * 3;
  return 0;
}

### Prompt

Develop a solution in Cpp to the problem described below:

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>
using namespace std;
const int maxn = 1e5 + 200;
const int mod = 1e9 + 7;
const int inf = 1e9;
int n, c[3], ans, fen[2][maxn];
pair<pair<int, int>, int> a[maxn];
vector<int> vec;
void upd(int f, int x, int y) {
  for (x += 5; x < maxn; x += x & -x) {
    fen[f][x] += y;
  }
  return;
}
int get(int f, int x) {
  int y = 0;
  for (x += 5; x > 0; x -= x & -x) {
    y += fen[f][x];
  }
  return y;
}
void solve() {
  fill(fen[0], fen[0] + n + 100, 0);
  fill(fen[1], fen[1] + n + 100, 0);
  sort(a, a + n);
  int x0 = 0, x1 = 0;
  for (int i = 0; i < n; i++) {
    if (a[i].second == c[0]) x0++;
  }
  for (int i = 0; i < n; i++) {
    if (i == 0 || a[i].first.first != a[i - 1].first.first) {
      int l = -1, r = n, mid;
      while (r - l > 1) {
        mid = (l + r) >> 1;
        int p1 = get(1, mid), p2 = x1 - get(0, mid);
        if (p1 <= p2)
          l = mid;
        else
          r = mid;
      }
      ans = max(ans, min({x0, get(1, l), x1 - get(0, l)}));
      ans = max(ans, min({x0, get(1, r), x1 - get(0, r)}));
    }
    if (a[i].second == c[0]) {
      x0--;
    }
    if (a[i].second == c[1]) {
      upd(0, a[i].first.second, 1);
      x1++;
    }
    if (a[i].second == c[2]) {
      upd(1, a[i].first.second, 1);
    }
  }
  fill(fen[0], fen[0] + n + 100, 0);
  fill(fen[1], fen[1] + n + 100, 0);
  x0 = 0, x1 = 0;
  for (int i = 0; i < n; i++) {
    if (a[i].second == c[0]) x0++;
  }
  for (int i = 0; i < n; i++) {
    if (i == 0 || a[i].first.first != a[i - 1].first.first) {
      int l = -1, r = n, mid;
      while (r - l > 1) {
        mid = (l + r) >> 1;
        int p1 = get(1, mid), p2 = x1 - get(0, mid);
        if (p1 <= p2)
          l = mid;
        else
          r = mid;
      }
      ans = max(ans, min({x0, get(1, l), x1 - get(0, l)}));
      ans = max(ans, min({x0, get(1, r), x1 - get(0, r)}));
    }
    if (a[i].second == c[0]) {
      x0--;
    }
    if (a[i].second == c[1]) {
      upd(0, a[i].first.first, 1);
      x1++;
    }
    if (a[i].second == c[2]) {
      upd(1, a[i].first.first, 1);
    }
  }
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i].first.first >> a[i].first.second >> a[i].second;
    vec.push_back(a[i].first.first);
  }
  sort((vec).begin(), (vec).end());
  vec.resize(unique((vec).begin(), (vec).end()) - vec.begin());
  for (int i = 0; i < n; i++) {
    a[i].first.first =
        lower_bound((vec).begin(), (vec).end(), a[i].first.first) - vec.begin();
  }
  vec.clear();
  for (int i = 0; i < n; i++) {
    vec.push_back(a[i].first.second);
  }
  sort((vec).begin(), (vec).end());
  vec.resize(unique((vec).begin(), (vec).end()) - vec.begin());
  for (int i = 0; i < n; i++) {
    a[i].first.second =
        lower_bound((vec).begin(), (vec).end(), a[i].first.second) -
        vec.begin();
  }
  c[0] = 1;
  c[1] = 2;
  c[2] = 3;
  do {
    for (int t = 0; t < 2; t++) {
      for (int o = 0; o < 2; o++) {
        solve();
        for (int i = 0; i < n; i++) {
          a[i].first.first = n - 1 - a[i].first.first;
        }
      }
      for (int i = 0; i < n; i++) {
        swap(a[i].first.first, a[i].first.second);
      }
    }
  } while (next_permutation(c, c + 3));
  cout << ans * 3;
  return 0;
}

```