require 'prime' n = gets.to_i a = gets.chomp.split best = -1 a.permutation do |perm| i = (perm*"").to_i best = [best, i].max if i.prime? end p best