We need to read four integers: a,b and c,d. However careful: The problem statement: first line contains a and b (1 ≤ a,b ≤ 100). Second line contains c and d (1 ≤ c,d ≤ 100). But in description: Rick screams at times b, b + a, b + 2a, ... Morty at d, d + c, d + 2c, ... So essentially Rick: start = b, step = a. Morty: start = d, step = c. We need the smallest time t such that t >= b and t >= d? Actually the first time they scream at the same time could be after some screams. The times are t = b + ax, x >= 0 integer; and t = d + cy, y >= 0 integer. Find smallest t that satisfies both, or -1 if none.
Constraints small (up to 100) but t could be large? Actually a,b,c,d up to 100, but the LCM could be up to 100100 = 10000, but also offsets. Actually worst-case: a=100, c=99, b and d up to 100, we can brute force up to some bound. However, is it guaranteed that t will be <= something? Since steps and starts are up to 100, we can search up to maybe 10000? But better to compute using modular arithmetic. Check if there is solution: t ≡ b (mod a) and t ≡ d (mod c). This is a system of congruences. Since a and c might not be coprime, solution exists iff b ≡ d (mod gcd(a,c)). If exists, we can find smallest t >= max(b,d). Compute via enumerating x from 0 up to c-1? Actually we can iterate over one sequence up to some limit like ac? Since the sequences are linear, we can compute the set of times for one within a range up to LCM + max offset. But simpler: because values are small, we can simulate: for t from max(b,d) to some large limit (like 10000? But is it enough? Possibly t could be larger than 10000? Let's think: the condition for existence is modular. If solution exists, there is a solution t0 less than LCM(a,c) plus something? Actually the general solution: t ≡ t0 (mod lcm(a,c)). So there will be a solution in [0, lcm(a,c)-1] (if we consider residues) but we need t >= both b and d. However we can compute the smallest solution by considering the first few multiples of a and compare with the congruence. Since a,c <= 100, lcm(a,c) <= 10000 (since worst a=100, c=100 -> lcm=100). Actually 100 and 100 lcm = 100. So maximum lcm? a=100, c=99 => lcm = 9900. a=100, c=97 => 9700? Actually 100 and 97 are coprime? 100=2^25^2, 97 prime, so lcm=9700. So lcm max under 100 is 10099 = 9900 if they are coprime. But also 100 and 1? lcm=100. So lcm ≤ 10099? Actually 100 and 99 have gcd=1? gcd(100,99)=1, so lcm=9900. So max lcm = 9900? But also 100 and 100 => lcm=100. So max lcm is 9900. So we can safely set upper bound to 10000 or maybe 100000 to be safe? But wait, what about offsets b and d? They can be up to 100, so the smallest t could be slightly larger than lcm? For instance, b=100, d=100, a=100, c=99 => t must be >= 100. The solution t0 in modulo lcm is some residue r. The minimal t >= max(b,d) could be r + klcm. r could be less than max(b,d). So we may need to add multiples of lcm until t >= max(b,d). Since r < lcm <= 9900, and max(b,d) <= 100, t will be at most r + lcm if r < max(b,d) and we add one lcm. So t could be up to lcm + max(b,d) ≈ 9900+100 = 10000. So bound ~10000. So brute force up to maybe 20000 is safe. But better to compute directly.
Approach: Solve using CRT with non-coprime moduli.
Let g = gcd(a, c). Condition: (b - d) % g == 0. If not, output -1.
If condition holds, we can find t.
We need smallest t such that t ≡ b (mod a) and t ≡ d (mod c). There is a known method: let t = b + ax. Then b + ax ≡ d (mod c) => ax ≡ d - b (mod c). Let g = gcd(a, c). Since RHS divisible by g, we can divide: (a/g)x ≡ (d-b)/g (mod c/g). Then compute modular inverse of (a/g) modulo (c/g) (they are coprime). Then x = ((d-b)/g) * inv(a/g) mod (c/g). So we get x0 modulo (c/g). Then t = b + ax0. That gives a solution within [0, a(c/g) - 1? Actually x0 is the minimal nonnegative solution. t may be less than b? Actually x0 is minimal nonnegative residue, so t = b + ax0, which may be >= b? b is >=1, so t >= b. But could be less than d? If t < d, we might need to add multiples of lcm until t >= d as well? Actually t satisfies both congruences. It will be a solution but not necessarily the smallest that is also >= max(b,d). However any solution is congruent modulo lcm = ac/g. If t < max(b,d), we can add lcm until it becomes >= max(b,d). But is it possible that t < d? t satisfies t ≡ d (mod c) and t >= b? Wait t might be less than d. Since we only required congruence, t could be smaller than d. But the actual scream times: Morty starts at d, then d + c, d+2c,... So times for Morty are >= d. Similarly Rick's times are >= b. So we need t that is a scream time for both, meaning t must be >= b and >= d. However if t satisfies both congruences but is less than d, does that correspond to a scream time for Morty? For Morty, times are d, d + c, ... So negative multiples? Typically we consider x>=0. So t must be >= d. So we need the smallest t that is >= both b and d and satisfies the congruences. Since the set of solutions is arithmetic progression: t0 + k * lcm, where t0 is the smallest nonnegative solution (or some base solution). But if t0 < max(b,d), we need to add multiples of lcm until >= max(b,d). However it's possible that t0 is already >= max(b,d). That would be answer. Also there might be multiple solutions with different residues? Actually there is a unique residue modulo lcm, so all solutions are t0 + klcm. So we can compute t0 as described (the smallest nonnegative solution? Actually we need the smallest t that satisfies the congruences, but it might be negative if we allow negative x. Usually CRT gives the smallest nonnegative solution modulo lcm. So if we compute t0 modulo lcm (i.e., in range [0, lcm-1]), that t0 is the unique solution in that range. However it might be less than b or d. But is it guaranteed that there is some t >= max(b,d) that is exactly t0 + klcm? Yes, because by adding lcm we can get arbitrarily large. So answer = smallest t >= max(b,d) that is congruent to t0 modulo lcm. This can be computed as: if t0 < max(b,d), then answer = t0 + ceil((max(b,d) - t0)/lcm) * lcm. But careful: we need t0 to be a valid solution? But t0 must be a solution to the congruences. However we have to ensure that t0 is indeed a solution to the original equations with x,y >= 0? Actually if t0 is less than b or d, does it correspond to a scream time? For Rick, if t0 < b, then t0 = b + ax with x could be negative? Since x = (t0 - b)/a, which would be negative if t0 < b. So that would imply that t0 is not a valid Rick scream time because x must be >= 0. So we cannot simply use any t0 that satisfies congruences but less than b (or d) because it wouldn't correspond to nonnegative indices. So we need to find t such that t = b + ax, x>=0 and t = d + c*y, y>=0. That means t >= b and t >= d. So we need to find smallest t satisfying both linear equations with x,y integers >= 0.
The condition for existence is b ≡ d (mod g) but also we need that there exists x,y >=0. Even if congruence holds, there might be no solution with nonnegative indices? Actually if congruence holds, there exist integer solutions (x,y) but may be negative. For example, a=2, b=1, c=4, d=3. g=gcd(2,4)=2, b-d = -2 divisible by 2. Solve: t ≡ 1 mod 2, t ≡ 3 mod 4. Solutions: t = 3 mod 4? 3 mod 4 is 3,7,11,... but also 3 mod 2 is 1? 3 mod 2 =1. So t=3 is solution. But t=3 is >= b=1 and >= d=3, so ok. Another example: a=5,b=4, c=10,d=9. g=5, b-d=-5 divisible by5. Solve: t ≡4 mod5, t≡9 mod10. Solutions: numbers ≡9 mod10: 9,19,29,... 9 mod5 =4? 9 mod5=4 yes, so 9 is solution, but 9>=b=4 and >=d=9 so ok. Another: a=5,b=4, c=10,d=8. b-d=-4 not divisible by5? Actually gcd=5, -4 mod5=1 not divisible, so no solution. So it seems that if congruence holds, there is always a solution with t >= max(b,d)? Not always. Consider a=10,b=1, c=10,d=2. gcd=10, b-d=-1 not divisible by10, so no. So condition fails.
Consider a=6,b=4, c=10,d=2. gcd=2, b-d=2, divisible by2. Solve: t ≡4 mod6, t≡2 mod10. Find t? Let's compute. t mod lcm? lcm=30. We can find t? 4 mod6: numbers: 4,10,16,22,28,34,... 2 mod10: 2,12,22,32,... Intersection: 22. So t=22. b=4, d=2, t=22 >= both, ok. So works.
But could there be a case where congruence holds but smallest t satisfying both congruences is less than b or d, and then to get >= max(b,d) we add lcm and that t is >= max(b,d), but is it guaranteed that the resulting t corresponds to nonnegative x,y? Since adding lcm preserves congruence, t0 + klcm is still solution. But does it automatically give nonnegative x,y? For Rick: x = (t - b)/a. If t >= b, then x >=0? Actually t could be >=b but still (t-b) divisible by a? Since t ≡ b mod a, t-b is multiple of a. So (t-b)/a is integer. Since t >= b, it is nonnegative? If t >= b, then t-b >=0, so x >=0. Similarly for Morty, if t >= d, then y >=0. So as long as we ensure t >= max(b,d), x and y are automatically nonnegative. So indeed after we get the smallest nonnegative solution t0 (which may be less than max(b,d)), we can add multiples of lcm to reach at least max(b,d). That yields t = t0 + klcm, which is >= max(b,d) and satisfies congruences, and thus yields nonnegative indices.
But is t0 guaranteed to be >= 0? Yes. So the answer is: compute the smallest t >= max(b,d) that is congruent to the solution residue modulo lcm.
However, we must be careful: t0 computed as above might not be the smallest nonnegative solution? Actually we need to find any solution t_sol. Then answer = smallest t >= L = max(b,d) such that t ≡ t_sol (mod lcm). But if t_sol is any particular solution, we can compute k = ceil((L - t_sol)/lcm). If t_sol >= L, then answer = t_sol; else answer = t_sol + k*lcm.
But is it possible that t_sol is negative? If we compute using modular inverse method we can get t0 in [0, lcm-1] maybe. So that's fine.
We must also handle case where no solution exists: output -1.
Implementation plan:
- Read a,b,c,d.
- Let g = gcd(a,c).
- If (b - d) % g != 0: print -1 and return.
- Otherwise, find a solution t0.
Option 1: brute force loop t from max(b,d) to some upper bound (like 10000 or 100000) and check if (t - b) % a == 0 and (t - d) % c == 0. Since bounds are small, this is simplest. Complexity: at most 10000 iterations, trivial. I think it's safe.
Option 2: compute using CRT. But for reliability, I'll implement brute force because constraints are tiny. However note: what if the first common scream occurs beyond 10000? With a,b,c,d <= 100, could it be > 10000? Let's check worst-case scenario: a and c are coprime with values 99 and 100. LCM = 9900. b and d could be 100 each. The smallest solution t0 is the unique solution modulo 9900, but we need t >= 100. It could be that t0 is, say, 0? But if b=1, d=2? Let's check extremes: a=100,b=100, c=99,d=99. Solve t ≡100 mod100 => t=100,200,...; t≡99 mod99 => t=99,198,297,... Intersection? 100 mod100=0? Actually 100 mod100=0, not 100? Wait t ≡ b (mod a) means t mod a = b mod a. Since b < a? Actually b can be up to 100, but a also up to 100. It could be that b >= a? For example, a=20,b=2 (given). b=2 <20. Usually we think of b as starting time, but b could be larger than a? The problem says "Rick screams at times b, b + a, b + 2a, ..." So b is the first scream time, so b can be any number from 1 to 100. It doesn't have to be less than a, but it's possible b >= a. For example, a=5,b=7: times: 7,12,17,... So congruences are t ≡ b (mod a). But b mod a is the residue. So effectively, the condition for existence is (b - d) % g == 0. The smallest solution t0 might be less than b? Could t0 be less than b? Possibly if b is large, but since b is up to 100, and t0 is in [0, lcm-1], t0 could be less than b. For example, a=10,b=15, c=6,d=3. g=gcd(10,6)=2, b-d=12 divisible by 2. t ≡15 mod10 => t ≡5 mod10; t≡3 mod6. Solve: numbers ≡3 mod6: 3,9,15,21,... 15 mod10=5, so t=15 is solution. t0=15, which is >= b=15? Actually b=15, so t0=15 equals b. Another: a=10,b=5, c=6,d=9. b=5,d=9. t≡5 mod10 => residues 5,15,25,...; t≡9 mod6 => residues 9,15,21,... Intersection 15. b=5, d=9, t0=15 > both. Another: a=10,b=5, c=10,d=15. b-d = -10 divisible by 10? gcd=10, -10%10=0 yes. t≡5 mod10, t≡15 mod10? Actually 15 mod10=5, so all numbers ≡5 mod10. Intersection: any number ≡5 mod10. Smallest such is 5? But 5>= b=5, and 5 >= d=15? No, 5 < d=15. So t=5 is solution to congruences? Check: Rick: 5 is b? b=5, so yes Rick screams at 5. Morty: times start at 15, then 25,... So 5 is not a Morty scream time because Morty's first scream is at 15. So t=5 does not correspond to Morty's scream because it's before d. However, t=5 satisfies t ≡ d (mod c)? d=15, c=10 => 5 mod10 =5, 15 mod10=5, so t=5 ≡15 (mod10). But Morty's scream times are d + cy, with y>=0 integer. For y = -1, d + c(-1) = 5, but y cannot be negative. So t must be >= d to be a valid Morty scream. So t=5 is not valid. So we need t >= d. Next solution is 5+LCM? LCM=10? Actually lcm(10,10)=10. So t=15 is the next (5+10=15). t=15 >= d=15, and >= b=5. So answer =15. In this case, t0 (the smallest nonnegative solution) might be 5? But if we take t0 as the solution modulo lcm, there is a unique residue 5 mod10. The smallest nonnegative is 5, but that is < max(b,d)=15. So we need to add lcm to get to 15. So answer = 5 + 1*10 = 15. So it works.
Thus brute force up to some bound like maybe lcm + max(b,d) is enough. But lcm max = 9900, max(b,d)=100, so 10000 is enough. But consider a=100,b=1, c=99,d=100. g=gcd(100,99)=1, so sol exists. Find t: t ≡1 mod100, t≡100 mod99 => 100 mod99 =1, so t ≡1 mod99 as well. So t ≡1 mod lcm=9900? Actually lcm=9900, t ≡1 mod 9900? Check: 1 mod100 and 1 mod99 => by CRT, t ≡1 mod9900. So smallest t =1. But max(b,d)=100, so we need t >=100. Then answer = 1 + ceil((100-1)/9900)9900 = 1 + 19900 = 9901? Because ceil((99)/9900)=1? Actually (100-1)=99, 99/9900=0.01, ceil=1, so 1+9900=9901. Check: 9901 ≡? 9901 mod100 = 1? 9900 mod100=0 => 9901 mod100=1, OK. 9901 mod99: 99100=9900, remainder 1, OK. So t=9901. But is this the first common scream time? Let's verify Rick: times 1,101,201,... Actually b=1, a=100: 1,101,201,... Morty: d=100, c=99: 100,199,298,... We need to find common. Rick: sequence: 1,101,201,301,401,501,601,701,801,901,1001,1101,... Actually careful: 1 + 100k. Morty: 100 + 99k. Find intersection: solve 1+100x = 100+99y => 100x - 99y = 99 => 100x = 99(y+1). Since 100 and 99 are coprime, x must be multiple of 99? Actually solve: 100x ≡ 99 mod 99 => 100x ≡ 0 mod99? Wait mod99: 100 ≡1 mod99 => x ≡ 99 mod99 => x ≡0? Let's solve: 1+100x = 100+99y => 100x - 99y = 99. This Diophantine: 100x = 99y+99 => 100x = 99(y+1) => since gcd(100,99)=1, 99 divides x. So let x=99k => then 10099k = 99(y+1) => 100k = y+1 => y = 100k -1. For k=1, x=99, y=99, t=1+10099=9901. So indeed first common scream is 9901. That's > 10000? Actually 9901 < 10000? 9901 < 10000, yes. So 10000 bound would cover 9901. But we need to ensure upper bound covers worst-case where t could be up to lcm + max(b,d). In that example, lcm=9900, max(b,d)=100, t=9901 which is lcm+1? Actually 9900+1=9901. So it's less than lcm+max? lcm+max = 10000. So 10000 is enough. But consider scenario where b and d are both 100, a=100,c=99. Then t0 maybe 1? Actually as above? Wait b=100,d=100? Then Rick: times 100,200,... (100 mod100 = 0? Actually b=100, a=100 => times: 100,200,300,... So Rick's congruence: t ≡ 100 mod100 => t ≡0 mod100? Because 100 mod100 =0. So t ≡ 0 mod100. Morty: d=100,c=99 => 100 mod99 =1 => t ≡1 mod99. Solve t ≡0 mod100, t≡1 mod99. Find smallest t? Using CRT: t = ? Let's compute: t = 100x, need 100x ≡1 mod99 => 100 mod99 =1 => x ≡1 mod99 => smallest x=1 => t=100. Check Morty: 100 is first Morty scream? d=100, yes. So t=100 is common scream. So answer 100. That's small. So worst-case seems when t0 is much smaller than b/d, forcing addition of one lcm. But t0 is at most lcm-1. So t = t0 + lcm maybe up to 2lcm -1? Actually if t0 < max(b,d), we might need to add multiple lcm's? Ceil((max - t0)/lcm) could be >1 if max(b,d) - t0 > lcm. Since max(b,d) ≤ 100 and lcm ≤ 9900, max - t0 ≤ 100 - 0 = 100, which is less than lcm for lcm >= 101? But lcm could be as small as, say, a=1, c=1, lcm=1. Then max(b,d) up to 100, t0 could be e.g., 0? But then we might need to add many lcm's. Example: a=1,b=100, c=1,d=1. Rick: times every integer starting at 100: 100,101,102,... Morty: times every integer starting at 1: 1,2,3,... They will first scream together at time 100? Actually Morty screams at all integers from 1 onward; Rick starts at 100, so common at 100. That's answer 100. But using CRT: a=1, b=100 => t ≡100 mod1 (any integer). c=1,d=1 => t ≡1 mod1 (any integer). So any t works. Smallest t >= max(b,d)=100 is 100. Our method: g=gcd(1,1)=1, condition holds. Compute t0? But careful: When a=1, modulus 1, CRT is degenerate. We need to handle special cases. But brute force up to some bound would also work. But if we use the formula, we might have issues with modulus 1. However brute force up to some bound might be easier.
But what about a=1,b=1, c=1,d=2? They will meet at? Rick: 1,2,3,... Morty: 2,3,4,... First common is 2. That's fine. So brute force up to 10000 should be sufficient for all inputs? But consider a=100,b=1, c=1,d=100? Wait c=1 means Morty screams at all times starting from d=100? Actually c=1 means step 1: times 100,101,102,... Rick: 1,101,201,... First common: 101? Check: Rick: 1,101,... Morty: 100,101,... So answer 101. That's small. So seems bound 10000 is safe.
But is there any scenario where the first common scream time exceeds 10000? Let's search worst-case: a and c are coprime and near 100, b and d are small, but t0 maybe small? Actually if t0 is small (e.g., 0 or 1) and max(b,d) = 100, then t = t0 + klcm where k = ceil((max - t0)/lcm). Since lcm could be up to 9900, (max - t0) ≤ 100, so k=1 if max - t0 > 0? Actually if t0=0 and max=100, then ceil((100-0)/9900) = ceil(100/9900)=1? Wait 100/9900 < 1, ceil gives 1? Actually ceil(0.0101...) = 1. So k=1, t = 0+9900 = 9900, but t=9900 < max? 9900 > 100. But we need t >= max, and 9900 >= 100, so okay. But t=9900 may not be the smallest? Because if t0=0 and we add lcm once, t=9900 which is >=100. But is there any solution between t0 and t0+lcm that is also >= max? t0 is the only solution in [0, lcm-1]? Actually t0 is the unique solution modulo lcm, so there is exactly one solution in each interval of length lcm. So if t0 < max, the next solution is t0 + lcm. So answer is t0 + lcm. That could be up to 9900 + (lcm?) Actually t0 could be up to lcm-1, but if t0 is small, t0 + lcm = lcm + small. The maximum possible lcm is 9900, so answer maximum is 9900 + (lcm-1) ≈ 29900 -1 = 19799? Wait no, if t0 is near lcm-1, and max(b,d) is say 100, then t0 might be > max, so answer = t0, which could be up to lcm-1 = 9899. If t0 < max, answer = t0 + lcm, which could be up to (lcm-1) + lcm = 2lcm - 1. But could t0 be less than max? t0 is the unique solution modulo lcm in [0, lcm-1]. Since max(b,d) <= 100, and lcm can be as small as say 1? Actually lcm can be 1 (if a=c=1). Then t0 is unique solution modulo 1, meaning all integers are solutions? Actually when modulus 1, the residue is 0 mod 1 (any integer). So we can take t0=0 as a representative. Then answer = smallest t >= max(b,d) that is congruent to 0 mod 1, which is simply max(b,d). That would be <=100. So answer <=100. But when lcm is small, t0 + lcm is also small. The worst-case for answer would be when t0 is small and lcm is large, and we need to add lcm to exceed max. That yields answer ≈ lcm + small. Since lcm max is 9900, answer max ≈ 9900 + (max possible small). But if lcm=9900, t0 could be 0? Could t0 be 0? Is 0 a solution? t0 must satisfy t0 ≡ b (mod a) and t0 ≡ d (mod c). Since b,d >=1, t0=0 would not satisfy unless b ≡0 mod a and d ≡0 mod c. For b and d up to 100, it's possible that b ≡0 mod a and d ≡0 mod c. For example, a=100, b=100, c=99, d=99? b=100 mod100=0, d=99 mod99=0. Then t0 ≡0 mod100 and ≡0 mod99 => t0 ≡0 mod lcm=9900. So t0 could be 0. Indeed smallest nonnegative solution is 0. Then max(b,d)=100, answer = 0 + 9900 = 9900. Check: Rick: start b=100, so times 100,200,... Morty: start d=99, times 99,198,... Intersection? 9900? Rick: 10099=9900? Actually Rick: 100 + 100k: for k=98 => 9900? 100+100*98 = 9900? 100+9800=9900 yes. Morty: 99 + 99k: for k=99 => 99+9801=9900. So yes, they meet at 9900. So answer could be 9900. That's within 10000.
But could answer be larger than 10000? Consider lcm=9900, t0 maybe 1? Then answer = 1 + 9900 = 9901 (as earlier). That's still <10000. So max answer maybe 9900+9899? That would be if t0 is just below max? Actually if t0 < max, we add lcm. t0 can be as large as lcm-1 = 9899. But if t0 is 9899, then t0 >= max? Since max <=100, yes it is >= max. So answer = t0 itself, which is at most 9899. So the scenario where we add lcm only occurs when t0 < max. The maximum possible t0 that is still < max is max-1 <= 99. So the largest answer in that case is (max-1) + lcm. Since max <=100, max-1 <=99, lcm <= 9900, so answer <= 9900+99 = 9999. Actually if max=100, t0=99, then answer = 99 + 9900 = 9999. If t0=0, answer = 9900. So maximum answer appears to be 9999? Let's test: a=100,b=1? To get t0=99, need t0 ≡ b mod a and ≡ d mod c. Since t0=99, b mod a = 99 mod100 = 99, so b could be 99. d mod c = 99 mod c? For t0=99 to satisfy d mod c, we need d ≡ 99 mod c, with c <=100, d <=100. So possible? For instance, a=100,b=99; c=99,d=0? But d must be >=1. d could be 99? Then t0=99 ≡ 99 mod99? 99 mod99=0, so d would have to be 0, not allowed. For t0=99 to satisfy d mod c, we need d ≡ 99 mod c. Since d is 1..100, we can choose c small such that 99 mod c equals something <=100. For example, c=50, then 99 mod50=49, so d would need to be 49. That's allowed. And b=99 mod100 =99, so b=99. Then t0=99 satisfies both congruences. Check: a=100,b=99 => Rick: times 99,199,... c=50,d=49 => Morty: times 49,99,149,... So they meet at 99, which is t0=99, answer=99 (since max(b,d)=99? b=99,d=49 -> max=99, answer=99). So no addition needed. To have t0 < max, we need t0 less than both b and d? Actually we need t0 < max(b,d). For t0=99 and max=100, we need b and d such that max=100 >99. For example, b=100, d=100, t0 might be 0? Let's construct: a=100,b=100 => Rick's residue 0 mod100; c=99,d=99 => Morty's residue 0 mod99; t0=0, max=100 => answer=9900. That's 9900 < 9999. Could we get answer=9999? We need t0 = some value less than max, say max=100, t0=99, then answer = 99 + lcm. lcm would be 9900? That would give 9999. But to have t0=99, we need t0 ≡ b mod a and ≡ d mod c. For a=100, b ≡ 99 mod100 => b = 99 or 199? But b must be 1..100 so b=99. So b=99. For c, we need 99 ≡ d mod c => d ≡ 99 mod c. Since d <=100 and >=1, if c=100, then 99 mod100 =99, so d=99. Then b=99,d=99, max=99, not 100. So max=99, not 100. To have max=100, we need either b=100 or d=100 (or both). If b=100, then residue mod100 is 0, so t0 cannot be 99 because 99 mod100=99, not 0. So b cannot be 100 if t0=99. So b=99. d could be 100? If d=100, then residue mod c: we need 99 ≡ 100 mod c => 100-99 =1, so c must divide 1 => c=1. Then lcm(100,1)=100. t0=99? But we must check if t0=99 satisfies both: Rick: b=99 mod100 -> 99; Morty: c=1,d=100 -> any integer works (since modulo 1 all numbers are congruent). The smallest nonnegative solution is t0=99? Actually with c=1, the condition t ≡ d mod 1 is always true. So t must satisfy t ≡ 99 mod100. The smallest nonnegative solution is 99. So t0=99. Then max(b,d)=max(99,100)=100. t0=99 < 100, so answer = t0 + lcm = 99 + lcm(100,1)=99+100=199. That's far less than 9999. So lcm small. To get large answer, we need both lcm large and t0 < max. lcm large requires a and c to be near 100 and coprime. To have t0 < max, we need the unique residue modulo lcm to be small (less than max). Since max <= 100, that's likely if b and d are such that the solution is small. For lcm large, t0 is the unique number in [0, lcm-1] satisfying the congruences. That number could be as small as 0, 1, 2, ... up to maybe 99. So it's possible that t0=99 while lcm=9900, giving answer=9999. But we need to check if there exist a,b,c,d with a,c up to 100, b,d up to 100, such that lcm=9900 (i.e., a and c are 100 and 99 in some order, coprime) and the solution t0 is 99. Let's attempt: a=100, c=99. Need t0 ≡ b (mod 100) and t0 ≡ d (mod 99). We want t0 = 99. Then b ≡ 99 mod100 => b=99 (since b in 1..100). d ≡ 99 mod99 => d ≡ 0 mod99 => d must be 99? Because 99 mod99 =0, and d between 1 and 100 inclusive, so d could be 99 or? Actually any number ≡0 mod99 within 1..100 is 99 only (since 0 is not allowed? 0 not in range). So d=99. Then b=99, d=99, max=99. So max=99, not 100. So t0=99 equals max, so answer=99, not 9999. So t0 cannot be exactly 99 and max=100 with lcm=9900, because if t0=99, then b=99 and d must be 0 mod99 which forces d=99, giving max=99. If we try a=99, c=100. Then t0=99: For a=99, b ≡ 99 mod99 => b ≡ 0 mod99 => b could be 99? Actually b mod99=0 => b=99. d ≡ 99 mod100 => d=99. Same result. So t0=99 gives max=99.
What about t0=98? With a=100, c=99: b≡98 mod100 => b=98; d≡98 mod99 => 98 mod99 =98, so d=98 (since 1..100). So b=98,d=98 => max=98. So again t0 equals max.
Generally, if t0 < 100, then b = t0 mod a (with b in 1..100) and d = t0 mod c (with d in 1..100). Since a and c are at most 100, t0 mod a and t0 mod c are just t0 if t0 < a and t0 < c? Actually t0 is less than 100, and a and c are up to 100, possibly > t0. For t0=0, b mod a is 0, but b must be between 1 and 100. So b cannot be 0. So t0 cannot be 0 unless b= a? Actually b mod a = 0 means b is a multiple of a. Since b is in 1..100, b could be a, 2a, ... but b can be up to 100. So b could be 100 if a=100, then b mod100 = 0. So t0=0 is possible with b=100 and d=99? Check: a=100,b=100 => 0 mod100; c=99,d=99 => 0 mod99. So t0=0 works. Then max(b,d)=100, t0=0 <100, answer = 0+9900=9900. So answer 9900 possible.
What about t0=1? Then b≡1 mod100 => b=1; d≡1 mod99 => d=1? Actually 1 mod99 =1, so d=1. So b=1,d=1 => max=1, answer=1. So not large.
Thus for t0 small but less than max, max must be greater than t0. That requires that either b or d is > t0. But if t0 is small, say t0=0, then b must be such that b mod a =0, i.e., b is a multiple of a, and d mod c =0, i.e., d is a multiple of c. Since b and d are <=100, they can be a and c respectively, both could be <100? Actually a and c are up to 100. For a=100, b=100 works; for c=99, d=99 works; max=100 > 0. So that yields answer 9900. For t0=10, we would need b ≡10 mod a, d ≡10 mod c. Since b,d <=100, we could have b=10 if a>10, and d=10 if c>10. Then max=10, not >10. To have max > t0, we need either b > t0 or d > t0. But if b ≡ t0 mod a, and b is in 1..100, b could be t0 + ka. Since t0 is small, b could be t0 + a, which would be > t0. For example, t0=10, a=20: b ≡10 mod20 => b could be 10 or 30 or 50 etc. So b could be 30, which is >10. Similarly, d could be t0 + kc > t0. So it's possible that t0 is small but b and d are larger, giving max > t0. However, note that b and d are given, not derived from t0. In the CRT solution, t0 is the smallest nonnegative number satisfying the congruences. If b is large (e.g., 30) and a=20, then b mod20 = 10. So the residue class is 10 mod20. So t0 will be the smallest number that is 10 mod20 and also satisfies the other congruence. That smallest number could be 10 (if 10 satisfies the other congruence) or something else. For t0 to be 10, we need 10 ≡ d mod c. That could hold if d mod c = 10 mod c. For example, c=30, d=10? Then d mod30 =10, so 10 ≡10 mod30, t0=10. Then b=30? Wait b=30 with a=20 gives b mod20 =10, so b=30 works. So b=30, d=10, a=20, c=30. t0=10. max(b,d)=30 >10. So answer = t0 + lcm? lcm(20,30)=60. So answer=10+60=70. That's 70, not huge. But if we want lcm large, need a and c coprime and both near 100. For that, if a=99, c=100, we could have b=30? But b mod99 =30, d mod100= something. t0 would be the solution to t ≡30 mod99 and t ≡? We need to choose d such that the smallest solution is small. For a=99, c=100, t0 can be as small as? The CRT gives a unique solution modulo 9900. The smallest nonnegative solution could be anywhere from 0 to 9899. To get t0 small (like 10), we need to pick b and d such that the congruences yield a small number. For instance, b=10 (if a>10) but a=99, b mod99 =10, so b could be 10 (since 10 <99). d mod100 =? To get t0=10, need 10 ≡ d mod100, so d=10. Then b=10,d=10 => max=10, answer=10. So not large. For t0 to be small but max large, we need b or d to be large while residue is small. For example, a=99, b=109? But b max is 100, so b cannot exceed 100. So b is at most 100. For a=99, possible b values: any integer from 1 to 100. The residue b mod99 is either b if b<99, or b-99 if b=99? Actually 99 mod99 =0, 100 mod99 =1. So residues are between 0 and 98. So if b is 100, residue is 1. So b could be 100 with residue 1. Similarly, for c=100, d can be up to 100, residues d mod100 is d if d<100, and 0 if d=100. So d=100 gives residue 0.
Now to have t0 small but max large: Suppose we want t0=1. Then we need residue 1 mod99 and residue something mod100 that gives 1. For residue mod100, t0=1 means 1 mod100, so d must be 1 (since d mod100 =1). That forces d=1, which is small. So max would be max(b,d). b could be large? For residue 1 mod99, b must be ≡1 mod99, so b could be 1 or 100? Since 100 mod99=1, so b could be 100. So b=100, d=1 gives t0=1? Check: a=99,b=100 -> t ≡1 mod99; c=100,d=1 -> t ≡1 mod100; smallest solution is 1 (since 1 satisfies both). So t0=1. Then max(b,d)=max(100,1)=100. t0=1 < 100, answer = t0 + lcm = 1 + 9900 = 9901. That's 9901. So answer 9901 is possible. In our earlier example with a=100,b=1? Wait we had a=100,b=1? Let's check: a=100,b=100? Actually I set a=99,b=100 (Rick: start at 100? Actually Rick's a=99? Wait we need to be careful: Rick screams at times b, b + a, ... So a is step, b is start. So a=99, b=100 means Rick: 100, 199, 298,... That's times 100 mod99? 100 mod99 =1. So congruence t ≡ 100 (mod 99) => t ≡ 1 (mod 99). Morty: c=100, d=1 => times 1,101,201,... => t ≡ 1 (mod 100). So intersection: t ≡ 1 mod lcm(99,100)=9900, smallest positive is 1. But t=1 is less than b=100? Actually Rick's first scream is at 100, not at 1. So t=1 is not a valid Rick scream because it is before b. So we need t >= b. Our method: t0=1, but max(b,d)=100, answer = 1 + 9900 = 9901, which is >=100. So answer 9901 is valid. So yes, answer can be 9901. So maximum could be 9901? But we earlier found scenario with a=100,b=100,c=99,d=99 gave answer 9900. With a=99,b=100,c=100,d=1 gave answer 9901? Let's check if that input is valid: a=99, b=100; c=100, d=1. All numbers ≤100. Does Rick's screams: start at 100, step 99: times 100,199,298,... Morty: start 1, step 100: times 1,101,201,... They meet? Find first common time: Solve 100+99x = 1+100y => 99x -100y = -99 => 99x = 100y -99. Compute small numbers: check for y=1: 1+100=101; Rick: 100,199 -> 100,199 -> 101 not. y=2: 201; Rick: 199,298. y=3:301; Rick:298,397. Not. Let's solve: We need 100+99x = 1+100y => 99x = 100y -99 => 99x +99 = 100y => 99(x+1) = 100y. Since 99 and 100 coprime, y must be multiple of 99: y=99k, then 99(x+1)=10099k => x+1=100k => x=100k-1. Smallest k=1 gives y=99, x=99, t=1+10099=9901. So answer 9901. So indeed 9901 is achievable.
What about 9999? Could we get answer = 9999? We need t0 = some value less than max, and lcm=9900, so answer = t0 + 9900. To get 9999, t0 would be 99. But as argued, t0=99 leads to b=99 mod a and d=99 mod c. For a=99 and c=100? Let's attempt: a=99, b=?? For t0=99: need b ≡ 99 mod 99 => 0 mod99, so b could be 99 (since 99 mod99=0) or 198? but b ≤100 so b=99. For c=100, need d ≡ 99 mod100 => d=99. So b=99,d=99, max=99, answer=99, not 9999. For a=100, c=99: t0=99 => b ≡ 99 mod100 => b=99; d ≡ 99 mod99 => 0 mod99, so d=99. So again b=99,d=99, max=99, answer=99. So t0 cannot be 99 with one of b or d larger than 99 because the residues force b and d to be exactly 99 (or 0 mod? Actually for a=100, b ≡ 99 mod100, the possibilities for b (1..100) are 99 only? Since 99 mod100 =99, and 199 >100, so only 99. Similarly for c=99, d ≡ 99 mod99 => d ≡ 0 mod99, so d could be 99 (since 0 not allowed) or? 0 is not in [1,100], so only 99. So both are 99. So max=99. So t0=99 yields max=99. So answer = t0 (since t0 >= max? Actually t0=99, max=99, answer = t0 = 99) no addition. So 9999 not achievable. What about t0=98? Then b=98 (if a>98) and d=98 (if c>98) leads both to 98, max=98. So indeed when lcm is large (9900) and t0 is less than 100, the residues determine b and d uniquely as the same number (t0) if a and c > t0. But a and c are up to 100, t0 < 100, if both a and c > t0, then b must equal t0 (since b = t0 mod a = t0, because t0 < a and b in [1,100], could also be t0 + a, but that would be >= a > t0, but then b would be >100 if a>t0? For a=99, t0=98, b could be 98 or 98+99=197 >100, so only 98. So b is forced to be t0. Similarly d forced to be t0. So max = t0. So addition not needed. If one of a or c is <= t0, then maybe b could be t0 + a, but a <=100, t0 <100, t0 + a could be <= 100 + 99? But b max is 100. So t0 + a <= 100? Since a <= 100, t0 + a could exceed 100. For b to be within 1..100, if a > t0 and a + t0 >100, then the only possibility is b = t0. Let's examine possibility where a is small such that t0 mod a can be something else. For t0=98, if a=50, then 98 mod50 = 48. So b could be 48 or 48+50=98? Actually 48+50=98, which is t0? Wait 48+50=98, that's t0 itself? 98 mod50 = 48. So the residue class is numbers ≡48 mod50. The numbers in [1,100] that are ≡48 mod50 are 48 and 98. So b could be 48 or 98. So if we choose b=48, then b is not t0. But t0 is 98, but b=48 is a different number. But does that satisfy? For a=50, b=48, the congruence is t ≡48 mod50. For t0=98, 98 mod50 = 48, so it matches. So b can be 48 while t0=98. Similarly, d could be something else. So it is possible to have t0=98, b=48 (which is <98), and d maybe? Let's construct with lcm=9900, but lcm=9900 requires a and c to be 100 and 99 in some order. For a=100 and c=99, both > t0=98, then b must be either 98 or 98+100=198>100, so only 98. So that fails. For a=99 and c=100, similarly both >98, so b and d forced to be 98? Actually a=99 >98, so b could be 98 (since 98 mod99 =98) or 98+99=197 >100, so only 98. So again forced. So for lcm=9900, a and c are both close to 100, so they are > any t0 <100, thus b and d are forced to be t0. Thus max = t0. So answer = t0, which is <100. So for lcm=9900, if t0 <100, then necessarily b and d are both t0, so max=t0 and answer = t0. So the scenario where t0 < max and lcm=9900 cannot happen because max would be at most t0? Actually if b and d are both t0, then max = t0, not > t0. Could one of them be > t0 while the other equals t0? Let's see: For a=100 ( > t0), b could be t0 or t0+100? But t0+100 >100 (since t0 >=1, t0+100 >100) so b cannot be t0+100 because b <=100. So b must be t0. Similarly for c=99, d must be t0 (or t0+99 >100 unless t0=1? 1+99=100, that's possible! For c=99, if t0=1, then d could be 1 or 1+99=100. So there is a possibility: t0=1, a=100 (b must be 1? Actually b could be 1 or 1+100=101>100, so only 1). For c=99, d could be 1 or 100? Because 1 mod99 =1, and 1+99=100 is also ≡1 mod99 and within 1..100. So d could be 100. So we can have b=1, d=100. Then t0=1, max=100, answer = 1+9900 = 9901. That's the example we had earlier. So t0=1 is possible with one of b or d > t0. That yields answer 9901. Could we get t0=2? For a=100, b must be 2 (since 2+100=102 >100). For c=99, d could be 2 or 101? 101 >100, so only 2. So d forced to 2. So b=2,d=2 => max=2. So t0=2 yields max=2, answer=2. So only t0=1 gives an extra possibility because 1+99=100, which is within bound. Similarly, could t0 be something like? For a=99, c=100: t0=1 => a=99 >1, b could be 1 or 1+99=100 (since 100 <=100). So b could be 100. c=100, d must be 1 (since 1 mod100 =1, and 1+100=101>100, so only 1). So b=100, d=1 => max=100, answer=9901. So t0=1 works. t0=0? For a=100, b must be? 0 mod100 -> b could be 100? Actually b=100 gives residue 0. For c=99, d must be? 0 mod99 -> d could be 99? (0 mod99 gives 99, since 99 is multiple of 99? Actually 99 mod99 =0, and 99 is within 1..100. Also 0? not allowed. So d=99. So b=100,d=99 => max=100, t0=0, answer=9900. So t0=0 works. t0=0 gave answer 9900. t0=1 gave 9901. Could we get t0= something else that allows one of b or d to be larger than t0? For t0=k, we need either a > k but a + k ≤ 100 to allow b = k+a (which would be >k) OR c > k but c + k ≤ 100 to allow d = k + c > k. For a=100, a + k ≤ 100 => k=0 only. So a=100 can only give b > k if k=0? Actually b = k mod a, and b can be k or k+a. Since b ≤ 100, if k+a ≤ 100, then b could be k+a. For a=100, k+a = k+100 ≤ 100 => k ≤ 0, so only k=0. So for a=100, b cannot be k+a unless k=0 and b=100? But for k=0, b could be 0? b must be between 1 and 100, so b could be 100 (since 0 mod100 and 0+100=100). So that's the case. For c=99, k + 99 ≤ 100 => k ≤ 1. So for k=1, d could be 1+99=100. So that's how we got d=100 for k=1. For k=2, 2+99=101 >100, so only d=2