import scala.collection.{immutable, mutable}
import scala.io.StdIn._
object Main extends App {
  val n = readInt()
  val labels = mutable.Map[String, Int]()
  val nodes = Array.fill(n + 1){Nil: List[Edge]}
  val statements = Array.fill(n){
    readLine.trim.split(' ') match {
      case Array("goto", label) ⇒ Goto(label.dropRight(1))
      case Array(label) ⇒ Label(label.dropRight(1))
      case _ ⇒ ???
    }
  }
  for ((s, i) ← statements.zipWithIndex) {
    s match {
      case Label(name) ⇒
        labels(name) = i
        nodes(i) ::= Edge(i + 1, 0)
      case Goto(_) ⇒
        nodes(i) ::= Edge(i + 1, 1)
    }
  }
  for ((Goto(name), i) ← statements.zipWithIndex) {
    nodes(i) ::= Edge(labels(name), 0)
  }
  val minCost = Array.fill(n + 1){Int.MaxValue}
  minCost(0) = 0
  val queue = mutable.PriorityQueue[(Int, Int)]()(Ordering.by[(Int, Int), Int](_._1).reverse)
  queue.enqueue((0, 0))
  while (queue.nonEmpty) {
    val (current, cost) = queue.dequeue()
    if (minCost(current) == cost) {
      for (Edge(to, c) ← nodes(current) if minCost(to) > cost + c) {
        minCost(to) = cost + c
        queue.enqueue((to, cost + c))
      }
    }
  }
  println(minCost.last)
  case class Edge(to: Int, cost: Int)
  sealed trait Statement
  case class Label(name: String) extends Statement
  case class Goto(labelName: String) extends Statement
}
