require 'prime' require 'set' def calc(a) sum = 0 a.each do |s| sum += s.split('').map(&:to_i).inject(:+) end return sum end n = gets.to_i arr = gets.chomp.split if arr.uniq.size == 1 p -1 exit elsif calc(arr) % 3 == 0 && !(n == 1 && arr[0].to_i == 3) p -1 exit end ans = -1 s = Set.new() arr.sort!.reverse! arr.permutation(n) do |a| x = a.join.to_i if s.add?(x).nil? next end if x.prime? ans = (x > ans ? x : ans) end end p ans