object Main extends App {
  val sc = new java.util.Scanner(System.in)
  val n = sc.nextInt()
  val a = Array.fill(3 * n)(sc.nextInt()-1)

  def compose(x1: Int, x2: Int): Int = {
    x1 * n + x2
  }

  val dp = Array.fill(n * n)(Int.MinValue)
  val m1 = Array.fill(n)(Int.MinValue)
  val m2 = Array.fill(n)(Int.MinValue)
  var m = 0
  var ans = 0

  dp(compose(a(0), a(1))) = 0
  m1(a(0)) = 0
  m2(a(1)) = 0

  var update = List[(Int, Int, Int)]()

  for (i <- 1 until n) {
    val p = a(3 * i - 1)
    val q = a(3 * i)
    val r = a(3 * i + 1)
    if (p == q && q == r) {
      ans += 1
    } else {
      if (p == q || p == r || q == r) {
        val x = if (p == q || p == r) p else q
        val y = if (p == q) r else if (p == r) q else p
        for (i <- 0 until n) {
          update ::= (i, y, dp(compose(x, i)).max(dp(compose(i, x))) + 1)
        }
      }

      update ::= (q, r, dp(compose(p, p)) + 1)
      update ::= (p, r, dp(compose(q, q)) + 1)
      update ::= (p, q, dp(compose(r, r)) + 1)

      update ::= (p, q, m)
      update ::= (p, r, m)
      update ::= (q, r, m)

      for (i <- 0 until n) {
        update ::= (i, p, m1(i).max(m2(i)))
        update ::= (i, q, m1(i).max(m2(i)))
        update ::= (i, r, m1(i).max(m2(i)))
      }

      for ((i, j, v) <- update) {
        dp(compose(i, j)) = dp(compose(i, j)).max(v)
        m1(i) = m1(i).max(dp(compose(i, j)))
        m2(j) = m2(j).max(dp(compose(i, j)))
        m = m.max(dp(compose(i, j)))
      }
      update = Nil
    }
  }

  val last = a(3*n - 1)
  m = m.max(dp(compose(last, last)) + 1)

  println(ans + m)
}
