// yukicoder: No.443 GCD of Permutation // 2019.5.8 bal4u #include #if 1 #define gc() getchar_unlocked() #define pc(c) putchar_unlocked(c) #else #define gc() getchar() #define pc(c) putchar(c) #endif int f[10]; char s[10003]; void ins() { int c; char *p = s; while (1) { *p++ = c = gc(); if (c <= ' ') break; f[c & 0xf]++; } *(p-1) = 0; } void out(int n) { int i; char ob[20]; if (!n) pc('0'); else { i = 0; while (n) ob[i++] = n%10 + '0', n/=10; while (i--) pc(ob[i]); } } void outs(char *s) { while (*s) pc(*s++); } int gcd(int a, int b) { int r; while (b != 0) r = a % b, a = b, b = r; return a; } int bigGCD(char *s, int b) { int a, r; r = 0; while (*s) { a = *s++ & 0xf; if (*s) a = a * 10 + (*s++ & 0xf); r = (r*100 + a) % b; } return gcd(b, r); } int main() { int i, j, g; ins(); j = 0; for (i = 0; i < 10; i++) if (f[i]) j++; if (j == 1) outs(s); else { g = 0; for (i = 0; i < 10; i++) if (f[i]) { for (j = i+1; j < 10; j++) if (f[j]) { if (g == 0) g = 9*(j-i); else g = gcd(g, 9*(j-i)); } } out(bigGCD(s, g)); } pc('\n'); return 0; }