#!ruby -pl
def f a
!a[2]&&a[0]==a[1]||a.combination(3).any?{|p|b,c,d=p.sort
(b==c&&c==d||b+1==c&&c+1==d)&&(e=a.dup;p.map{|i|e.delete_at(e.index i)};f(e))}end
$_=(d=(1..9).find_all{|i|(l=$_.chars.map(&:to_i)<<i).count(i)<5&&f(l)}*" ")>""?d:0