import scala.collection.mutable

object Main extends App {

  import scala.io.StdIn._
  solve
  def solve: Unit = {
    def readWhile(func: String ⇒ Boolean): List[String] = {
      val line = readLine.trim
      if (!func(line)) Nil
      else line::readWhile(func)
    }
    def sub(lines: List[String], linePos: Int = 1)(implicit state: Map[Char, A] = Map[Char, A]()): Option[Int] = {
      lines match {
        case line::t ⇒
          line.toList match {
            case array DeclarationAs name ⇒ sub(t, linePos + 1)(state + ((name, array)))
            case Assignment(b) ⇒ if (b) sub(t, linePos + 1) else Some(linePos)
            case _ ⇒ ???
          }
        case Nil ⇒ None
      }
    }
    val line = readLine.trim
    if (line == ".") return
    sub(line::readWhile(_ != ".")) match {
      case Some(pos) ⇒ println(pos)
      case None ⇒ println(0)
    }
    solve
  }
  type ~>[A, B] = PartialFunction[A, B]
  object DeclarationAs{
    def unapply(arg: List[Char]): Option[(A, Char)] = {
      arg match {
        case name ArrayName '['::(number Number ']'::Nil) ⇒ Some(new A(number), name)
        case _ ⇒ None
      }
    }
  }
  object Assignment{
    def unapply(arg: List[Char])(implicit map: Char ~> A): Option[Boolean] = {
      arg match {
        case name ArrayName '['::(index Expression ']'::'='::(value Expression Nil)) ⇒
          if (map.isDefinedAt(name) && index.isDefined && map(name).isInRange(index.get) && value.isDefined) {
            map(name)(index.get) = value.get
            Some(true)
          }else Some(false)
        case _ ⇒ None
      }
    }
  }
  object Expression{
    def unapply(arg: List[Char])(implicit map: Char ~> A): Option[(Option[Int], List[Char])] = {
      arg match {
        case n Number t ⇒ Some(Some(n), t)
        case name ArrayName '['::(e Expression ']'::t) ⇒ if (map.isDefinedAt(name) && e.isDefined && map(name).isDefinedAt(e.get)) Some(Some(map(name).apply(e.get)), t) else Some(None, t)
        case _ ⇒ None
      }
    }
  }
  object ArrayName{
    def unapply(arg: List[Char]): Option[(Char, List[Char])] = {
      arg match {
        case a::t if a.isLetter ⇒ Some(a, t)
        case _ ⇒ None
      }
    }
  }
  object Number {
    def unapply(arg: List[Char]): Option[(Int, List[Char])] = {
      arg.span(_.isDigit) match {
        case (Nil, _) ⇒ None
        case (n, t) ⇒ Some(n.mkString.toInt, t)
      }
    }
  }
  class A(size: Int){
    private val mMap: mutable.Map[Int, Int] = mutable.Map()
    def update(position: Int, value: Int) = mMap(position) = value
    def apply(position: Int): Int = mMap(position)
    def isInRange(position: Int): Boolean = (0 until size).contains(position)
    def isDefinedAt(position: Int): Boolean = mMap.isDefinedAt(position)
  }
  implicit class Extension[T](val value :T) extends AnyVal {
    @inline
    def let[A](@inline func: T ⇒ A): A = func(value)
    @inline
    def also(@inline func: T ⇒ Unit): T = {func(value); value}
  }
}
