require 'prime' n = gets.to_i a = gets.chomp.split h = {} a.sort.reverse.permutation do |perm| i = (perm*"").to_i next if h[i] h[i] = true if i.prime? p i exit end end p -1