n = gets.to_i a =[*1..10**n] b = [] a.each{|i|b << i if i % 3 == 0} a.map(&:to_s).each {|i| b << i if i.include?("3")} puts b.map(&:to_i).uniq.size