#include #include #include using namespace std; int gcd(int a, int b){ while(b){ int tmp = b; b = a % b; a = tmp; } return a; } bool modEqZero(string s, int x){ int r = 0; for(int i = 0; i < s.size(); i++) r = (r * 10 + s[i] - '0') % x; return r == 0; } int main(){ string s; cin >> s; bool inc[10] = {false}; int g = -1; for(int i = 0; i < s.size(); i++) inc[s[i] - '0'] = true; for(int i = 0; i < 10; i++) for(int j = i + 1; j < 10; j++) if(inc[i] && inc[j]) g = g == -1 ? 9 * (j - i) : gcd(g, 9 * (j - i)); if(g == -1){ cout << s << endl; return 0; } vector d; for(int i = 1; i * i <= g; i++) if(g % i == 0){ d.emplace_back(i); if(i != g / i) d.emplace_back(g / i); } sort(d.begin(), d.end()); for(int i = d.size(); i--;) if(modEqZero(s, d[i])){ cout << d[i] << endl; return 0; } }