#include #include using namespace std; typedef long long ll; #define INF (ll)1e9 int main() { ll A, B, T; cin >> A >> B >> T; ll tmpA = A, tmpB = B; int i = 2, gcd = 1; while (tmpA >= i*i) { if (tmpA%i == 0 && B%i == 0) { int cnt = 0; while (tmpA%i == 0) { tmpA /= i; gcd *= i; } } i++; } if (tmpB%tmpA == 0)gcd *= tmpA; ll lcm = A*B / gcd; ll tmpT = T; tmpT %= lcm; if (tmpT + lcm <= T)tmpT += lcm; ll over_T = INF; for (int i = 0; i*B <= tmpT; i++) { over_T = min(over_T, (A - (tmpT - i*B) % A) % A); } int j = (tmpT / B) + 1; over_T = min(over_T, j*B - tmpT); cout << T + over_T << endl; return 0; }