#include #include #include #include #include using namespace std; int A, B; int main() { int a, b, t; cin >> a >> b >> t; int b2 = b, a2 = a; if (a > b)swap(a, b); vector > soinsu[2]; int i = 2; while (a >= i*i){ if (a%i == 0){ int cnt = 0; while (a%i == 0){ a /= i; cnt++; } soinsu[0].push_back(make_pair(i, cnt)); } else{ i++; } } soinsu[0].push_back(make_pair(a, 1)); i = 2; while (b >= i*i){ if (b%i == 0){ int cnt = 0; while (b%i == 0){ b /= i; cnt++; } soinsu[1].push_back(make_pair(i, cnt)); } else{ i++; } } soinsu[1].push_back(make_pair(b, 1)); int gcd = 1; vector >g; for (i = 0; i < soinsu[0].size();i++){ g.push_back(make_pair(soinsu[0][i].first,soinsu[0][i].second)); } for (int j = 0; j < soinsu[1].size(); j++){ for (i = 0; i < g.size(); i++){ if (soinsu[1][j].first == g[i].first){ g[i] = make_pair(g[i].first, max(g[i].second, soinsu[1][j].second)); break; } if (i == g.size() - 1)g.push_back(make_pair(soinsu[1][j].first, soinsu[1][j].second)); } } for (i = 0; i < g.size(); i++){ gcd *= pow(g[i].first, g[i].second); } //cout << gcd << endl; int t2 = t; t %= gcd; //cout << t << endl; int mn = 1000000001; int x; for (i = 0; i*b2 <= t; i++){ mn = min(mn, (a2 - (t - i*b2)%a2)%a2); } mn = min(mn, i*b2-t); cout << mn + t2 << endl; return 0; }