### subroutines

def parse_decl(str)
  aname = str[$pos]
  $pos += 2
  num = parse_num(str)
  $pos += 1

  $decls[aname] = num
  true
end

def parse_asgn(str)
  aname = str[$pos]
  $pos += 2
  idx = parse_exp(str)
  return false if idx.nil? || $decls[aname].nil? || idx >= $decls[aname]
  $pos += 2
  
  val = parse_exp(str)
  return false if val.nil?

  $asgns[aname] = {} if $asgns[aname].nil?
  $asgns[aname][idx] = val
  true
end

def parse_exp(str)
  if str[$pos] >= '0' && str[$pos] <= '9'
    return parse_num(str)
  end

  aname = str[$pos]
  $pos += 2
  idx = parse_exp(str)
  $pos += 1

  if ! idx.nil? && ! $asgns[aname].nil? && ! $asgns[aname][idx].nil?
    return $asgns[aname][idx]
  end
  nil
end

def parse_num(str)
  num = 0
  while ! str[$pos].nil? && str[$pos] >= '0' && str[$pos] <= '9'
    num = num * 10 + (str[$pos].ord - '0'.ord)
    $pos += 1
  end
  num
end

### main

loop do
  $decls = {}
  $asgns = {}

  lines = []
  loop do
    line = gets.strip
    break if line == '.'
    lines << line
  end
  break if lines.empty?
  #p lines

  bug = nil

  for ln in (0...lines.length)
    line = lines[ln]

    if ! line.include?('=')
      $pos = 0
      parse_decl(line)
    else
      $pos = 0
      rc = parse_asgn(line)
      if ! rc
        bug = ln + 1
        break
      end
    end
  end

  puts (bug || 0)
end