#!/usr/bin/ruby a,b=2.times.map{ gets.to_i.times.map{gets.chomp} } prefix=a.map{|e|e[0]}&b.map{|e|e[0]} (p -1;exit)if prefix.size>1 p -1