def gcd(m,n) n,m=m,n if m < n while n > 0 m,n = n,m % n end return m end cin = gets.chomp a = cin.chars.uniq.map(&:to_i).sort! if a.size==1 puts cin;exit 0 end d = 0 a.combination(2){|s| d = gcd(d,9*(s[1]-s[0])) } p gcd(cin.to_i,d)