class Hash
	alias :orig_ref :[]
	alias :orig_sub :[]=

	def [](x)
		y = orig_ref(x)
		raise "uninitialized" if !y
		y
	end

	def set_size(s)
		@s = s
	end

	def []=(i, x)
		raise "range exceed" if i >= @s
		orig_sub(i, x)
	end
end

loop do 
	((?a..?z).to_a + (?A..?Z).to_a).each do |char|
		self.instance_eval "@#{char} = {}"
	end

	line = gets.chomp
	i = 1
	occured = false
	break if line =~ /^\.$/
	loop do
		if line =~ /^([a-zA-Z])\[(\d+)\]$/
			eval "@#{$1}.set_size(#{$2})"
		else
			s = line.gsub(/([a-zA-Z])/, '@\1')
			begin
				eval(s)
			rescue
				puts i if !occured
				occured = true
			end
		end
		line = gets.chomp
		break if line =~ /^\.$/
		i += 1
	end
	puts 0 if !occured
end	