#include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair pii; typedef vector vi; int main() { string N; cin >> N; int n = N.size(); sort(N.begin(), N.end()); if (n <= 8) { int ans = 0; do { ans = __gcd(ans, stoi(N)); } while (next_permutation(N.begin(), N.end())); cout << ans << endl; return 0; } if (N[0] == N[n-1]) { cout << N << endl; return 0; } if (N[n-2] == '0') { cout << N[n-1] << endl; return 0; } int sum = 0; int cnt0 = 0, cnt2 = 0, cnt4 = 0, cnt8 = 0; for (char c : N) { int num = c-'0'; sum += num; if (num == 0) ++cnt0; else { if (num%2 == 0) ++cnt2; if (num%4 == 0) ++cnt4; if (num%8 == 0) ++cnt8; } } int ans = 1; if (sum%9 == 0) ans *= 9; else if (sum%3 == 0) ans *= 3; if (cnt8 > 0 && cnt8 + cnt0 == n) ans *= 8; else if (cnt4 > 0 && cnt4 + cnt0 == n) ans *= 4; else if (cnt2 > 0 && cnt2 + cnt0 == n) ans *= 2; cout << ans << endl; return 0; }