m=Hash.new{0} a=c=1 gets.chop.chars{|r|a=a*c/m[r]+=1;c+=1} p~-a%573