#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005, MAXT = MAXN * 20;
int n, xx[MAXN], yy[MAXN], cnt[MAXT][3], ls[MAXT], rs[MAXT];
int rt[MAXN], per[3], chs[MAXN], nx, ny, tot, ans;
struct Po {
  int x, y, c, id;
} po[MAXN];
bool cmpx(const Po &x, const Po &y) { return x.x < y.x; }
int update(int pre, int x, int c, int l, int r) {
  int k = ++tot;
  ls[k] = ls[pre], rs[k] = rs[pre];
  memcpy(cnt[k], cnt[pre], sizeof(cnt[k]));
  ++cnt[k][c];
  if (l == r) return k;
  int mid = (l + r) >> 1;
  if (x <= mid)
    ls[k] = update(ls[pre], x, c, l, mid);
  else
    rs[k] = update(rs[pre], x, c, mid + 1, r);
  return k;
}
int query1(int k, int a, int b, int c, int l, int r) {
  if (!k || l > b || r < a) return 0;
  if (l >= a && r <= b) return cnt[k][c];
  int mid = (l + r) >> 1;
  return query1(ls[k], a, b, c, l, mid) + query1(rs[k], a, b, c, mid + 1, r);
}
int query(int x1, int y1, int x2, int y2, int c) {
  x1 = max(x1, 1);
  x2 = min(x2, nx);
  y1 = max(y1, 1);
  y2 = min(y2, ny);
  if (x1 > x2 || y1 > y2) return 0;
  return query1(rt[x2], y1, y2, c, 1, ny) -
         query1(rt[x1 - 1], y1, y2, c, 1, ny);
}
void setans(int x1, int y1, int x2, int y2, int c) {
  int cnt = 0;
  for (int i = 1; i <= n && cnt < ans; i++) {
    if (po[i].x >= x1 && po[i].y >= y1 && po[i].x <= x2 && po[i].y <= y2 &&
        po[i].c == c)
      chs[i] = 1, ++cnt;
  }
}
int cmax(int x1, int y1, int x2, int y2, int dep, bool out) {
  if (x1 > x2 || y1 > y2) return 0;
  if (dep == 2) {
    if (out) setans(x1, y1, x2, y2, per[2]);
    return query(x1, y1, x2, y2, per[2]);
  }
  int l = x1 - 1, r = x2 + 1;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    if (query(x1, y1, mid, y2, per[dep]) <=
        cmax(mid + 1, y1, x2, y2, dep + 1, 0))
      l = mid;
    else
      r = mid;
  }
  int a = query(x1, y1, l, y2, per[dep]);
  if (out && a >= ans) {
    setans(x1, y1, l, y2, per[dep]);
    cmax(l + 1, y1, x2, y2, dep + 1, 1);
    return a;
  }
  a = max(a, cmax(l + 2, y1, x2, y2, dep + 1, 0));
  if (out && a >= ans) {
    setans(x1, y1, l + 1, y2, per[dep]);
    cmax(l + 2, y1, x2, y2, dep + 1, 1);
    return a;
  }
  l = x1 - 1, r = x2 + 1;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    if (cmax(x1, y1, mid, y2, dep + 1, 0) <=
        query(mid + 1, y1, x2, y2, per[dep]))
      l = mid;
    else
      r = mid;
  }
  a = max(a, cmax(x1, y1, l, y2, dep + 1, 0));
  if (out && a >= ans) {
    cmax(x1, y1, l, y2, dep + 1, 1);
    setans(l + 1, y1, x2, y2, per[dep]);
    return a;
  }
  a = max(a, query(l + 2, y1, x2, y2, per[dep]));
  if (out && a >= ans) {
    cmax(x1, y1, l + 1, y2, dep + 1, 1);
    setans(l + 2, y1, x2, y2, per[dep]);
    return a;
  }
  l = y1 - 1, r = y2 + 1;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    if (query(x1, y1, x2, mid, per[dep]) <=
        cmax(x1, mid + 1, x2, y2, dep + 1, 0))
      l = mid;
    else
      r = mid;
  }
  a = max(a, query(x1, y1, x2, l, per[dep]));
  if (out && a >= ans) {
    setans(x1, y1, x2, l, per[dep]);
    cmax(x1, l + 1, x2, y2, dep + 1, 1);
    return a;
  }
  a = max(a, cmax(x1, l + 2, x2, y2, dep + 1, 0));
  if (out && a >= ans) {
    setans(x1, y1, x2, l + 1, per[dep]);
    cmax(x1, l + 2, x2, y2, dep + 1, 1);
    return a;
  }
  l = y1 - 1, r = y2 + 1;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    if (cmax(x1, y1, x2, mid, dep + 1, 0) <=
        query(x1, mid + 1, x2, y2, per[dep]))
      l = mid;
    else
      r = mid;
  }
  a = max(a, cmax(x1, y1, x2, l, dep + 1, 0));
  if (out && a >= ans) {
    cmax(x1, y1, x2, l, dep + 1, 1);
    setans(x1, l + 1, x2, y2, per[dep]);
    return a;
  }
  a = max(a, query(x1, l + 2, x2, y2, per[dep]));
  if (out && a >= ans) {
    cmax(x1, y1, x2, l + 1, dep + 1, 1);
    setans(x1, l + 2, x2, y2, per[dep]);
    return a;
  }
  return a;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x, y, c;
    scanf("%d%d%d", &x, &y, &c);
    po[i] = (Po){x, y, c - 1, i};
    xx[i] = x, yy[i] = y;
  }
  sort(xx + 1, xx + 1 + n);
  sort(yy + 1, yy + 1 + n);
  nx = unique(xx + 1, xx + 1 + n) - xx - 1;
  ny = unique(yy + 1, yy + 1 + n) - yy - 1;
  sort(po + 1, po + 1 + n, cmpx);
  for (int i = 1; i <= n; i++) {
    po[i].x = lower_bound(xx + 1, xx + nx + 1, po[i].x) - xx;
    po[i].y = lower_bound(yy + 1, yy + ny + 1, po[i].y) - yy;
    rt[po[i].x] = update(rt[po[i - 1].x], po[i].y, po[i].c, 1, ny);
  }
  per[0] = 0, per[1] = 1, per[2] = 2;
  do {
    ans = max(ans, cmax(1, 1, nx, ny, 0, 0));
  } while (next_permutation(per, per + 3));
  per[0] = 0, per[1] = 1, per[2] = 2;
  do {
    if (cmax(1, 1, nx, ny, 0, 1) >= ans) break;
  } while (next_permutation(per, per + 3));
  printf("%d\n", 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 = 300005, MAXT = MAXN * 20;
int n, xx[MAXN], yy[MAXN], cnt[MAXT][3], ls[MAXT], rs[MAXT];
int rt[MAXN], per[3], chs[MAXN], nx, ny, tot, ans;
struct Po {
  int x, y, c, id;
} po[MAXN];
bool cmpx(const Po &x, const Po &y) { return x.x < y.x; }
int update(int pre, int x, int c, int l, int r) {
  int k = ++tot;
  ls[k] = ls[pre], rs[k] = rs[pre];
  memcpy(cnt[k], cnt[pre], sizeof(cnt[k]));
  ++cnt[k][c];
  if (l == r) return k;
  int mid = (l + r) >> 1;
  if (x <= mid)
    ls[k] = update(ls[pre], x, c, l, mid);
  else
    rs[k] = update(rs[pre], x, c, mid + 1, r);
  return k;
}
int query1(int k, int a, int b, int c, int l, int r) {
  if (!k || l > b || r < a) return 0;
  if (l >= a && r <= b) return cnt[k][c];
  int mid = (l + r) >> 1;
  return query1(ls[k], a, b, c, l, mid) + query1(rs[k], a, b, c, mid + 1, r);
}
int query(int x1, int y1, int x2, int y2, int c) {
  x1 = max(x1, 1);
  x2 = min(x2, nx);
  y1 = max(y1, 1);
  y2 = min(y2, ny);
  if (x1 > x2 || y1 > y2) return 0;
  return query1(rt[x2], y1, y2, c, 1, ny) -
         query1(rt[x1 - 1], y1, y2, c, 1, ny);
}
void setans(int x1, int y1, int x2, int y2, int c) {
  int cnt = 0;
  for (int i = 1; i <= n && cnt < ans; i++) {
    if (po[i].x >= x1 && po[i].y >= y1 && po[i].x <= x2 && po[i].y <= y2 &&
        po[i].c == c)
      chs[i] = 1, ++cnt;
  }
}
int cmax(int x1, int y1, int x2, int y2, int dep, bool out) {
  if (x1 > x2 || y1 > y2) return 0;
  if (dep == 2) {
    if (out) setans(x1, y1, x2, y2, per[2]);
    return query(x1, y1, x2, y2, per[2]);
  }
  int l = x1 - 1, r = x2 + 1;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    if (query(x1, y1, mid, y2, per[dep]) <=
        cmax(mid + 1, y1, x2, y2, dep + 1, 0))
      l = mid;
    else
      r = mid;
  }
  int a = query(x1, y1, l, y2, per[dep]);
  if (out && a >= ans) {
    setans(x1, y1, l, y2, per[dep]);
    cmax(l + 1, y1, x2, y2, dep + 1, 1);
    return a;
  }
  a = max(a, cmax(l + 2, y1, x2, y2, dep + 1, 0));
  if (out && a >= ans) {
    setans(x1, y1, l + 1, y2, per[dep]);
    cmax(l + 2, y1, x2, y2, dep + 1, 1);
    return a;
  }
  l = x1 - 1, r = x2 + 1;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    if (cmax(x1, y1, mid, y2, dep + 1, 0) <=
        query(mid + 1, y1, x2, y2, per[dep]))
      l = mid;
    else
      r = mid;
  }
  a = max(a, cmax(x1, y1, l, y2, dep + 1, 0));
  if (out && a >= ans) {
    cmax(x1, y1, l, y2, dep + 1, 1);
    setans(l + 1, y1, x2, y2, per[dep]);
    return a;
  }
  a = max(a, query(l + 2, y1, x2, y2, per[dep]));
  if (out && a >= ans) {
    cmax(x1, y1, l + 1, y2, dep + 1, 1);
    setans(l + 2, y1, x2, y2, per[dep]);
    return a;
  }
  l = y1 - 1, r = y2 + 1;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    if (query(x1, y1, x2, mid, per[dep]) <=
        cmax(x1, mid + 1, x2, y2, dep + 1, 0))
      l = mid;
    else
      r = mid;
  }
  a = max(a, query(x1, y1, x2, l, per[dep]));
  if (out && a >= ans) {
    setans(x1, y1, x2, l, per[dep]);
    cmax(x1, l + 1, x2, y2, dep + 1, 1);
    return a;
  }
  a = max(a, cmax(x1, l + 2, x2, y2, dep + 1, 0));
  if (out && a >= ans) {
    setans(x1, y1, x2, l + 1, per[dep]);
    cmax(x1, l + 2, x2, y2, dep + 1, 1);
    return a;
  }
  l = y1 - 1, r = y2 + 1;
  while (l + 1 < r) {
    int mid = (l + r) >> 1;
    if (cmax(x1, y1, x2, mid, dep + 1, 0) <=
        query(x1, mid + 1, x2, y2, per[dep]))
      l = mid;
    else
      r = mid;
  }
  a = max(a, cmax(x1, y1, x2, l, dep + 1, 0));
  if (out && a >= ans) {
    cmax(x1, y1, x2, l, dep + 1, 1);
    setans(x1, l + 1, x2, y2, per[dep]);
    return a;
  }
  a = max(a, query(x1, l + 2, x2, y2, per[dep]));
  if (out && a >= ans) {
    cmax(x1, y1, x2, l + 1, dep + 1, 1);
    setans(x1, l + 2, x2, y2, per[dep]);
    return a;
  }
  return a;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x, y, c;
    scanf("%d%d%d", &x, &y, &c);
    po[i] = (Po){x, y, c - 1, i};
    xx[i] = x, yy[i] = y;
  }
  sort(xx + 1, xx + 1 + n);
  sort(yy + 1, yy + 1 + n);
  nx = unique(xx + 1, xx + 1 + n) - xx - 1;
  ny = unique(yy + 1, yy + 1 + n) - yy - 1;
  sort(po + 1, po + 1 + n, cmpx);
  for (int i = 1; i <= n; i++) {
    po[i].x = lower_bound(xx + 1, xx + nx + 1, po[i].x) - xx;
    po[i].y = lower_bound(yy + 1, yy + ny + 1, po[i].y) - yy;
    rt[po[i].x] = update(rt[po[i - 1].x], po[i].y, po[i].c, 1, ny);
  }
  per[0] = 0, per[1] = 1, per[2] = 2;
  do {
    ans = max(ans, cmax(1, 1, nx, ny, 0, 0));
  } while (next_permutation(per, per + 3));
  per[0] = 0, per[1] = 1, per[2] = 2;
  do {
    if (cmax(1, 1, nx, ny, 0, 1) >= ans) break;
  } while (next_permutation(per, per + 3));
  printf("%d\n", ans * 3);
  return 0;
}

```