# frozen_string_literal: true x = gets.chomp.chars.map(&:to_i).sort.permutation.to_a largest = x[-1].map(&:to_s).join.to_i if x[-2][0].zero? p(-1) exit end candidate = x[-2].map(&:to_s).join.to_i if candidate if candidate == largest p(-1) else p candidate end else p(-1) end