We are given two teams: Team UKU and Team Ushi (I think "Team Uku" might be a typo but it's consistent). The problem describes a game where initially each person has physical strength 2 (i.e., health 2). There is a common score, initially 0. The procedure alternates:
- Each person in Team UKU makes exactly 1 attack on someone in Team UKU (i.e., they attack within their own team? Actually the text says: "Each person in Team UKU makes exactly 1 attacks on someone in Team UKU for 1 ."Theremightbeamis−translation.Let′sreadcarefully:"1.EachpersoninTeamUKUmakesexactly 1 attacksonsomeoneinTeamUKUfor 1 1 eachandjust 1 2 teams,TeamUKUandTeamUshi.Initially,T N peopleandTeamUkuhas M people."Sosecondteamis"Ushi"maybe. 1 eachandjust 1 0 $, the game ends." In step 1: "when the number of team members reaches 0" - which team? It says: "when the number of team members reaches 0, the game ends." It might refer to the team being attacked? But let's parse carefully.
Given the examples: (20,10) output 0; (10,20) output 1; (64783,68943) output 4; (10^18,10^18) output 2.
We need to interpret the game and find the minimum possible common score at the end. Since the attacks are chosen by the players (presumably they can decide whom to attack strategically), we need to find the optimal strategy to minimize the common score. The common score increases by 1 after each full round (or half-round?) if the game hasn't ended.
Let's restate the rules clearly.
Game: Two teams: Team A (UKU) with N people, Team B (Ushi) with M people. Initially each person has health = 2. Common score S = 0.
Procedure repeats alternating phases (steps 1 and 2) until one team has no members? Actually "when the number of team members reaches 0, the game ends." Which team? Possibly Team UKU? Let's read: "At this time, when the number of team members reaches 0, the game ends." In step 1, after attacks, if number of team UKU reaches 0? But step 1 says: "Each person in Team UKU makes exactly 1 attacks on someone in Team UKU ... At this time, when the number of team members reaches 0, the game ends." Could be the team that was attacked? But they attacked within Team UKU, so only Team UKU members are being attacked. So after the attacks, some UKU members may die. If all UKU members die (i.e., number of UKU members becomes 0), game ends.
If game not over, then 1 is added to the common score and proceed to step 2.
Step 2: "Similarly, team members make 1 each and just 1 attacks on someone in Team UKU." This likely means: each person in Team Ushi makes exactly 1 attack on someone in Team UKU. Then the attacked person loses 1 health, and if health reaches 0 leaves the team. At this time, when the number of team UKU reaches 0, game ends. If not, add 1 to common score and go back to step 1.
Wait: step 2 says "someone in Team UKU". So it's consistent: both steps attack Team UKU members? Actually step 1 attacks within Team UKU (i.e., self-attack?), step 2 attacks from Team Ushi to Team UKU. That seems odd: only Team UKU is being attacked; Team Ushi never gets attacked? Then how can the game end? Only when Team UKU members become 0. But then Team Ushi never loses members. The game ends when Team UKU is wiped out. That seems unfair. And then what about M? The initial M matters because step 2 attacks by Team Ushi can cause more damage to Team UKU. So N and M both affect how fast UKU gets wiped out. The common score increments each time both steps (or maybe each step) are completed without ending.
But step 1: each UKU attacks someone in UKU. That means UKU members are damaging themselves, reducing their own team. Step 2: Ushi members attack UKU, further reducing UKU. So essentially both teams are cooperating to eliminate UKU as fast as possible, to minimize the score? The score increments each time we go through a full cycle (step1 then step2). So if we can eliminate UKU quickly, the score stays low.
But what about Team Ushi? They never lose members, so they will always be there. So the game always ends when UKU dies. The common score is the number of times we complete a "pair" of steps? But the problem says: "find the minimum possible common score at the end of the game." So we can choose attack targets optimally to minimize the score. Since the score increments each time we go to the next step without ending, we want to end the game as early as possible.
But wait: step 1: each UKU attacks someone in UKU. So each UKU deals 1 damage to some UKU member(s). Since each UKU attacks exactly one target (could be themselves? Possibly allowed, but it says "on someone", not necessarily distinct? Typically "attacks on someone" might allow self-attack? It's ambiguous. Usually in such problems, attacks can be allocated arbitrarily, possibly multiple attackers on the same target, etc. So we can decide the distribution of damage among UKU members.
Similarly, step 2: each Ushi attacks someone in UKU (again, can be any UKU member, possibly multiple attacks on same target).
Each person initially has health 2, so they can take up to 2 damage before dying. So they die if they receive 2 damage total (health decreases by 1 per attack). Since health starts at 2, after one attack they become health 1; after two attacks they die.
Goal: minimize number of times we increment the score. The score increments after step 1 if game not over (i.e., after UKU attacks, if UKU still has at least 1 member), we add 1 and go to step 2. After step 2, if game not over, we add 1 and go back to step 1. So each time we complete a step and the game continues, we add 1 to S. But careful: The problem says: "If the game is not over when everyone on Team UKU has just completed 1 attacks, 1 will be added to the common score and proceed to 2." So after step 1, we check if game over (UKU members = 0). If not, we increment S by 1 and then go to step 2. Similarly after step 2: "If the game isn't over when everyone on the team has just finished 1 attacks, 1 will be added to the common score and back to 1." So each time we go to the next step, we add 1. So S increments each half-round essentially. But S is the common score at the end. At the end, we don't add extra for the step that ends? Because when the game ends, we stop and we have S as it is. So if game ends during step 1 (i.e., after UKU attacks, UKU becomes 0), then we do not add the 1 after step 1 (since we only add if not over). Similarly, if ends during step 2, we stop, and we have added after step 1 (if that added happened) but not after step 2.
So S counts the number of times we have successfully completed a step without the game ending, i.e., the number of transitions. So if game ends after step 1 of round k, then S = (number of previous step completions). Actually we need to map.
Let's define stages: Stage 0: start, S=0.
Step 1 occurs: all UKU attack. After attacks, if UKU=0, game ends with S=0. If not, we increment S (S becomes 1) and go to step 2.
Step 2 occurs: all Ushi attack. After attacks, if UKU=0, game ends with S=1 (since we added after step 1). If not, we increment S (S becomes 2) and go to step 1.
And so on.
Thus S is essentially the number of completed steps that did not end the game, i.e., the count of steps executed where after executing we found game not over and added 1. Another way: S is the number of times we added 1, which equals the number of half-cycles (steps) that were fully completed without ending, but note that if the game ends after step 2, we have added after step 1 but not after step 2, so S equals the number of steps that were completed BEFORE the final step? Let's think: Suppose game ends after step 2 of some cycle. Then before step 2, S = some value, after step 2 we check if game over. If over, we stop and do not increment. So final S is that value. That value is the number of increments that occurred after previous steps. So essentially S = (number of steps executed before the final step) because each step (except possibly the final one) is followed by an increment if the game continues? Actually careful: After each step, we increment only if the game is not over. So the increment happens after the step and before the next step. So if the game ends during step 1, we never increment after step 1, so final S = number of increments that have occurred so far (which would be from previous steps). If the game ends during step 2, we have an increment after step 1 (since step 1 didn't end it), but not after step 2, so final S = (previous increments) + 1.
Thus S depends on at which step the elimination occurs.
Given we can control attacks, we want to minimize S.
Now the question: what's the minimum possible S given N and M?
We can think of this as a process where we have to allocate damage from UKU and Ushi attacks over time to kill all N UKU members (each requires 2 damage). The damage comes in two types: from UKU themselves (in step 1) and from Ushi (in step 2). The order is fixed: first UKU attacks, then if still alive, Ushi attacks, then UKU attacks again, etc. We can decide at each step how to distribute the attacks among the surviving UKU members.
We want to end the game as soon as possible, i.e., minimize the number of steps until all UKU are dead, and also perhaps choose to end at step 1 rather than step 2 to avoid an extra increment.
But note that after each step, we might or might not add to S. So the score S is essentially the number of steps that have been completed where the game did not end after that step. That is, if the game ends after a step, we do not add after that step. So S = (total number of steps executed) - 1? Not exactly.
Let's define t = total number of steps executed until termination. Steps are numbered: step 1, step 2, step 1, step 2,... The game terminates after some step t (t>=1). For each step i from 1 to t-1, after step i, since the game did not terminate (otherwise we would have stopped at i), we incremented S by 1. After step t, we do not increment. So S = (t-1). Because after step 1,..., step t-1 we incremented; after step t we stop. Check: If game ends at step 1 (t=1), then no increments, S=0 = t-1. If game ends at step 2 (t=2), then after step 1 we incremented, so S=1 = t-1. If game ends at step 3 (which would be a step 1 again), then increments after step1 and step2, S=2 = t-1. So indeed S = t-1.
Therefore minimizing S is equivalent to minimizing t, the total number of steps executed. Because S = t-1. Minimizing S means we want the smallest possible t.
But is that always? Wait, is there any scenario where we could have t=2 but S=0? That would contradict S = t-1. So no. So we need to minimize the number of steps required to eliminate all UKU members, given we can choose attack targets each step.
Thus problem: Given N UKU members (each health 2) and M Ushi members (which do not die and always attack), we have a sequence of steps: in odd-numbered steps (step type A), each UKU (surviving ones) attacks a UKU member (could be themselves? They must be alive to attack; presumably only surviving members can attack. In step 1, all N are alive initially, each makes one attack. After attacks, some may die. In subsequent step 1s (which occur after a step 2), only the remaining UKU members attack. In step type B (even steps), each Ushi (all M, constant) attacks a UKU member.
We need to allocate attacks to kill all UKU as quickly as possible, i.e., minimize the number of steps t until zero UKU remain.
But careful: In step 1, UKU members attack. But if some UKU members die during that step due to attacks, do they still get to attack? The description: "Each person in Team UKU makes exactly 1 attacks on someone in Team UKU for 1. The attacked person loses 1 in health and leaves the team when he or she reaches 0. At this time, when the number of team members reaches 0, the game ends." The attacks happen simultaneously? Typically in such problems, all attacks happen simultaneously, and then we check deaths after. So all UKU members that were alive at the start of the step make their attacks, even if the target dies during that step, but that's fine. So the attacking set is the set of UKU alive at the beginning of that step. Similarly for step 2, all M Ushi attack (they never die, so always M attacks each step 2).
Thus each step we have a certain number of attackers: in step type A: x = current number of UKU; in step type B: M (constant). Each attack reduces a target's health by 1; if health reaches 0, the member is removed (after step? but simultaneous, but health reduction is aggregated; if a member receives multiple attacks in one step, they could lose more than 1 health and potentially die if total damage >= remaining health. Since health starts at 2, a member could be killed in a single step if it receives 2 or more attacks in that step (since each attack is -1). But careful: they "lose 1 in health and leaves the team when he or she reaches 0." So if they receive 2 attacks in the same step, their health goes from 2 to 0, so they die. So it's possible to kill a member with multiple attacks in one step.
Thus we can concentrate damage to kill members quickly.
Goal: minimize number of steps t (i.e., rounds of attacks) to reduce UKU population from N to 0.
This looks like a resource allocation over time: we have a process with two types of attacks per step, alternating. We can decide at each step how to distribute the attacks among the current UKU members.
We need to find the minimal t such that we can schedule attacks to kill all N UKU within t steps, given that step 1 (or odd steps) provide attacks equal to the current number of UKU at start of that step, and step 2 (even steps) provide M attacks each time.
But note that the number of UKU attacks changes over time as they die. So we need to plan a strategy.
Because we can choose targets arbitrarily, we can aim to kill as many UKU as possible in early steps. However, killing UKU reduces the number of attackers in future step 1s. So there's a trade-off: if we kill many UKU early, we have fewer attacks later from UKU, but maybe we can rely on Ushi attacks. The objective is to minimize steps, not total number of attacks. So we want to finish as quickly as possible, maybe in as few steps as possible.
Observation: Since all UKU have health 2, each UKU member must receive a total of 2 damage before dying. The total damage needed to eliminate all UKU is 2N. This damage can be delivered by UKU attacks (from the UKU themselves) and by Ushi attacks (from Ushi). However, note that UKU attacks are only available as long as UKU members are alive to perform them. So the total damage that can be dealt by UKU across all steps is limited by the cumulative number of UKU attacks that can be made. But since UKU attacks are made by survivors each step, the total number of UKU attacks over the process equals sum over odd steps (step type A) of the number of UKU alive at start of that step. Similarly, Ushi attacks are always M per even step.
We need to find the minimal t such that we can allocate the damage to kill all UKU within t steps, respecting that in each step the number of attacks available is determined by the current state, and the attacks can be distributed arbitrarily.
This is reminiscent of a "parallel processing" or "scheduling" problem: We have two types of workers: type A (UKU) that can work only on some steps and whose number decreases as tasks are completed; type B (Ushi) works every even step, constant M. The tasks are to apply 2 units of work to each of N items. Each step, the available work from the workers can be allocated to items, and after each step, we check if any items have received 2 work, they are removed.
But we want to minimize the number of steps (time slots). This is like we want to finish as quickly as possible. Since we can allocate attacks arbitrarily, the optimal strategy likely involves focusing all attacks on as few UKU as possible each step to kill them quickly, because killing a UKU earlier reduces future UKU attacks? Actually, if we kill a UKU early, we lose its future attacks. That could be detrimental because UKU attacks are a resource. So maybe we want to preserve UKU members as long as possible to keep the attack count high, but we also need to eventually kill them all. However, we want to finish quickly, so we might want to use the attacks to kill some UKU quickly to reduce the total population, but then we lose their attacks for later steps. There is a balance.
Alternatively, maybe we can kill all UKU in a single step? Is that possible? In step 1, we have N attackers, each dealing 1 damage. To kill all N UKU, each must receive at least 2 damage (except maybe some could die from just 1? No, health 2 requires 2 attacks). With N attacks, total damage N, but total required is 2N, so impossible. So can't finish in step 1. In step 2 (if we go to step 2), we have M attacks from Ushi plus maybe the UKU attacks from step 1 already dealt some damage. But step 2 occurs after step 1, and after step 1 some UKU may be damaged (health 1). Then step 2 with M attacks could potentially finish them off if enough damage. Could we finish by step 2? That would mean after step 2, all UKU dead. That would be t=2, S=1. For that to be possible, we need that after step 1 and step 2, total damage applied is at least 2N, but note that step 1 attacks are from N UKU, step 2 attacks are M Ushi. So total damage available in first two steps = N + M. To kill N members (each need 2 damage), we need N + M >= 2N => M >= N. So if M >= N, it's possible in principle to allocate damage to kill all by step 2. But we also need to consider that UKU attackers are themselves being damaged and possibly die, which could affect the number of attacks from UKU in step 1? Step 1 attacks happen with all N alive, they all attack, then after step 1, some may die, but that doesn't affect step 1 damage because they already attacked. For step 2, only Ushi attack; UKU do not attack in step 2. So it's possible. However, we need to ensure that we can allocate the attacks to actually kill all N by step 2. With N+M damage, we need to assign 2 damage to each of N units. This is possible if N+M >= 2N and also we can distribute because each unit can take at most as many attacks as available. No other constraints: we can have multiple attacks on same unit. So if M >= N, then we can allocate: In step 1, each UKU attacks itself? But if each UKU attacks itself, each receives 1 damage, so after step 1 all have health 1. Then in step 2, we need to deliver N more damage to finish them off. With M attacks, if M >= N, we can assign each of the N surviving UKU one attack from Ushi, killing them. So yes, if M >= N, we can finish by step 2. That yields t=2, S=1.
But wait, the example: Input 20 10 gives output 0. Here N=20, M=10. Since M < N, maybe cannot finish by step 2. Let's check: total damage in steps 1 and 2 = 20+10=30, need 40. So not enough. So maybe finish by step 3? Step 3 is again a UKU attack (type A). So t=3 would be after step 3. S = t-1 = 2? But example output is 0 for 20 10. That suggests something else: for N=20, M=10, output 0, meaning S=0, so t=1? That would mean game ends after step 1. But is that possible? After step 1, can all UKU be dead? As argued, with N attacks, total damage N, cannot kill N units each requiring 2 damage. Unless some UKU start with health 2 and can die from 2 attacks, but we have only N attacks total, so maximum number of kills in step 1 is floor(N/2) if we concentrate attacks. Because to kill a unit you need 2 attacks on it. So maximum kills in step 1 = floor(N/2). For N=20, you could kill at most 10. Then 10 remain, not zero. So game cannot end after step 1. So output 0 can't mean t=1. So my interpretation of S might be wrong.
Let's re-examine the problem statement and examples.
Example 1: N=20, M=10 => output 0.
Example 2: N=10, M=20 => output 1.
Example 3: N=64783, M=68943 => output 4.
Example 4: N=10^18, M=10^18 => output 2.
Given these, if my earlier reasoning that M>=N gives S=1, then for N=10, M=20 (M>=N) we would get S=1, which matches example 2. For N=20, M=10 (M<N) we got S? According to earlier, maybe S=2 or more? But output is 0. So maybe my interpretation of S as t-1 is wrong; maybe S is something else. Or maybe the game ends when the number of team UKU reaches 0? But perhaps the team that must reach 0 is Team Ushi? Let's re-read original text.
The problem statement (as given) is a bit messy:
"There are 2 teams, Team UKU and Team Ushi. Initially, Team UKU has N people and Team Uku has M people. Team UKU and Team Ushi decided to play a game called 'U & U'. 'U & U' has a common score for 2 teams, and the goal is to work together to minimize the common score. In 'U & U', everyone's physical strength is 2 at first, and the common score is 0, and the procedure is as follows.
- Each person in Team UKU makes exactly 1 attacks on someone in Team UKU for 1. The attacked person loses 1 in health and leaves the team when he or she reaches 0. At this time, when the number of team members reaches 0, the game ends. If the game is not over when everyone on Team UKU has just completed 1 attacks, will be added to the common score and proceed to .
Interpretation might be that in step 1, UKU attacks UKU. In step 2, Ushi attacks UKU. But the phrase "when the number of team members reaches 0" in step 1 could refer to the team that is being attacked? Or could be ambiguous.
Maybe step 2 says: "team members make 1 each and just 1 attacks on someone in Team UKU." Actually it might be: "Similarly, team members make 1 each and just 1 attacks on someone in Team Ushi." The original problem might be symmetric: both teams attack each other? Let's think: The game is called "U & U" maybe referring to both teams starting with U. Possibly both teams attack each other alternately. Let's try to guess: Usually in such problems, there are two teams, and they alternately attack the other team. But the text says: "Each person in Team UKU makes exactly 1 attacks on someone in Team UKU for 1." That seems like self-attack. Could it be a mis-translation and it should be "on someone in Team Ushi"? Let's look at step 2: "Similarly, team members make 1 each and just 1 attacks on someone in Team UKU." It mentions "team members" maybe referring to Team Ushi? It says "on someone in Team UKU". So step 2 is Ushi attacking UKU. So it's asymmetric.
But then why would the common score be minimized by cooperating? They both attack UKU, so they are cooperating to kill UKU quickly. That seems plausible.
But the examples: 20 10 output 0. If they are cooperating to kill UKU, with N=20, M=10, they cannot kill UKU in one step, but maybe they can kill UKU in such a way that the game ends after step 2? That would give S=1 (since after step 1 not ended, add 1, then step 2 ends). But output is 0. So maybe the game ends when the number of team members reaches 0, but which team? Perhaps in step 1, when the number of team members (UKU) reaches 0, game ends; in step 2, when the number of team UKU reaches 0, game ends. So it's always UKU. So why output 0 for 20,10? Could it be that they can arrange attacks such that after step 1, the number of team UKU reaches 0? But as argued, can't kill 20 with 20 attacks. Unless some UKU may die from receiving 2 attacks from both UKU and Ushi in the same step? But step 1 only has UKU attacks; no Ushi attacks yet. So impossible.
Maybe the game ends when the team that is attacking has 0 members? That is, in step 1, if after the attacks, the number of team UKU (the attackers) becomes 0, game ends. But they are attacking their own team, so some may die. Could it be that after step 1, if all UKU die, game ends with S=0. With 20 UKU, can we kill all 20 in step 1? Each UKU attacks exactly one target. They can target each other. If we arrange for each UKU to be attacked by at least 2 attacks? But there are only 20 attacks total. To kill all 20, each needs at least 2 damage, so need 40 attacks. Not possible. So not.
Maybe the health is not 2 but starts at 2, but maybe an attack reduces health by 1, and if health reaches 0, they leave. But if a person is attacked multiple times in the same step, they lose multiple health, so they could die from 2 attacks. Still total attacks needed is 40.
Thus something else.
Let's re-check example: Input 20 10 output 0. Could it be that the roles are reversed? Maybe the team that is attacked alternates? Step 1: UKU attacks Ushi? Step 2: Ushi attacks UKU? And the game ends when either team has 0 members? And the common score increments after each full round? That would be more symmetric. Let's try: Initially N UKU, M Ushi. Step 1: each UKU attacks someone in Ushi. After attacks, if Ushi members become 0, game ends. If not, add 1 to score and go to step 2. Step 2: each Ushi attacks someone in UKU. After attacks, if UKU members become 0, game ends. If not, add 1 to score and go back to step 1. This is a typical turn-based elimination game. And they want to minimize the common score, so they want to end the game as soon as possible, perhaps by coordinating attacks to eliminate one team quickly. In this scenario, both teams can attack each other, and the game ends when one team is wiped out. The score increments after each step that does not end the game. That is similar to my earlier S = t-1 where t is number of steps until termination. But now termination can happen when either team becomes 0. So we have two teams each with health 2 per person, and they alternate attacks. We want to minimize steps to eliminate one team (the one that gets eliminated first). Since the game ends when either team reaches 0, we can aim to eliminate the smaller team quickly, perhaps in one step if possible.
But in example 1: N=20, M=10. Which team is smaller? M=10. If UKU attacks Ushi in step 1, they have 20 attacks, each Ushi has health 2, so to kill all 10 Ushi, need 20 damage, exactly. Each attack does 1 damage. So with 20 attacks, they could kill all 10 Ushi (each needs 2 damage, so need 20 attacks). But is it possible to distribute 20 attacks among 10 Ushi such that each gets exactly 2? Yes, if each Ushi is attacked exactly twice. So after step 1, Ushi team becomes 0, game ends immediately. Then score remains 0 because game ends before adding any increment. So output 0. That matches! Example 2: N=10, M=20. Now UKU has 10, Ushi has 20. Step 1: UKU attacks Ushi. With 10 attacks, can they kill all 20 Ushi? No, they need 40 damage. They can kill at most floor(10/2)=5 Ushi (since each requires 2 attacks). So Ushi will not be eliminated in step 1. After step 1, Ushi still has some members, game not over, so add 1 to score (S=1) and go to step 2. Step 2: Ushi attacks UKU. Now Ushi has 20 members (still 20? Actually after step 1, some Ushi might have died, but if we didn't kill all, then number is >0. But we want to minimize score, so we want to end as soon as possible. Perhaps we can arrange that after step 2, UKU becomes 0. UKU has 10 members, each health 2. Ushi has 20 attacks. To kill all 10 UKU, need 20 damage, exactly. So if we arrange that in step 2, Ushi kills all UKU, then game ends after step 2. At that point, after step 1 we added 1, after step 2 we do not add because game ends. So final S=1. That matches output 1. Example 4: N=10^18, M=10^18. Both equal. Step 1: UKU attacks Ushi with 10^18 attacks. To eliminate Ushi, need 210^18 damage, only 10^18 available, so cannot eliminate. So after step 1, add 1. Step 2: Ushi attacks UKU with 10^18 attacks, need 210^18 damage to eliminate UKU, only 10^18, so cannot eliminate. So after step 2, add 1 (S becomes 2) and go back to step 1. Now we have both teams still alive, but their healths might have been reduced. Need to see if we can finish by step 3 or 4, etc. But output is 2, so the minimal score might be 2? That would correspond to game ending after step 3? Because if ends after step 3, S would be 2? Actually if ends after step 3, we would have added after step 1 and step 2, so S=2, and then step 3 ends, no further increment, S=2. So output 2 could mean game ends at step 3. Or if ends after step 4, S would be 3? Actually after step 3 (if not ended) we would add to S=3, then step 4 maybe ends. So S=2 corresponds to t=3 steps. So for N=M=10^18, minimal steps t=3? Let's see if possible: after step 1, Ushi members have some damage (maybe all have 1 health left if we distribute evenly). After step 2, UKU members also have 1 health left. Then step 1 again: UKU attacks Ushi again, now Ushi have health 1 each, so 10^18 attacks can kill all Ushi (since each needs 1 damage). So after step 3, Ushi eliminated, game ends. That yields t=3, S=2. That matches output 2. So this interpretation fits all examples.
Thus correct interpretation: There are two teams, UKU (size N) and Ushi (size M). They alternate attacks: first all UKU members each attack someone in Ushi (each deals 1 damage). Then if Ushi is eliminated, game ends. Otherwise, score increments by 1. Then all Ushi members each attack someone in UKU. If UKU is eliminated, game ends. Otherwise, score increments by 1, and repeat.
We need to find the minimal possible common score at the end, assuming optimal choice of attack targets (i.e., we can decide how to allocate attacks each step to try to end the game as soon as possible). Since both teams cooperate to minimize the score, they will coordinate attacks to eliminate a team as quickly as possible. So it's a cooperative game where both teams choose targets to minimize steps.
Thus problem reduces to: Given initial numbers N and M (both up to 1e18), both teams have health 2 per member. They take turns attacking: Team A (UKU) attacks Team B (Ushi) in odd steps, Team B attacks Team A in even steps. In each attack turn, every member of the attacking team makes exactly one attack on any member(s) of the opposing team (can be multiple attacks on same target). After the attacks, any member of the attacked team whose total damage taken so far reaches 2 dies (i.e., health reduces to 0). The game ends when one team has no members left. The common score is incremented by 1 after each attack turn that does not end the game (i.e., after a turn where the attacked team is not completely eliminated). We want to minimize the final score (which equals the number of attack turns that have been completed without ending the game). Equivalent to minimizing the number of turns until elimination, minus 1.
So find the minimal possible number of turns t (starting from turn 1) such that elimination can be achieved, with optimal allocation of attacks each turn. The final score S = t-1 (since after each non-terminal turn we increment). So we need to compute minimal t-1.
Thus we need to compute the minimal number of turns (steps) needed to eliminate one team, given we can control attack distribution.
This is similar to a process where we have two piles of "units" each with 2 hit points. In each turn, the attacker's side deals a number of "damage points" equal to its current number of members, distributed among the opposing side's members. The damage persists across turns (health doesn't reset). The goal is to eliminate all members of one side as quickly as possible, with both sides cooperating (i.e., we can choose distribution to achieve fastest elimination).
We can think in terms of total damage needed to eliminate a team: For team of size X, total health = 2X. But because the attacking team's size may change over time (as its own members may be killed by the other team's attacks), the number of damage points it can deliver in future turns depends on how many survive.
So it's a dynamic resource allocation problem. Since both teams cooperate, they can coordinate to focus attacks to eliminate one team as soon as possible. Usually, the optimal strategy is to focus all attacks on as few opponents as possible to kill them quickly, reducing the opponent's attacking force (if we are trying to eliminate the opponent). But there is a trade-off: killing opponent members reduces their future attack capacity, which can help protect your own team. However, since we want to minimize total turns, we likely want to eliminate one team entirely as quickly as possible. Which team should be eliminated? Possibly the smaller one, but not necessarily; it might be faster to eliminate the larger one if the attacking force is large enough early. But we can choose to eliminate either team; we want the minimal number of turns to eliminate any team.
Since both teams cooperate, they can decide which team to eliminate, and they can coordinate attacks to achieve that in minimal turns. So we need to compute the minimum over two possibilities: the minimum turns to eliminate UKU (by Ushi attacks) and to eliminate Ushi (by UKU attacks). Actually the game ends when either team is eliminated; we can aim to eliminate whichever can be eliminated sooner.
But note that the attack order alternates: Turn 1: UKU attacks Ushi. If we eliminate Ushi on turn 1, t=1, S=0. That requires N >= 2M? Wait: To eliminate Ushi on turn 1, we need to deal at least 2M damage (since each Ushi has health 2) with N attacks. So need N >= 2M. But example 1: N=20, M=10 gives N=20, 2M=20, so N=2M works. That yields elimination on turn 1. Output 0 matches. So condition for turn 1 elimination of Ushi: N >= 2M. Similarly, could we eliminate UKU on turn 1? Turn 1 is UKU attacking Ushi, so UKU cannot be eliminated on turn 1 because they are not attacked. So turn 1 only possible elimination is Ushi. For turn 2: elimination could be of UKU (attacked by Ushi) or possibly Ushi if it survived turn 1 and then UKU attacks again? But turn 2 is Ushi attacks UKU, so elimination on turn 2 could be UKU if Ushi deals enough damage. Also, could Ushi be eliminated on turn 2? No, because turn 2 is Ushi attacking, not being attacked. So the earliest turn when UKU can be eliminated is turn 2. For elimination on turn 2, we need that after turn 1, UKU still has all members (since they are not attacked in turn 1), and then Ushi attacks with M attacks. To eliminate UKU in turn 2, need M >= 2N? But careful: In turn 2, UKU members are at full health (2 each) if they haven't been attacked. So to kill all N UKU in one turn, need M attacks to deliver 2N damage, i.e., M >= 2N. However, note that UKU may have been reduced in health if they were attacked earlier? They are not attacked before turn 2. So condition for turn 2 elimination of UKU: M >= 2N. Example 2: N=10, M=20 => M=20, 2N=20, so M=2N, can eliminate on turn 2. That yields S=1, matches example 2.
If neither N >= 2M nor M >= 2N, then turn 1 and turn 2 cannot eliminate any team. Then we go to turn 3. Turn 3 is again UKU attacks Ushi. By now, both teams may have taken damage from previous turns, so we need to consider the state.
In general, we need to find the minimal t such that there exists a strategy to eliminate one team by the end of that turn.
Since both teams cooperate, we can assume they will use optimal targeting to minimize t.
This becomes a problem of determining the minimal number of turns to reduce one team's total health to zero given alternating attacks and health degradation.
We can view each team as having total health points: H_U = 2N (UKU), H_S = 2M (Ushi). Each turn, the attacking team deals a number of damage points equal to its current number of surviving members (which may have been reduced over time). The damage can be allocated arbitrarily among the opposing team's members, but the effect on the opposing team's total health is straightforward: each damage point reduces total health by 1, until members die when their individual health reaches 0. However, because each member can take at most 2 damage before dying, the distribution matters: to kill a member, you need to allocate exactly 2 damage to it (or more than 2, but extra damage is wasted if member already dead). But since we want to minimize turns, we would never waste damage; we can allocate damage to exactly finish off members. So we can assume optimal allocation: damage is used as efficiently as possible, i.e., we always try to apply damage to members with remaining health >0, and we avoid overkill.
Thus, from a total health perspective, the process is equivalent to: we have two pools of health: each team's total remaining health (sum of health of all members). Initially, UKU total health = 2N, Ushi total health = 2M. When a team attacks, it deals damage equal to its current number of members (not its total health). But the number of members is not simply total health/2 because members can be partially damaged (health 1). The number of members is the count of alive individuals, which is between ceil(total health/2) and floor(total health)? Actually each alive member has health 1 or 2. Total health = sum of healths. Number of members = number of individuals with health>0. If total health is H, the minimum number of members possible for that total health is ceil(H/2) (if as many as possible have health 2, but that's maximum members? Wait, if total health is H, the maximum number of members you can have is H (if each has health 1) and the minimum number is ceil(H/2) (if you try to concentrate health into as few members as possible, each up to 2). Actually if total health is H, you can have any number of members between ceil(H/2) and H, as long as each member has health 1 or 2. Because you can split health into many members with health 1, giving more members; or combine into members with health 2 (and possibly one with health 1 if H odd), giving fewer members. So the number of members can vary based on how damage has been distributed. This is important because the attacking force is the number of members, not total health. So we can control the number of members by how we distribute damage: we can choose to kill some members completely (reducing member count) or leave many wounded (health 1) to keep member count high. Which is beneficial? For the attacking team, we want to maximize its future attack power, so we want to keep as many members alive as possible (i.e., avoid losing members). But when we are defending (i.e., being attacked), we might want to minimize the opponent's future attack power by killing their members quickly. However, since both cooperate, we can decide distribution on both sides to expedite elimination.
Thus, it's a game where we can choose how to distribute damage each turn to shape the sizes of both teams for future turns, with the goal of eliminating one team as soon as possible.
We need to find the minimal number of turns t (or S = t-1). Because N and M up to 1e18, there must be a formula or simple condition.
Let's denote after each turn, we have state (a, b) where a = number of UKU members, b = number of Ushi members, and also their health distributions. But we can also think in terms of total health and member counts, but health distribution matters for member count.
Observation: Because each member has health 2, and damage is integer, we can think of each member as having a state: either healthy (2 health) or wounded (1 health). When a member receives one damage, it becomes wounded if it was healthy; if it was wounded, it dies and is removed.
Thus we can model the process more concretely. We'll denote:
- UKU: N_h healthy, N_w wounded. Total members = N_h + N_w. Total health = 2*N_h + N_w.
- Ushi: M_h healthy, M_w wounded. Total members = M_h + M_w. Total health = 2*M_h + M_w.
Initially: N_h = N, N_w = 0; M_h = M, M_w = 0.
Turn 1 (UKU attacks Ushi): All UKU members (N) attack. They can target Ushi members arbitrarily. Each attack reduces a target's health by 1. So we can decide how many Ushi healthy become wounded, and how many wounded become dead. Let's denote that we apply D = N damage to Ushi.
We want to minimize total turns until either UKU or Ushi is eliminated. Since we control targeting, we can decide to prioritize killing Ushi members (i.e., convert wounded to dead) or wounding healthy ones. The effect on Ushi's member count: each attack on a healthy reduces it to wounded (health 1) and member count unchanged (still alive). Each attack on a wounded kills it, reducing member count by 1. Also, we could attack a healthy twice to kill it in one turn, but attacks are simultaneous? Actually each UKU member makes exactly one attack, so total N attacks are applied at once. We can allocate multiple attacks to the same Ushi member, as long as total attacks on that member can be up to N. So we can kill some Ushi members outright by assigning 2 attacks to them, and wound others with 1 attack, etc. The distribution we choose affects the resulting numbers of healthy/wounded Ushi and thus the number of Ushi members that survive for future turns (which will attack later).
Similarly, on turn 2, Ushi attacks UKU with its current member count (which after turn 1 is M' = number of surviving Ushi). We can again allocate damage to UKU strategically.
Our goal: find minimal t such that we can schedule these attacks to eliminate one side by turn t.
We can think of it as a kind of "battles" where each turn the attacker deals damage equal to its current size to the defender, and we can choose how many defenders to kill vs wound.
Since we want to end fast, we likely want to eliminate the side that is easier to eliminate given the current attacker sizes. But we can also try to weaken the opponent to reduce their future attacks if we are aiming to eliminate the other side.
But note: The game ends as soon as either team is eliminated. So we might aim to eliminate the team that will be eliminated sooner. Usually that will be the team with smaller total health relative to the cumulative attacks it will receive. But due to alternating attacks and changing sizes, we need to compute optimal.
Given the complexity, perhaps there is a known solution: The minimal score is something like ceil(log_{something}(...)? But examples: 64783, 68943 => output 4. Let's test with simple conditions.
Let's try to simulate small values to see pattern. Since N and M up to 1e18, there must be a closed-form formula based on comparing N and M, maybe involving binary search or something like the number of steps until one side can be eliminated.
We can think in terms of the following: In each full cycle (turn 1 then turn 2), both teams get to attack once. Over multiple cycles, the attacks reduce both teams' total health and member counts.
But we can perhaps derive the minimal number of turns as the smallest integer t such that either cumulative attacks from one side can eliminate the other, considering that the attacker's size may decrease over time.
This resembles a "mutual elimination" process where each side's size evolves like a recurrence.
Let’s denote after k turns, we have some state. Turns are odd: UKU attacks Ushi; even: Ushi attacks UKU.
We want to find smallest t where either UKU size becomes 0 after an attack (i.e., after an even turn) or Ushi size becomes 0 after an attack (after an odd turn).
Because both cooperate, they will try to focus attacks to kill as many opponents as possible each turn, i.e., to minimize the opponent's member count for future. So the optimal strategy to eliminate the opponent as fast as possible is to use all attacks to kill as many opponents as possible, rather than wounding. Because wounding does not reduce the opponent's member count, which matters for their attack power in the next turn if they are the attacker. However, if we are aiming to eliminate the opponent in this turn, we might need to wound some if we don't have enough damage to kill all. But for minimizing turns, we likely want to maximize the reduction in the opponent's member count each turn, to reduce their future attack capacity if they are the next attacker. So the optimal strategy for the attacker is to use its attacks to kill as many opponents as possible. Since each kill requires 2 attacks, with X attacks we can kill floor(X/2) opponents, and if there is an odd remainder, we can wound one additional opponent (leaving it at health 1). This maximizes reduction in the opponent's member count: we reduce member count by floor(X/2) (since kills reduce count) and possibly also convert a healthy to wounded, which doesn't reduce count but might help later.
But is it always optimal to maximize kills? Possibly yes, because reducing the opponent's member count weakens their future attacks, which could help protect your own team. Since both teams cooperate to end quickly, they would both adopt this "max kill" strategy. However, we also might consider that sometimes wounding can be more beneficial if we plan to finish off the opponent in the next turn? But if we wound, the opponent's member count stays high, meaning they will have more attacks on their turn, which could kill more of our team, potentially delaying elimination of the opponent. Since we want to minimize turns, we want to eliminate one team asap, so we want to reduce the other team's ability to attack us only if that helps us survive long enough to eliminate them? But if we are trying to eliminate the opponent, we want to deal as much damage as possible to them, and killing is more effective than wounding for reducing their future attacks. So maximizing kills seems optimal.
But there might be cases where it's better to wound some to preserve some of our own members? Let's think: Suppose we are UKU and we want to eliminate Ushi. We could use all our attacks to kill as many Ushi as possible. That reduces M for the next turn, so when Ushi attacks back, they have fewer members, thus fewer attacks on us, preserving our size N for our next attack. That seems beneficial for us to continue attacking. Conversely, if we instead wound many, Ushi would retain more members, and they would attack us more strongly, potentially reducing our N, which reduces our future attack power. So to maximize our chance to eliminate Ushi quickly, we should kill as many as possible.
Similarly, when Ushi attacks UKU, they would also kill as many UKU as possible to reduce UKU's size for future UKU attacks, if they want to eliminate UKU. But since both cooperate to minimize score, they can coordinate to decide which team to eliminate. The optimal overall strategy will be to aim for elimination of one team, and both sides will act in a way that maximizes progress toward that goal. That likely means each attacker uses its attacks to kill as many opponents as possible (i.e., floor(attacks/2) kills, and possibly one wound if odd).
Thus we can model the process deterministically if both use optimal kill-maximizing strategy. However, we need to consider that we might not always want to kill max because maybe we want to eliminate the other team instead. But we can choose whichever elimination path yields fewer turns. So we need to compute the minimal turns under the assumption that each side uses the optimal strategy to achieve the earliest possible elimination, which may involve cooperating: e.g., if we aim to eliminate Ushi, then UKU will try to kill as many Ushi as possible, and Ushi, even though it's cooperating, might also try to help eliminate itself? But Ushi is also cooperating to minimize score, so it will also act in a way that leads to fastest overall elimination, even if that means sacrificing itself. That means Ushi, when it attacks, might not necessarily try to kill as many UKU as possible if that would delay Ushi's own elimination? Wait, if the goal is to minimize the score, and both teams are cooperating, then they can coordinate their actions. So Ushi can choose to attack in a way that helps to eliminate itself faster? But Ushi attacks UKU, which actually harms UKU, making UKU weaker and potentially making it harder for UKU to eliminate Ushi. If we want to eliminate Ushi quickly, we want UKU to stay strong, so Ushi might want to not kill many UKU, maybe even waste attacks (e.g., attack already dead members? but there are none) or maybe attack in a way that does not reduce UKU's member count (i.e., wound only, not kill). Because if Ushi kills UKU members, that reduces UKU's attack power, which slows down Ushi's own elimination. Since both are cooperating, Ushi could choose to wound UKU members instead of killing them, to preserve UKU's numbers. Is that allowed? Yes, they can choose any target distribution. So they can choose to only wound (i.e., give 1 damage to as many UKU as possible) to avoid killing any, thereby keeping UKU member count high. But note: each attack reduces health by 1. If they attack a healthy UKU, it becomes wounded; if they attack a wounded UKU, it dies. So to avoid killing, they should only attack healthy UKU members, and ensure they never attack a wounded one. But they might have more attacks than the number of healthy UKU; then some attacks would have to go to already wounded, causing kills. But they could also attack the same healthy multiple times? If they attack a healthy twice, that would kill it (since 2 damage kills). So to avoid kills, they should allocate at most one attack per healthy UKU. If M (number of Ushi) is larger than the number of healthy UKU, then some attacks will have to go to already wounded or to healthy again, causing kills. But they might be able to wound all healthy, and then the remaining attacks could be allocated to healthy again? That would cause kills. Alternatively, they could "waste" attacks by attacking already dead? There are no dead members. So they must attack living members. So if M > number of healthy UKU, then after wounding all healthy, the remaining attacks would have to be allocated to either wounded (killing them) or to healthy again (killing them). So kills are unavoidable if M exceeds the number of healthy UKU.
Thus, the cooperating Ushi can minimize its kill count by first wounding all healthy UKU (one attack each), and if there are still attacks left, they would have to kill some members (by attacking wounded or healthy again). So the optimal cooperation to eliminate Ushi quickly is for Ushi to minimize the reduction in UKU's member count. That means Ushi should aim to wound as many as possible without killing, and only kill when forced.
Similarly, if the plan is to eliminate UKU quickly, then UKU (when attacking) should minimize kills on Ushi, i.e., wound rather than kill, to preserve Ushi's numbers for its attacks? Wait, if we aim to eliminate UKU, we want Ushi to be strong to kill UKU. So UKU (the attacker) should avoid killing Ushi members, to keep Ushi's numbers high, so that Ushi can deliver more damage to UKU in its turn. But UKU is attacking Ushi; if UKU kills Ushi members, that weakens Ushi, making it harder for Ushi to eliminate UKU. So UKU should wound rather than kill if possible. So indeed, the cooperating teams can choose to wound rather than kill to preserve the other team's strength, depending on which elimination they are targeting.
Thus, the optimal strategy to achieve fastest elimination of a particular team is for the attacker that is opposite to that team to minimize kills on the team we want to keep strong, while the attacker that is the same side as the eliminator should maximize kills on the opponent. However, both teams are cooperating, so they can coordinate such choices.
But wait: The ultimate goal is to minimize the common score, which is equivalent to minimizing the number of turns until either team is eliminated. So we can decide which team to eliminate, and then both teams will act in a way that leads to that elimination in as few turns as possible.
Thus, we need to compute, for each possible elimination target (Ushi or UKU), the minimal number of turns required under optimal cooperation, and then take the minimum of the two.
Given the symmetry, it's sufficient to compute the minimal turns to eliminate Ushi (i.e., UKU is the attacker in odd turns, Ushi in even turns) with optimal cooperation where UKU tries to kill Ushi, and Ushi tries to avoid killing UKU (to keep UKU strong). But also, Ushi may need to kill some UKU if forced.
Similarly, compute minimal turns to eliminate UKU (Ushi tries to kill UKU, UKU tries