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: import java.io.*;
  import java.util.HashMap;
  import java.util.Scanner;
  import java.util.StringTokenizer;
  import java.util.*;
  
  public class E {
      static long[][][][] cells;
  
      static long[][] lims;
  
      public static boolean check(int mid, int mask) {
          if (mask == 7)
              return true;
  //        System.out.println(Arrays.deepToString(lims) + " " + mid+" "+Integer.toBinaryString(mask));
          boolean ans = false;
          int bitc = Integer.bitCount(mask);
          boolean first = true;
          for (int i = 0; i < 3; i++) {
              if ((mask & (1 << i)) != 0) {
                  continue;
              }
              if (bitc != 0 && !first)
                  break;
              first = false;
              for (int j = 0; j < 2; j++) {
                  int rem = mid;
                  int idx = 0;
                  long[] lastCell = null;
                  while (rem > 0 && idx < cells[i][j].length) {
                      if (isValid(cells[i][j][idx])) {
                          lastCell = cells[i][j][idx];
                          rem--;
                      }
                      idx++;
                  }
                  if (rem != 0) {
                      continue;
                  }
                  long old = lims[j][0];
                  lims[j][0] = Math.max(lims[j][0], lastCell[j] + 1);
                  ans = ans || check(mid, mask | (1 << i));
                  lims[j][0] = old;
                  rem = mid;
                  idx = cells[i][j].length - 1;
                  lastCell = null;
                  while (rem > 0 && idx < cells[i][j].length) {
                      if (isValid(cells[i][j][idx])) {
                          lastCell = cells[i][j][idx];
                          rem--;
                      }
                      idx--;
                  }
                  if (rem != 0) {
                      continue;
                  }
                  old = lims[j][1];
                  lims[j][1] = Math.min(lims[j][1], lastCell[j] - 1);
                  ans = ans || check(mid, mask | (1 << i));
                  lims[j][1] = old;
              }
          }
          return ans;
      }
  
      private static boolean isValid(long[] cell) {
          boolean ans = true;
          for (int j = 0; j < 2; j++) {
              ans &= lims[j][0] <= cell[j] && cell[j] <= lims[j][1];
          }
  //        System.out.println(Arrays.deepToString(lims) + " " + Arrays.toString(cell) +" " +ans);
          return ans;
      }
  
      public static void main(String[] args) throws IOException {
          Reader sc = new Reader();
          PrintWriter pw = new PrintWriter(System.out);
          int n = sc.nextInt();
          int lo = 1;
          int hi = n / 3;
          cells = new long[3][2][n / 3][2];
          int[] size = new int[3];
  
          for (int i = 0; i < n; i++) {
              int x = sc.nextInt();
              int y = sc.nextInt();
              int clr = sc.nextInt() - 1;
              cells[clr][0][size[clr]][0] = x;
              cells[clr][0][size[clr]][1] = y;
              cells[clr][1][size[clr]][0] = x;
              cells[clr][1][size[clr]++][1] = y;
          }
          for (int i = 0; i < 3; i++) {
              for (int j = 0; j < 2; j++) {
                  int jj = j;
                  Arrays.sort(cells[i][j], (u, v) -> Long.compare(u[jj], v[jj]));
              }
          }
          long INF = (long) 1e10;
          int ans = 1;
          while (lo <= hi) {
              int mid = lo + hi >> 1;
              lims = new long[][]{{-INF, INF}, {-INF, INF}};
              if (check(mid, 0)) {
                  ans = mid;
                  lo = mid + 1;
              } else {
                  hi = mid - 1;
              }
          }
          pw.println(ans * 3);
          pw.close();
      }
  
  
      static class Reader {
          final private int BUFFER_SIZE = 1 << 16;
          private DataInputStream din;
          private byte[] buffer;
          private int bufferPointer, bytesRead;
  
          public Reader() {
              din = new DataInputStream(System.in);
              buffer = new byte[BUFFER_SIZE];
              bufferPointer = bytesRead = 0;
          }
  
          public Reader(String file_name) throws IOException {
              din = new DataInputStream(
                      new FileInputStream(file_name));
              buffer = new byte[BUFFER_SIZE];
              bufferPointer = bytesRead = 0;
          }
  
          public String readLine() throws IOException {
              byte[] buf = new byte[64]; // line length
              int cnt = 0, c;
              while ((c = read()) != -1) {
                  if (c == '\n') {
                      if (cnt != 0) {
                          break;
                      } else {
                          continue;
                      }
                  }
                  buf[cnt++] = (byte) c;
              }
              return new String(buf, 0, cnt);
          }
  
          public int nextInt() throws IOException {
              int ret = 0;
              byte c = read();
              while (c <= ' ') {
                  c = read();
              }
              boolean neg = (c == '-');
              if (neg)
                  c = read();
              do {
                  ret = ret * 10 + c - '0';
              } while ((c = read()) >= '0' && c <= '9');
  
              if (neg)
                  return -ret;
              return ret;
          }
  
          public long nextLong() throws IOException {
              long ret = 0;
              byte c = read();
              while (c <= ' ')
                  c = read();
              boolean neg = (c == '-');
              if (neg)
                  c = read();
              do {
                  ret = ret * 10 + c - '0';
              } while ((c = read()) >= '0' && c <= '9');
              if (neg)
                  return -ret;
              return ret;
          }
  
          public char nextChar() throws IOException {
              byte c = read();
              while (c != '.' && c != '*') {
                  c = read();
              }
              return (char) c;
          }
  
          public double nextDouble() throws IOException {
              double ret = 0, div = 1;
              byte c = read();
              while (c <= ' ')
                  c = read();
              boolean neg = (c == '-');
              if (neg)
                  c = read();
  
              do {
                  ret = ret * 10 + c - '0';
              } while ((c = read()) >= '0' && c <= '9');
  
              if (c == '.') {
                  while ((c = read()) >= '0' && c <= '9') {
                      ret += (c - '0') / (div *= 10);
                  }
              }
  
              if (neg)
                  return -ret;
              return ret;
          }
  
          private void fillBuffer() throws IOException {
              bytesRead = din.read(buffer, bufferPointer = 0,
                      BUFFER_SIZE);
              if (bytesRead == -1)
                  buffer[0] = -1;
          }
  
          private byte read() throws IOException {
              if (bufferPointer == bytesRead)
                  fillBuffer();
              return buffer[bufferPointer++];
          }
  
          public void close() throws IOException {
              if (din == null)
                  return;
              din.close();
          }
      }
  }