#gcd of permutation from math import gcd def main(): s = input() M = int(s) a = list(map(int, list(s))) a.sort() N = len(a) if a[0] == a[-1]: print(int(s)) return else: ans = 0 G = 9*(a[-1] - a[0]) for i in range(1, G+1): if G % i == 0: if M % i == 0: ans = i print(ans) return if __name__ == "__main__": main()