def calc(a, b) return a[0] if b < 0 mask = 2 ** b p0, p1 = a.partition{|n| n & mask == 0} if p0.empty? then return calc(p1.map{|n| n ^ mask}, b - 1) elsif p1.empty? then return calc(p0, b - 1) else v1 = calc(p0.map{|n| n ^ mask}, b - 1) v2 = calc(p1, b - 1) return [v1, v2].min end end gets a = gets.split.map(&:to_i).uniq puts calc(a, 30)