#!/usr/bin/env python3 import fractions import functools import itertools def divisors(n): for i in range(1, n): if i * i > n: break elif n % i == 0: yield i yield n // i def gcd_of_permutations(n): if n < 10: return n ds = (int(d) for d in str(n)) xs = (9 * abs(a - b) for a, b in itertools.combinations(ds, 2)) g = functools.reduce(fractions.gcd, xs) if g == 0: return n else: return max(f for f in divisors(g) if n % f == 0) def main(): print(gcd_of_permutations(int(input()))) if __name__ == '__main__': main()