import java.io.PrintStream; import java.util.Scanner; public class Y443 { Scanner in = new Scanner(System.in); PrintStream out = new PrintStream(System.out); Y443() throws Exception { String s = in.next(); boolean[] flag = new boolean[10]; if (isRepetitive(s)) { out.println(s); return; } for (int i = 0; i < s.length(); i++) { flag[s.charAt(i) - '0'] = true; } long g = 0; for (int i = 0; i < 10; i++) { for (int j = i + 1; j < 10; j++) { if (flag[i] && flag[j]) { g = gcd(g, 9 * (j - i)); } } } for (int i = (int)g; i>0; i--) { if (g % i == 0 && divisible(s, i)) { out.println(i); return; } } } boolean isRepetitive(String s) { for (int i = 0; i < s.length(); i++) { if (s.charAt(0) != s.charAt(i)) { return false; } } return true; } boolean divisible(String s, int x) { int r = 0; for (int i = 0; i < s.length(); i++) { r = (r * 10 + s.charAt(i) - '0') % x; } return r == 0; } long gcd(long x, long y) { while (y > 0) { x %= y; long t = x; x = y; y = t; } return x; } public static void main(String argv[]) throws Exception { new Y443(); } }