We need to solve: There are n aliens with distinct ranks from 1 to n. Initially all are in section 3. They need to all move to section 1. Sections are arranged linearly: 1 - 2 - 3. Sections are adjacent: 1-2, 2-3. Moves allowed: an alien can move from a section to an adjacent section only if it is senior (higher rank) to all aliens currently in both the source and destination sections. Only one move per minute. Find minimal total time (minutes) to transfer everyone from section 3 to section 1, modulo m (where m up to 1e9 but can be less than answer). Input: n and m (1 <= n,m <= 1e9). Output answer modulo m.
We need to compute minimal number of moves needed under constraints. Since only one move per minute, minimal time equals minimal number of moves required to achieve the goal (since we can schedule moves sequentially). So we need to compute minimal number of moves required.
This is a puzzle reminiscent of the Tower of Hanoi, but with three pegs in a line and a ranking constraint. Actually it's like the "Reve's puzzle"? Not exactly. The constraint is that when moving an alien from section a to adjacent section b, the alien must be senior to all aliens currently in a and b. That means the moving alien must be the highest-ranked among all aliens present in both sections at that moment. So essentially, you can only move the highest-ranked alien among those present in the two adjacent sections.
We start with all n aliens in section 3. Goal: all in section 1. This is like moving a stack of disks with the restriction that a disk can move only if it's the largest among the two pegs? Actually in Tower of Hanoi, only the top disk can be moved. Here the condition is similar but dependent on rank: you can move an alien if it's the highest-ranked among all aliens in the two sections involved. Since all aliens are distinct, this essentially forces that you can only move the largest alien present in the two sections.
But note that the two sections are adjacent. So if we consider sections 2 and 3, we can only move the highest-ranked alien among those currently in 2 and 3, and that alien must be moving from one to the other. So it's analogous to the "Tower of Hanoi" with three pegs in a line, where only the top disk on each peg can be moved, but also there is a restriction about size? Actually typical Hanoi: you can move only the top disk, and you cannot place a larger disk on a smaller one. Here the aliens have ranks; moving a senior alien (higher rank) to a section containing a junior alien (lower rank) seems allowed because the condition only applies when moving: the moving alien must be senior to all in both sections. That means after moving, the destination section might contain aliens of lower rank; but there's no restriction that a senior alien cannot be on top of a junior? But the sections are not stacks; they are sets. There is no ordering within a section; they are just present. So it's like pegs where all disks are just placed in a pile with no ordering. But the move condition that the moving alien must be senior to all in both sections effectively enforces that the senior alien can be moved only when the two sections contain only junior aliens. That means you cannot have a senior alien in a section with a junior alien? Actually if a senior alien is in a section, then any alien that is junior can also be there, but then if you try to move a junior alien from that section, it is not senior to the senior alien present, so cannot move. So effectively, the senior alien blocks the movement of juniors. So the only alien that can move between two sections is the highest-ranked alien among those two sections. That suggests that the process is deterministic: the only possible moves involve moving the maximum ranked alien among the two adjacent sections.
But we need to transfer all from 3 to 1. The minimal number of moves likely is something like (something similar to Hanoi, maybe 2^{n+1} - 3?). Let's try small n manually and see pattern.
n=1: All in 3 -> need to move to 1. Moves: 3->2, then 2->1 = 2 moves. So answer=2.
n=2: aliens ranks 1 (junior) and 2 (senior). Start: both in 3. Goal: both in 1. Let's find minimal moves.
We have sections: 1,2,3. Initially: 3: {1,2} (I'll list ranks present). 2: empty, 1: empty.
Rules: Only one alien can move at a time. A move from a to b (adjacent) allowed if moving alien is senior (higher rank) than all aliens currently in both a and b.
We need to end with: 1: {1,2}, others empty.
Let's try to find sequence. We need to get senior alien (2) to move at some point, but maybe juniors need to clear the way.
Think: To move alien 2 from 3 to 2, we need that 2 is senior to all in 3 and 2. Initially, 3 contains {1,2}, so 2 is not senior to all in 3 because there is itself? Actually senior means rank higher than others. Is an alien senior to itself? The definition: "Alien A is senior in rank to alien B, if the number indicating rank A, is more than the corresponding number for B." So it's only defined between distinct aliens. Usually in such puzzles, "senior to all aliens" means to all other aliens. Does it include itself? Typically you compare to all others. So we should interpret that the moving alien must be senior (i.e., have higher rank) than all other aliens in the two sections. Since aliens are distinct, it's okay. So alien with rank 2 is senior to alien with rank 1 (since 2>1). So if we want to move alien 2 from 3 to 2, we need to check: In section 3, there is also alien 1. Alien 2 is senior to alien 1. In section 2, it's empty. So condition holds: alien 2 is senior to all other aliens in sections 3 and 2. So we can move alien 2 from 3 to 2. However, after moving, section 3 will have alien 1, section 2 has alien 2. Now we have both aliens separate.
Now we need both to end in section 1. But now alien 2 is in 2, alien 1 is in 3. Goal: both in 1.
Now can we move alien 2 from 2 to 1? Check: Move from 2 to 1: Alien 2 must be senior to all in 2 and 1. Section 2: only alien 2 (no other). Section 1: empty. So condition holds. So we could move alien 2 from 2 to 1. That would put alien 2 in 1. Then we have alien 1 in 3. Now we need to get alien 1 to 1, but alien 2 is now in 1. To move alien 1 from 3 to 2, we need alien 1 to be senior to all in 3 and 2. Section 3: alien 1 only, okay. Section 2: empty, okay. So alien 1 can move from 3 to 2. Then from 2 to 1? But to move alien 1 from 2 to 1, condition: alien 1 must be senior to all in 2 and 1. Section 2: alien 1 only, okay. Section 1: alien 2 is there. Alien 1 is junior to alien 2 (1<2). So condition fails: alien 1 is not senior to alien 2. So cannot move directly. So we need to move alien 2 away from 1 to allow alien 1 to come? But alien 2 is already at the target. Hmm.
Alternate sequence: Maybe need to move 1 first? Let's try to move alien 1 first from 3 to 2? Condition: Alien 1 must be senior to all in 3 and 2. In section 3, there is also alien 2 (rank 2). Alien 1 is not senior to alien 2. So cannot move alien 1 while alien 2 is in 3. So indeed only the senior alien can move initially.
After moving alien 2 to 2, we have 3: {1}, 2:{2}. Could we move alien 1 from 3 to 2? Check: Move 1 from 3 to 2 requires that 1 is senior to all in 3 and 2. Section 3 has {1} okay. Section 2 has {2}. 1 is not senior to 2, so cannot move. Could we move alien 2 from 2 to 3? Possibly but that would be backward. Check: Move 2 from 2 to 3: Must be senior to all in 2 and 3. Section 2: {2}, Section 3: {1}. 2 is senior to 1, so allowed. That would bring both back to 3. Not helpful.
So we have a deadlock? We need to get both to 1. We have after moving 2 to 2, we can move 2 to 1. But then 1 stuck in 3. We can't move 1 to 2 because 2 is not in 2; after 2 moved to 1, 2 is in 1. Then moving 1 from 3 to 2 is allowed? After 2 in 1, sections: 3: {1}, 2: empty, 1:{2}. Move 1 from 3 to 2: Condition: 1 must be senior to all in 3 and 2. 3: {1}, 2: empty -> ok. So that move is allowed. Then we have 2 in 1, 1 in 2. Next, to move 1 from 2 to 1: need 1 senior to all in 2 and 1. Section 2: {1}, Section 1: {2}, but 1 is junior to 2, so not allowed. So indeed we can't move 1 into 1 while 2 is there. So we need to temporarily move 2 out of 1. Perhaps move 2 from 1 back to 2, then move 1 to 1, then move 2 back to 1? But moving 2 from 1 to 2: Check condition: 2 must be senior to all in 1 and 2. After state: 1:{2}, 2:{1}. Actually wait: after we have 2 in 1, 1 in 2. Move 2 from 1 to 2: Source 1, dest 2. Need 2 senior to all in 1 and 2. Section 1: only 2 (no others). Section 2: contains 1. 2 is senior to 1, so condition holds. So we can move 2 from 1 to 2. Then we have 1:{}, 2:{2,1} (both aliens now in 2). Then we want to get both to 1. Now both are in 2. To get to 1, we need to move them to 1 via section 2->1 moves. But now both in 2, we need to move one at a time. However, moving an alien from 2 to 1 requires that alien is senior to all in 2 and 1. If we try to move alien 1 from 2 to 1, we need 1 senior to all in 2 and 1. But in 2 there is alien 2, and 1 is not senior to 2, so cannot. So only senior alien 2 can move from 2 to 1. So move 2 from 2 to 1: condition holds because 2 senior to 1. Then we have: 1:{2}, 2:{1}. That's back to state earlier (2 in 1, 1 in 2). So that loop doesn't help.
Thus we need a more clever sequence. Possibly we need to use the middle section as a temporary holding area and move the junior alien first? But initial move only senior. The puzzle reminiscent of Tower of Hanoi with three pegs but with the condition that larger disk (higher rank) can always be moved on top of smaller? Actually in Tower of Hanoi, you can't place larger on smaller. Here condition is opposite: moving larger can go anywhere? The condition for moving is that moving alien is larger than all in source and destination. That means if you want to move a junior alien, the source and destination must contain only aliens junior to it (i.e., all must be smaller). So junior aliens can only move when all other aliens are absent from those two sections. So to get the junior aliens to the destination, you need to clear out the senior ones from the path.
This is analogous to the "Reve's puzzle"? Actually it's like the "Tower of Hanoi with three pegs in a line" but with the rule that a disk can only move to an adjacent peg, and you can only move the top disk, and you cannot place a larger disk on a smaller? Let's compare: Classic Tower of Hanoi: three pegs in a line? Usually pegs are not linear but the move can be between any two pegs. However, there is a variant called "Linear" or "Adjacent" Tower of Hanoi, where moves only allowed between adjacent pegs. And the goal is to move a tower from one end to the other. The minimal number of moves for n disks in the Adjacent Tower of Hanoi (with 3 pegs) is known to be something like 3^n - 1? Actually let's recall: The classic Tower of Hanoi with 3 pegs (unrestricted) takes 2^n - 1 moves. If moves only allowed between adjacent pegs (i.e., from A to B, B to C, but not A to C directly), then the minimal moves for moving a tower from A to C is something else. I think there is known puzzle: "Reve's puzzle" is for four pegs. But there is "Tower of Hanoi with three pegs and only adjacent moves", sometimes called "The Tower of Hanoi with Adjacent Moves". I recall the minimum number of moves is 3^n - 1? Let's check small n: n=1: from peg 1 to peg 3? But if only adjacent moves, from 1 to 3 must go 1->2->3, so 2 moves. 2 moves matches our n=1 (2 moves). For n=2: I recall the Adjacent Tower of Hanoi with 3 pegs takes 8 moves? Let's compute. Actually let's simulate classic adjacent move puzzle: Disks can only be moved to adjacent pegs. The goal: move entire stack from peg 1 to peg 3. For n=1: moves: 1->2, 2->3 = 2. For n=2: we can think recursively. One algorithm: Move top (small) from 1 to 3? But that's not adjacent; you must go via 2. Actually moving from 1 to 3 requires at least two moves. But standard solution: Move small disk from 1 to 2 (1 move). Then move large disk from 1 to 3? Not allowed directly because they are not adjacent (1 and 3 are not adjacent). So large disk must go 1->2 then 2->3. But you need to clear small out of the way. Let's compute minimal moves known: I think for n disks the minimal moves = 3^n - 1? For n=1: 3^1-1=2 OK. For n=2: 9-1=8. So predicted 8 moves. But our puzzle's n=2 we haven't found a solution; we need to see if 8 moves are sufficient. Let's attempt to see if we can solve n=2 in 8 moves. In our earlier attempts we struggled to get both to 1. But we only tried naive sequences; maybe a longer sequence works. Let's attempt to systematically solve for n=2 using known Adjacent Hanoi algorithm.
But we must verify that our puzzle exactly matches the Adjacent Tower of Hanoi constraints. In Tower of Hanoi, disks have sizes, only smaller can be placed on larger. In our puzzle, there is no restriction about placing a senior alien on a section with junior aliens; the only restriction is on the move: moving alien must be senior to all in source and destination. This implies that after a move, the destination may have a junior alien, but that will block further moves of that junior alien until the senior leaves? Actually if a senior alien moves to a section with a junior, now that junior is present together with senior. For future moves, if you want to move the junior, you need it to be senior to all in source and destination. But senior is present, so junior cannot move. So effectively, once a senior alien joins a section with a junior, the junior becomes stuck until the senior leaves. So the sections behave like stacks: you can only have a "stack" where larger disks can be on top of smaller? Actually think: The condition for moving any alien: It must be the highest rank among all aliens in the two sections involved. So if a section contains both a senior and a junior, the junior cannot move (since it's not highest), and the senior can move only if it is highest among both sections. If the senior is present with a junior in the same section, the senior is still highest, so it can move out of that section provided the destination section does not contain any alien with higher rank (but there is none). But there is no restriction about moving senior when there are juniors in the source: as long as it is highest, it can move. So a senior can leave a section with juniors. That would free them. So it's not exactly a stack because a larger can be placed on top of smaller, but the smaller cannot move out while larger is present. This is analogous to a stack: you can only remove the top (largest) disk first. Indeed, if we consider each section as containing a set of aliens, the only alien that can be moved out of a section is the one with highest rank among all aliens in that section and the adjacent destination (since the move involves two sections, but for moving out of a section, you also need to consider the destination). However, to move an alien from section A to B, it must be the highest among A∪B. So if B contains some alien of higher rank than the mover, cannot move. So effectively, you can only move the maximum rank among the two sections, and that alien must be moving from one of them. So it's like a "two-stack" combined restriction.
But note that there is no ordering inside a section; all aliens are just present. However, the move constraints effectively enforce that at any time, within a given section, the aliens can be in any combination, but the ability to move is determined by the maximum ranks. This is reminiscent of the "Tower of Hanoi with relaxed placement rule"? Actually the classic Tower of Hanoi rule: only one disk can be moved at a time, each move consists of taking the top disk from one peg and placing it on top of another peg, and no disk may be placed on top of a smaller disk. Here there is no "top" concept: all in a section are co-located. But the move rule that the moving alien must be senior to all in both sections means that if you want to move a disk from a peg, it must be the largest among the two pegs. In classic Tower of Hanoi, when moving a disk from peg A to B, you need that the disk you move is smaller than the top disk on B (or B empty). That is: the moving disk must be smaller than the disk (if any) on the destination peg. Here it must be larger than all on both pegs, which is the opposite! Actually our condition: moving alien must be senior (higher rank) to all others in source and destination. That means it's larger than all other disks. In classic Hanoi, you can only move the smallest disk onto larger ones. So it's reversed: here you can only move the largest disk onto any (since being larger than all, there is no restriction about destination containing smaller). So it's like a reversed Tower of Hanoi where larger disks can be placed anywhere, but smaller ones are blocked when larger present. This suggests the process is like moving a set of tokens with the rule that only the token with highest rank can be moved, and it can be moved to an adjacent section as long as it is the highest among both sections. But that's always true if it's the highest overall? Not exactly: if the highest is in section A, and section B contains some other aliens, if the highest is the highest overall, then it is higher than any in B, so moving from A to B is allowed. So effectively the highest rank alien can always move anywhere (adjacent), provided the destination is adjacent. Because it is senior to all others anywhere. But wait, it must be senior to all in source and destination. Since it is the highest among all aliens, it's automatically senior to all others in both sections. However, consider if the highest is in section A, but there is another alien of same rank? No, ranks distinct. So yes, the highest rank alien (rank n) can always move from its current section to an adjacent section, regardless of who else is there, because it is senior to all other aliens. So the largest alien is "free" to move. But when it moves, it may end up in a section with lower-ranked aliens, which then blocks those lower ones from moving (since they are not highest). But the highest can always move out again. So the largest alien acts like a "key" that can unlock others? Actually it can be used to shuttle.
Now, consider the next largest alien (rank n-1). It can only move when there is no alien with higher rank (i.e., n) in either the source or destination section. Because if n is present in either, then n-1 is not senior to n. So to move n-1, we must first get n out of both sections involved.
This is reminiscent of the "Tower of Hanoi" but with reversed size ordering: The smallest disk is the hardest to move because it can be blocked by any larger disk. Actually in classic Hanoi, the largest disk is hardest because it can only be placed on empty peg. Here the smallest (rank 1) is hardest because it's blocked by any other alien. The largest is free.
Our goal: move all from section 3 to section 1. The initial configuration: all in 3. Since n is largest, we can move it around easily. But we need to get all to 1. This might be analogous to moving a stack where the largest can be moved arbitrarily, but we need to transport the whole set.
Let's try to solve n=2 explicitly using optimal moves. n=2, ranks: 2 (large), 1 (small). We want both from 3 to 1.
We can attempt to think in terms of a state machine. Denote positions of aliens as (position_of_2, position_of_1). Initially (3,3). Goal (1,1). Moves: choose an alien and move it to adjacent section, abiding by condition that it is senior to all in both source and destination.
We need minimal moves. Let's BFS mentally:
State: (p2, p1) with p2, p1 ∈ {1,2,3}. But also condition that aliens can be in same section. So state is multiset per section. Represent as a tuple of sections occupancy? But because aliens are distinct, we can track their positions.
Initial: both at 3.
Goal: both at 1.
Allowed moves: For a given state, an alien x can move from its current section s to adjacent section t (t = s±1) if rank(x) > all ranks of aliens in sections s and t other than itself. Equivalent: x is the maximum among the union of aliens in s and t.
So for each possible move, check if the moving alien is the unique maximum in the two sections.
Let's enumerate possible moves from initial (3,3). Aliens in section 3: both 2 and 1. Which can move? Check move from 3 to 2: For alien 2: check if 2 is senior to all others in sections 3 and 2. Section 3 has alien 1 (rank 1) so 2>1, section 2 empty. So yes. For alien 1: check if 1 is senior to all others in 3 and 2. Section 3 has alien 2, 1<2, so no. So only alien 2 can move from 3 to 2. So one move: move 2 to 2, resulting state: (2,3). (2 at 2, 1 at 3). That's state A.
From state A: positions: 2 at 2, 1 at 3.
Now possible moves: Consider each alien.
Alien 2 at 2: adjacent sections: 1 and 3. Check moving to 1: need 2 senior to all in sections 2 and 1. Section 2 has 2 only (and no other), section 1 empty. So yes. Check moving to 3: need 2 senior to all in sections 2 and 3. Section 2: only 2; section 3: has 1. Since 2>1, yes, allowed. So alien 2 can move to 1 or 3.
Alien 1 at 3: adjacent only 2. Check move to 2: need 1 senior to all in sections 3 and 2. Section 3: only 1? Actually section 3 has 1 (since 1 is there) but also? Check: sections: 3 has alien 1, 2 has alien 2. For alien 1 moving from 3 to 2, need 1 senior to all in 3 and 2. Section 3 has only 1, so okay; section 2 has alien 2. 1 is not senior to 2 (1<2). So not allowed. So only alien 2 can move.
Thus from state A, we can go to:
- Move 2 to 1 => state B: (1,3) (2 at 1, 1 at 3)
- Move 2 to 3 => state back to initial (3,3) since both end up in 3? Actually moving 2 from 2 to 3 results in positions: 2 at 3, 1 at 3 => both at 3, which is initial.
So we can go to B.
Now state B: (1,3). Positions: 2 at 1, 1 at 3.
Check moves:
Alien 2 at 1: adjacent only 2. Move to 2: need 2 senior to all in sections 1 and 2. Section 1: only 2; section 2: empty. So allowed. So alien 2 can move back to 2 => state (2,3) which is A.
Alien 1 at 3: adjacent only 2. Move to 2: need 1 senior to all in 3 and 2. Section 3: only 1; section 2: empty (since 2 is at 1). Actually note: in state B, section 2 is empty. So moving 1 from 3 to 2: check sections 3 and 2. Section 3 has 1, section 2 empty. Since 1 is senior to all (none), allowed. So alien 1 can move to 2! Wait earlier we thought alien 1 cannot move because 2 is in 1 not 2. Condition only checks the two sections involved: source (3) and destination (2). They are sections 3 and 2. The alien 2 is in section 1, not in 3 or 2. So indeed, alien 1 can move because the only other alien (2) is not in those sections. So alien 1 can move to 2. That's a crucial point. So from B, we have another move: move 1 to 2 => state C: (1,2) (2 at 1, 1 at 2).
So state C: 2 at 1, 1 at 2.
Now from state C:
Check moves:
Alien 2 at 1: can move to 2? Adjacent: 2. Need 2 senior to all in sections 1 and 2. Section 1: only 2; section 2: has 1. Since 2>1, allowed. So move 2 to 2 => state D: (2,2) both at 2? Actually after moving 2 from 1 to 2, positions: 2 at 2, 1 at 2 => both at 2, state (2,2).
Alien 1 at 2: adjacent: 1 or 3. Check move to 1: need 1 senior to all in sections 2 and 1. Section 2: contains both 2 and 1? Actually after state C, section 2 contains 1, and section 1 contains 2. Wait state C: 2 at 1, 1 at 2. So section 2 has only 1, section 1 has only 2. For moving alien 1 from 2 to 1: sections involved are 2 and 1. Section 2 has 1 (moving) and maybe others? Only 1 there. Section 1 has 2. Alien 1 is not senior to alien 2 (1<2). So move not allowed. Check move to 3: from 2 to 3, sections 2 and 3. Section 2: has 1; section 3: empty. So 1 is senior to all in those sections (since no other aliens). So alien 1 can move to 3 => state E: (1,3) (2 at 1, 1 at 3). That is back to B (since B was (1,3)). Actually B is (1,3) same. So from C we can go to B via moving 1 to 3? Wait B had 2 at 1, 1 at 3. Yes, that's exactly B. So C -> B via moving 1 to 3.
Thus from C we have two moves: 2->2 leads to D (both at 2); 1->3 leads back to B.
Now state D: both at 2. So positions: (2,2). From D:
Aliens: both in section 2. Which can move? For any move, we consider moving an alien from 2 to adjacent (1 or 3). Condition: alien must be senior to all in sections 2 and destination. Since both are in section 2, the highest rank alien is 2. So alien 2 can move to either 1 or 3. Alien 1 cannot move because section 2 contains alien 2 which is higher, and also the destination might have others; but even if destination empty, since alien 2 is in source, alien 1 is not senior to it, so cannot move. So only alien 2 can move.
- Move 2 to 1: then state becomes (1,2) (since 2 goes to 1, 1 remains at 2) = state C? Actually C was (1,2) yes. So D -> C via 2->1.
- Move 2 to 3: then state becomes (3,2) (2 at 3, 1 at 2) = new state F.
State F: (3,2) meaning 2 at 3, 1 at 2.
From F:
Check moves:
Alien 2 at 3: adjacent 2. Move to 2: need 2 senior to all in 3 and 2. Section 3: only 2; section 2: has 1. 2>1, so allowed. That would lead to (2,2) = D. So F -> D via 2->2.
Alien 2 at 3: also could move? Actually 3 adjacent only 2, so only that.
Alien 1 at 2: adjacent 1 or 3. Move to 1: need 1 senior to all in 2 and 1. Section 2: contains 1 and also? Actually section 2 currently: 1 is there, also? alien 2 is at 3, so section 2 only has 1. Section 1: empty. So move to 1 is allowed (since no other aliens). So move 1 to 1 => state G: (3,1) (2 at 3, 1 at 1). Also move to 3: from 2 to 3: need 1 senior to all in 2 and 3. Section 2: only 1; section 3: has 2. 1 not senior to 2, so not allowed. So only 1->1 allowed (to 1). So from F we can go to G.
State G: (3,1). 2 at 3, 1 at 1.
Now from G:
Check moves:
Alien 2 at 3: move to 2: need 2 senior to all in 3 and 2. Section 3: only 2; section 2: empty. Allowed => state H: (2,1) (2 at 2, 1 at 1).
Alien 1 at 1: move to 2: need 1 senior to all in 1 and 2. Section 1: only 1; section 2: empty (since 2 at 3). Allowed => state I: (3,2) (2 at 3, 1 at 2) which is F. So G -> F via 1->2.
State H: (2,1). 2 at 2, 1 at 1.
From H:
Alien 2 at 2: adjacent 1 or 3. Move to 1: need 2 senior to all in 2 and 1. Section 2: only 2; section 1: has 1. 2>1, allowed => state J: (1,1) which is goal! (both at 1). Move to 3: from 2 to 3: need 2 senior to all in 2 and 3. Section 2: only 2; section 3: empty. Allowed => state K: (3,1) (2 at 3, 1 at 1) which is G.
Alien 1 at 1: move to 2: need 1 senior to all in 1 and 2. Section 1: only 1; section 2: has 2 (since 2 at 2). 1 not senior to 2, so not allowed. So only alien 2 can move.
Thus from H we can reach goal J in one move: 2->1.
So we found a path: start -> A -> B -> C -> D -> C? Wait need to construct shortest path to goal.
Let's compute distances.
Define states and shortest moves from start (both at 3) to goal (both at 1). We can BFS with edges being moves.
We'll list states with positions of 2 and 1 (order: high, low). But careful: both at same section leads to state like (3,3) (both at 3), (2,2) (both at 2), (1,1) goal, also (1,2), (1,3), (2,1), (2,3), (3,1), (3,2). That's 9 possible states. Let's compute transitions systematically.
We'll label states as (position of rank 2, position of rank 1). Since they are distinguishable, but sometimes they co-locate. Could also treat occupancy per section but this representation fine.
Initial: (3,3).
Goal: (1,1).
Now list all transitions we deduced:
From (3,3): only move 2->2? Actually 2 can move from 3 to 2 => (2,3). Also could 2 move to? 3's adjacent only 2. So only (2,3). So initial -> A: (2,3) with cost 1.
From (2,3): as A. Moves:
- 2->1 => (1,3)
- 2->3 => (3,3)
- 1->2? not allowed
Thus edges: (2,3) -> (1,3) and (2,3) -> (3,3).
From (1,3): B. Moves:
- 2->2 => (2,3)
- 1->2 => (1,2)
So B -> (2,3) and B -> (1,2).
From (1,2): C. Moves:
- 2->2 => (2,2) (since 2 moves from 1 to 2, both at 2)
- 1->3 => (1,3) (since 1 moves from 2 to 3)
- 1->1? Could 1 move to 1? From 2, adjacent 1 and 3. We considered 1->3, also 1->1? Actually from 2, adjacent are 1 and 3. We considered 1->3 (to 3) and 1->1 (to 1). For moving 1 to 1: need 1 senior to all in 2 and 1. Section 2: only 1; Section 1: has 2 (since 2 at 1). So 1 not senior to 2 => not allowed. So only 1->3.
Thus C -> (2,2) and C -> (1,3).
From (2,2): D. Moves:
- 2->1 => (1,2)
- 2->3 => (3,2)
So D -> (1,2) and D -> (3,2).
From (3,2): F. Moves:
- 2->2 => (2,2)
- 1->1 => (3,1)
So F -> (2,2) and F -> (3,1).
From (3,1): G. Moves:
- 2->2 => (2,1)
- 1->2 => (3,2)
So G -> (2,1) and G -> (3,2).
From (2,1): H. Moves:
- 2->1 => (1,1) goal
- 2->3 => (3,1)
So H -> (1,1) and H -> (3,1).
Also other states? (2,? ) maybe (2,1) we have; (1,1) goal; (3,1) we have; (3,2) we have; (2,3) we have; (1,3) we have; (1,2) we have; (2,2) we have; (3,3) initial.
Also (?,?) maybe (2,? ) symmetric? That's all 9.
Now we can compute shortest path from (3,3) to (1,1). Let's BFS.
Level 0: (3,3)
Level 1: (2,3) [distance 1]
From (2,3): level 2: (1,3) and (3,3) (back)
(1,3) is new at distance 2.
From (1,3): neighbors: (2,3) (already visited), (1,2) new -> distance 3.
From (1,2): neighbors: (2,2) and (1,3). (2,2) new -> distance 4.
From (2,2): neighbors: (1,2) (visited), (3,2) new -> distance 5.
From (3,2): neighbors: (2,2) (visited), (3,1) new -> distance 6.
From (3,1): neighbors: (2,1) and (3,2). (2,1) new -> distance 7.
From (2,1): neighbors: (1,1) goal, (3,1) (visited). So goal reached at distance 8 (since from (2,1) to (1,1) one move). So distance from (3,3) to (1,1) is 8 moves.
Thus minimal moves for n=2 is 8. And we output answer modulo m. For n=2, answer would be 8 mod m. For m=8 in sample? Sample input: 3 8 gives output 2. That means for n=3, answer mod 8 is 2. So we need to compute moves for n=3? But sample 2 had n=3, m=8, output 2. So answer for n=3 is 26 (as described in note) mod 8 = 2. So indeed moves for n=3 is 26. So we see pattern: n=1 -> 2, n=2 -> 8, n=3 -> 26. Let's compute ratios: 2, 8, 26. Differences: 6, 18. Might be 3^n? 3^1=3, minus 1 =2 matches. 3^2=9, minus 1 =8 matches. 3^3=27, minus 1 =26 matches. So it seems minimal moves = 3^n - 1.
Check: For n=1: 3^1-1=2, yes. n=2: 9-1=8, yes. n=3: 27-1=26, yes. So pattern holds.
Thus answer = 3^n - 1 modulo m.
But need to confirm for n=0? not given (n>=1). So answer is (3^n - 1) mod m.
But careful: Is it always minimal? The problem statement says "minimum time (in minutes) they will need to perform this task." And we found sequence of moves for n=2 achieving 8 moves, which equals 3^2-1. For n=3, they gave example sequence length 26 which is 3^3-1. So indeed matches.
Thus answer is 3^n - 1 mod m.
Now constraints: n up to 1e9, m up to 1e9. So we need to compute 3^n mod m (and then subtract 1, adjust modulo). However, note that m can be up to 1e9, but n is huge (1e9). So we need to compute 3^n mod m efficiently using fast exponentiation modulo m. That's O(log n) time, acceptable. However, there's a nuance: m can be 1, then mod 1 yields 0. But careful: modulo operation with m might be 0? Since m>=1. If m=1, then answer is 0. Compute (3^n - 1) mod 1 = 0. So fine.
But wait: m can be as large as 1e9, which is within 32-bit int, but 3^n can be astronomically large, we only need modulo.
Thus solution: read n, m. Compute pow = modular exponentiation of 3^n mod m. However, we need (3^n - 1) mod m. Since we compute pow_mod = 3^n % m. Then result = (pow_mod - 1 + m) % m to ensure non-negative.
But is that correct? For example n=3, m=8: 3^3=27 mod 8 = 3, minus 1 = 2, matches sample.
Edge cases: n large, but exponentiation mod m fine.
But we should double-check if the recurrence indeed yields 3^n - 1. Could there be any twist? Perhaps for n=0 (not needed). So answer appears to be that.
But wait, the puzzle might have a more complex recurrence due to the fact that multiple aliens can be in the same section and the move condition that the moving alien must be senior to all in both source and destination leads to a recursion similar to the Tower of Hanoi with adjacent moves. Let's reason why minimal moves = 3^n - 1.
We can think recursively. Let T(n) be minimal moves to transfer n aliens from section 3 to section 1. Because the largest alien (rank n) can move freely, but it might need to shuttle. Actually classic adjacent Tower of Hanoi: moving a tower of n disks from peg 1 to peg 3 (with pegs labeled 1,2,3 and only adjacent moves allowed) has minimum moves: 3^n - 1. That is known. But note that in classic adjacent Hanoi, disks can only be moved to adjacent pegs, and cannot place a larger disk on a smaller one. In our puzzle, the movement rules are different: the moving disk must be the largest among the two pegs. However, we suspect it's equivalent to the reversed rule? Let's examine.
Classic adjacent Hanoi: Three pegs in a line: left (1), middle (2), right (3). Moves allowed only between adjacent pegs (1<->2, 2<->3). Goal: move all disks from peg 1 to peg 3. Disks have sizes 1 (smallest) to n (largest). Rules: Only one disk can be moved at a time; a disk can only be placed on an empty peg or on a larger disk. The minimal number of moves is known to be 3^n - 1. For n=1: 2 moves (1->2, 2->3) = 2. For n=2: 8 moves. For n=3: 26 moves. So indeed the numbers match exactly. So our puzzle yields the same numbers. That suggests that our puzzle is isomorphic to the classic adjacent Tower of Hanoi (with the usual rule) but with perhaps a reversal of order? Let's check mapping.
In classic adjacent Hanoi, the largest disk is hardest to move because it can only be placed on empty pegs? Actually the usual rule: you cannot place a larger disk on a smaller disk. In our puzzle, the largest alien is the most mobile because it can be moved anywhere, but the smallest is hardest. In classic Hanoi, the smallest is most mobile because it can be placed anywhere, and the largest is hardest. So the roles are reversed. However, if we invert the order of ranks (i.e., treat rank 1 as largest? But in our puzzle rank numbers higher = senior. In classic Hanoi, larger disk number means larger size, which is more restrictive. But in our puzzle, senior (higher rank) can move more freely, while junior is restricted. So if we map aliens to disks in reverse order (alien rank r corresponds to disk size n+1-r), then the movement condition might correspond to the usual Hanoi rule? Let's test.
Define disk size = n+1 - rank. So largest alien (rank n) becomes disk size 1 (smallest). Junior alien (rank 1) becomes disk size n (largest). In classic Hanoi, the smallest disk can move anywhere (since it can be placed on any disk) and is most mobile. That matches senior alien being mobile. And the largest disk (disk size n) can only be placed on empty peg (or on larger? Actually only on empty? Usually you cannot place a disk on a smaller one; larger can be placed on empty or on even larger? But the largest disk cannot be placed on any because there is no larger; it can only be placed on empty peg. So the largest disk is restricted: it can only move when the destination is empty. In our puzzle, the junior alien (rank 1) can only move if both source and destination have no other aliens (since any other alien is senior to it). That means it can only move to an empty section (or a section containing only aliens with even smaller ranks? But there are none smaller than rank 1; so indeed rank 1 can only move if both sections contain no other aliens. So it effectively can only be moved to an empty section (or perhaps a section containing aliens with rank lower? none). So it's like the largest disk in Hanoi which can only be placed on an empty peg. So indeed, if we reverse the ordering, the constraints map to the classic Hanoi rule: a disk can only be moved onto a larger disk (or empty). But need to check exactly: In classic Hanoi, when moving a disk from peg A to peg B (adjacent), we require that the top disk on B is larger than the disk we are moving (or B empty). In our puzzle, if we consider reversed ranks (disk size = n+1 - rank), then the condition "moving alien must be senior to all in source and destination" becomes: the moving alien's rank r is highest among those two sections => its reversed size s = n+1-r is smallest among those two sections. So the moving alien is the smallest disk in those two sections. In classic Hanoi, you can only move the top disk, but here there is no stacking order. However, in our puzzle, any alien can be moved regardless of order, but the condition that it must be senior (i.e., smallest reversed size) effectively means you can only move the smallest disk among the two sections. And after moving, it can be placed onto any other aliens (since it is smallest, it can be placed on larger ones). That is exactly analogous to the rule that you can only move the smallest disk? Not exactly: In classic Hanoi, you can only move the top disk of a peg; but if disks are stacked in increasing size order (small on top), then the smallest disk is at the top of its peg. In our puzzle, there is no stacking order, but the condition that the moving alien must be smallest among two sections effectively means that if there are multiple aliens on a section, only the smallest among them can move if the destination also has no smaller disk. Actually you can move any alien as long as it's the smallest in the two sections. That would be analogous to a relaxed version? But we can perhaps show that optimal solution corresponds to a state where each section contains aliens sorted in increasing rank order? In the classic Hanoi, you maintain that disks on a peg are in increasing size from top to bottom. In our puzzle, since there is no ordering, you could have a section with senior (largest) and junior (smallest) together; but then the smallest cannot move because it's not smallest? Actually senior is largest, so the smallest is junior; but junior is not senior, so it's not allowed to move because the moving alien must be senior (largest). So only the largest can move. So if a section contains both largest and smallest, only largest can move. That is opposite of classic Hanoi where only smallest (top) can move. So reversed ranking mapping yields: largest alien (rank n) becomes disk size 1 (smallest) and can move freely; junior alien (rank 1) becomes disk size n (largest) and can only move to empty. So the condition that only the largest alien can move when multiple together translates to only the smallest disk (in reversed) can move, which is like the classic rule where only the top disk (smallest) can move. However, in classic Hanoi, you also have the stacking order: larger disks cannot be placed on smaller. In our puzzle, after moving a senior alien (largest) to a section with a junior (smallest), there is no restriction; you can have senior on top of junior. But in reversed mapping, that would correspond to placing a smallest disk on a larger disk, which is allowed. And moving a junior alien (smallest) is only allowed if both sections have no senior (larger) aliens, i.e., destination must be empty or contain only even smaller aliens (none). So effectively junior alien can only be moved to empty sections, analogous to largest disk can only be placed on empty peg. And after moving, the configuration might have a mixture of disks sizes on a peg not in sorted order. But in reversed mapping, would that violate the classic Hanoi rule? In classic Hanoi, you cannot have a larger disk on top of a smaller one; i.e., pegs must always have disks in increasing size order (small on top). In our puzzle, if we map reversed ranks, what would the arrangement be? Consider two aliens, rank 2 (senior) and rank 1 (junior). In reversed mapping, they become sizes: 2 -> size 1 (small), 1 -> size 2 (large). In our puzzle, we can have both in same section. In reversed terms, that would be a peg with both size 1 (small) and size 2 (large). But are they in any order? There is no order; they just co-exist. In classic Hanoi, such a configuration would be illegal because you cannot have a larger disk on top of a smaller? Actually you could have small on top of large (if small placed on large), but you cannot have large on top of small. Since there is no stacking order, the configuration is ambiguous. However, the move condition in our puzzle ensures that only the senior (largest) can move when both are together. In reversed mapping, that means only the smallest (size 1) can move when both are together. In classic Hanoi, if a peg has small on top of large (allowed), the top disk is small, so you can move small. That's consistent: you can only move the top disk, which is the smallest. So if we imagine that within a section, the aliens are stacked in order of rank? But we don't have explicit stacking. However, the move condition effectively forces that the only movable alien from a section (when considering moves to a particular adjacent section) is the one with highest rank (i.e., the most senior). But if there are multiple aliens in a section, which one can move? Only the highest-ranked among the union of the two sections. If that highest-ranked alien is in the source section, it can move. If the highest-ranked alien is in the destination, you cannot move any from source because any alien in source would not be highest (since destination has higher). So the movable alien is always the global maximum among the two sections. This is different from classic Hanoi where you can move any top disk as long as it's smaller than the top of the destination. But maybe the dynamics are equivalent to classic adjacent Hanoi if we reverse the order and consider that we always maintain that on each peg, aliens are arranged in increasing order of rank from "bottom" to "top"? Not necessarily, but we can observe that the optimal solution uses the same number of moves. And it's known that the minimal number of moves for the adjacent Tower of Hanoi is 3^n - 1. So answer is that.
But we should also consider if there is any nuance: In the problem, initially all aliens are in section 3, and they all need to move to section 1. In classic adjacent Hanoi, the typical goal is to move the entire tower from peg 1 to peg 3. Here it's from 3 to 1. But due to symmetry, the number of moves would be same. Also the allowed moves are only between adjacent sections. So indeed it's the same puzzle but with reversed labeling of pegs (1 and 3 swapped). So the minimal moves are 3^n - 1.
Thus answer = (3^n - 1) mod m.
But wait, the sample for n=3 gave 26 moves, which matches 3^3 - 1 = 26. And the note says "In total: the aliens need 26 moves." So confirmed.
Thus we need to compute 3^n mod m, then subtract 1 modulo m.
Implementation: Since n can be up to 1e9, we can compute pow_mod(3, n, m) using fast exponentiation. However, note that m can be as large as 1e9, which fits in 64-bit. Multiplication of two ints up to 1e9 may overflow 32-bit, so use 64-bit (long long). In C++ we can use long long for intermediate.
Edge Cases: m could be 1, then result 0. Also note that 3^n may be huge, but mod m we can compute.
But is there any possibility that minimal moves are less than 3^n - 1? The puzzle's note for n=3 gives 26 moves, which is minimal? They didn't explicitly say it's minimal but they said "the aliens need 26 moves" and then they compute remainder, so it's likely the minimal. And our BFS for n=2 gave 8, which matches 3^2-1=8. So it's minimal.
Thus answer is straightforward.
But wait, there is also a known formula for the "Reve's puzzle"? Not needed.
Thus we can solve.
Now we must be cautious: For n up to 1e9, exponentiation O(log n) is fine. But there might be a catch: m can be up to 1e9, but 3^n mod m can be computed using exponentiation by squaring. However, if m is not prime, no problem.
One nuance: Since n can be huge, we need to ensure we don't run out of time, but log2(1e9) ~ 30 steps, so it's trivial.
Thus solution: read n, m; compute res = pow_mod(3, n, m); res = (res - 1 + m) % m; print res.
But verify with sample:
Sample1: n=1,m=10: 3^1=3, 3-1=2 mod10=2. output 2. ok.
Sample2: n=3,m=8: 3^3=27, 27-1=26 mod8=2. output2. ok.
Edge test: n=1,m=1: 3%1=0, (0-1+1)%1=0? Actually modulo 1 everything is 0, so result 0. That's fine.
But wait, what about n=0? Not in constraints.
So answer is simple. However, I want to double-check for n=4 to see if pattern holds maybe via recurrence. But I'm confident.
But before finalizing, let's derive recurrence to ensure T(n)=3^n-1.
We can define T(n) = minimal moves to transfer n aliens from peg 3 to peg 1, with the given move rules. Let's attempt to derive recurrence.
Consider the largest alien (rank n). Since it can move freely (senior to all), it can be used as a shuttle. The typical solution for adjacent Tower of Hanoi: To move n disks from peg A to peg C (with pegs A, B, C in line), you need to: move top n-1 disks from A to C (using B as intermediate), then move the largest disk from A to B, then move n-1 disks from C to A, then move largest from B to C, then move n-1 from A to C. That recurrence gives T(n) = 3T(n-1) + 2, with T(1)=2, leading to T(n)=3^n-1. Actually check: T(1)=2. For n>1, to move n disks from left to right: (1) Recursively move n-1 disks from left to right (using middle) = T(n-1) moves. (2) Move largest disk from left to middle: 1 move. (3) Recursively move n-1 disks from right to left = T(n-1) moves. (4) Move largest disk from middle to right: 1 move. (5) Recursively move n-1 disks from left to right = T(n-1) moves. Total T(n) = 3T(n-1) + 2. That recurrence yields T(n) = 3^n - 1 (since 3*(3^{n-1}-1)+2 = 3^n -3+2=3^n-1). So indeed.
But does this algorithm apply to our puzzle with the reversed order? Let's see if we can mimic that with aliens. Let’s denote aliens 1..n with ranks, where n is largest. We want to move all from peg 3 to peg 1. Equivalent to moving all from right to left. Using symmetry, we can rename pegs: treat peg 3 as source, peg 1 as target, peg 2 as middle. Then algorithm: To move n aliens from source (3) to target (1), we can:
- Move the n-1 smaller aliens (ranks 1..n-1) from source (3) to target (1) recursively? But that seems like we want to get them out of the way to allow largest to move. In classic Hanoi (disks from left to right), we move n-1 smaller disks from source to auxiliary (the other end) to free largest. But here with reversed rules? Let's attempt to map the known algorithm.
In classic adjacent Hanoi (with disks 1 smallest to n largest, rule: cannot place larger on smaller), to move n disks from peg 1 to peg 3, we need to use peg 2. The recursive algorithm: Move n-1 disks from 1 to 3 (using 2), move disk n from 1 to 2, move n-1 disks from 3 to 1 (using 2), move disk n from 2 to 3, move n-1 disks from 1 to 3 (using 2). That's the known minimal algorithm.
If we reverse the order of disks (i.e., treat our