#include #include #include #include int main() { using namespace std; auto divide = [&](string s, int d) -> int { int res = 0; for (char c : s) { res = (res * 10 + (c - '0')) % d; } return res == 0; }; auto perm_gcd = [&](string s) -> string { int n = s.size(); sort(s.begin(), s.end()); if (s[0] == s[n - 1]) { return s; } int dif_gcd = 0, tot = 0; for (int i = 0; i < n; i++) { int c = s[i] - '0'; int nc = (i < n - 1) ? s[i + 1] - '0' : c; dif_gcd = (dif_gcd == 0) ? nc - c : gcd(dif_gcd, nc - c); tot = (tot + c) % 9; } if (divide(s, dif_gcd * 9)) { return to_string(dif_gcd * 9); } if (divide(s, dif_gcd * 3)) { return to_string(dif_gcd * 3); } if (divide(s, dif_gcd)) { return to_string(dif_gcd); } return "1"; }; string s; cin >> s; cout << perm_gcd(s) << endl; }