N = gets.to_i M = N.to_s.chars ans = (1..500).inject(N) {|memo, _| memo.gcd(M.shuffle.join.to_i)} puts ans