This is a simplified version of the task Toy Train. These two versions differ only in the constraints. Hacks for this version are disabled.
Alice received a set of Toy Train™ from Bob. It consists of one train and a connected railway network of n stations, enumerated from 1 through n. The train occupies one station at a time and travels around the network of stations in a circular manner. More precisely, the immediate station that the train will visit after station i is station i+1 if 1 ≤ i < n or station 1 if i = n. It takes the train 1 second to travel to its next station as described.
Bob gave Alice a fun task before he left: to deliver m candies that are initially at some stations to their independent destinations using the train. The candies are enumerated from 1 through m. Candy i (1 ≤ i ≤ m), now at station a_i, should be delivered to station b_i (a_i ≠ b_i).
The blue numbers on the candies correspond to b_i values. The image corresponds to the 1-st example.
The train has infinite capacity, and it is possible to load off any number of candies at a station. However, only at most one candy can be loaded from a station onto the train before it leaves the station. You can choose any candy at this station. The time it takes to move the candies is negligible.
Now, Alice wonders how much time is needed for the train to deliver all candies. Your task is to find, for each station, the minimum time the train would need to deliver all the candies were it to start from there.
Input
The first line contains two space-separated integers n and m (2 ≤ n ≤ 100; 1 ≤ m ≤ 200) — the number of stations and the number of candies, respectively.
The i-th of the following m lines contains two space-separated integers a_i and b_i (1 ≤ a_i, b_i ≤ n; a_i ≠ b_i) — the station that initially contains candy i and the destination station of the candy, respectively.
Output
In the first and only line, print n space-separated integers, the i-th of which is the minimum time, in seconds, the train would need to deliver all the candies were it to start from station i.
Examples
Input
5 7
2 4
5 1
2 3
3 4
4 1
5 3
3 5
Output
10 9 10 10 9
Input
2 3
1 2
1 2
1 2
Output
5 6
Note
Consider the second sample.
If the train started at station 1, the optimal strategy is as follows.
Load the first candy onto the train.
Proceed to station 2. This step takes 1 second.
Deliver the first candy.
Proceed to station 1. This step takes 1 second.
Load the second candy onto the train.
Proceed to station 2. This step takes 1 second.
Deliver the second candy.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Hence, the train needs 5 seconds to complete the tasks.
If the train were to start at station 2, however, it would need to move to station 1 before it could load the first candy, which would take one additional second. Thus, the answer in this scenario is 5+1 = 6 seconds.
We are given a circular railway with n stations (2 ≤ n ≤ 100). There are m candies (1 ≤ m ≤ 200). Each candy i has a starting station a_i and a destination station b_i (a_i != b_i). The train moves in a circle, each step takes 1 second. At each station, when the train arrives, we can unload any number of candies (that are on the train and whose destination is that station) instantaneously. Also, we can load at most one candy from the station onto the train before it leaves the station. The train can hold unlimited candies. The loading happens at the moment when the train is at the station, before moving to next station. We can choose which candy to load (if multiple are present initially). However, note that once a candy is loaded, it remains until delivered. The train starts at some station s (given in output we need to compute for each start station). The goal is to deliver all candies (i.e., all m candies must be picked up from their start stations and dropped at their destination stations). We need to compute the minimum time required to finish, starting from each station s.
We need to compute for each start station s, the minimal time needed to deliver all candies.
We need to think about the problem. This is a classic "Toy Train" problem (Codeforces problem 1129A? Actually it's "Toy Train" from Codeforces Round #542 [Div. 2]? There's a problem "Toy Train" with constraints n up to 5000? I recall a problem: given circular track, each candy has start and destination, train can pick up at most one candy per station per visit, and drop off any number. Find minimal time to deliver all candies starting from each station. The solution typically: For each station, we consider the candies that start at that station. For each such candy, the travel distance from its start to its destination along the circle (clockwise) is something. Since we can only pick up one candy per visit to the start station, if there are k candies at a station, we must visit that station at least k times (or at least, we need to pick up each candy; but we might pick up more than one per visit? No, at most one per station before it leaves. So we need at least as many visits as the number of candies at that station). However, we can also combine pickups from multiple stations in one round trip. The train can go around and pick up candies, deliver them later.
We need to compute minimal time to finish all deliveries starting from s. The train moves only clockwise (I think it's deterministic: after station i it goes to i+1 mod n). It's a directed cycle, movement only one direction. So the train always moves forward (clockwise) one station per second.
We need to plan the sequence of actions (which candy to pick up at each station each time we visit) to minimize total time until all candies are delivered.
The problem seems like we need to compute, for each start station s, the minimal time such that we can schedule pickups and deliveries.
One standard solution for this problem (CF 1129A1/A2? Actually there is "Toy Train" with constraints n up to 2000 and m up to 2000? The simpler version (n,m <= 200) is exactly like this. The typical solution: For each station i, consider the number of candies that start at i, call cnt[i]. Also, for each station i, consider the distances (clockwise) from i to b_j for candies starting at i. Among those candies, we need to deliver them. Since we can only pick up one per visit, if we visit i k times (k >= cnt[i]), the last candy we pick up from i will have the longest time to be delivered, because we might need to go around after picking it up. Actually, we can think of an optimal strategy: The train will make some number of full laps or partial moves. Usually, we can think of delivering candies as: For each station i, we need to make cnt[i] visits to i to pick up all candies. On each visit, we may pick one candy, and then we might go forward and deliver some candies. The total time is essentially the time until the last candy is delivered.
The train can be considered to do multiple trips. Since the track is a circle, it may be optimal to just keep moving continuously, never stopping (we only have loading/unloading at stations, but those are instantaneous). So the train just moves around the circle. At each station, we can decide to load a candy if there are any left at that station (not yet loaded). We must also unload any candies whose destination is that station.
Thus, the process is deterministic given a schedule of which candy to load at each station each time the train arrives. So we need to find the minimal T such that there exists a schedule that completes by time T, starting from s.
One approach: For each start s, compute minimal time needed. Usually, the solution in Codeforces for this problem is:
For each station i, let cnt[i] = number of candies starting at i. Also, for each station i, let minDist[i] = minimal clockwise distance from i to a destination b among candies starting at i (if no candies, maybe ignore). Actually, we need to compute for each station i, the maximum "offset" among its candies? Let's recall typical solution.
I recall problem: "Toy Train" from Codeforces Round #542 (Div. 2) B? Actually it's problem A (maybe A2?). The solution: For each start station s, the answer is max over i of ( (cnt[i] - 1) * n + dist(s, i) + w[i] ), where dist(s, i) is clockwise distance from s to i, and w[i] is something like the minimal distance from i to the farthest destination among candies starting at i? Actually, I need to recall.
Let me search memory: There's Codeforces problem "1129A - Toy Train"? I think it's 1130A? Let's check memory: I recall a problem: "1129A Toy Train" appears in Codeforces Round #542 (Div. 2) (which is 1130? Actually Round #542 had problem C? Not sure). There's also "1130A Toy Train"? No.
But I recall a known solution: For each station i, compute two values: cnt[i] = number of candies starting at i; and the minimum distance (or maximum?) among candies from i to its destination? Actually we need to consider the time to deliver all candies from i after we have arrived at i to pick them up. If we have cnt[i] candies at i, we need to visit i cnt[i] times. The last candy we pick up will be the one that has the farthest delivery distance (in terms of the clockwise distance from i to its destination). Because we can reorder pickups: we can choose to pick up the candy with the farthest destination first? Actually, think about it: Suppose we have multiple candies at i. The train will arrive at i multiple times. At each arrival, we pick one candy. After picking a candy, we might continue moving forward and eventually deliver it. The total time from start until that candy is delivered equals: time to reach i for the pickup (which depends on which visit) + the distance from i to its destination (clockwise). Since we can decide the order of pickups, we can assign the candy with the largest distance to the earliest pickup (so that it gets delivered earlier) or to the later pickup? Let's analyze.
We want to minimize the total completion time (makespan). The completion time is the time when the last candy is delivered. The candies from station i will be delivered at times that depend on when we pick them up and their distances. For station i, if we make cnt[i] visits, the times of those visits (the moments when the train is at i) will be something like t0 + dist(s,i) mod something plus multiples of n (because each full lap takes n seconds). More precisely, since the train moves continuously, the arrival times at station i are periodic: if we start at s at time 0, the first time we reach i is d = dist(s,i) seconds (clockwise distance from s to i). Then we will be at i again after each full lap: d + n, d + 2n, ... So the k-th visit to i (k starting from 1) occurs at time d + (k-1)*n.
At each visit, we can pick up at most one candy from i. So we need to assign the cnt[i] candies to distinct visits (likely the first cnt[i] visits, as delaying later visits would increase completion time). So we will pick up candies on visits 1 through cnt[i]. The delivery time of a candy picked up on the k-th visit at time T_pick = d + (k-1)*n is T_delivery = T_pick + dist(i, dest) (clockwise distance from i to its destination) because the train will travel forward, and when it reaches dest (which is after moving that many steps) it can drop it off. However, note that after picking up, the train may go around; but the candy stays on train until dest is encountered. Since the train moves continuously, it will reach dest at some time: T_pick + dist(i, dest) (mod n considerations; but since we start at i and move forward, the first encounter of dest after i is exactly dist(i, dest) steps, which is between 1 and n-1). So yes, if we pick up at time T_pick, the candy will be delivered at T_pick + dist(i, dest).
Thus, the delivery times for candies from i are: for each candy assigned to visit k (k=1..cnt[i]), delivery time = d + (k-1)*n + dist(i, dest). We want to minimize the maximum of all delivery times over all candies (and across all stations). Since we can assign the candies from each station to the visits arbitrarily, we can assign the candy with the smallest dist(i, dest) to the latest visit? Because we want to minimize the maximum, we should assign the largest distances to earlier visits to reduce the largest delivery time. Actually, consider two candies from same station with distances d1 and d2 where d1 > d2. If we assign d1 to visit k (with delivery time = base + (k-1)n + d1) and d2 to visit k+1, then the delivery time for d1 is base + (k-1)n + d1, and for d2 is base + kn + d2. Which one is larger? Possibly the later visit plus smaller distance could be less or more than the earlier visit plus larger distance. Since n is at least 2, (k-1)n + d1 vs kn + d2 = (k-1)n + n + d2. So the later visit adds n, which is likely larger than the extra distance d1 - d2. So to minimize the maximum, we should assign the larger distances to later visits? Let's check: Suppose d1 > d2. If we assign d1 to earlier visit and d2 to later, then times: T1 = base + (k-1)n + d1, T2 = base + kn + d2. Since kn + d2 = (k-1)n + n + d2. Compare T1 and T2: T1 vs T2 = d1 vs n + d2. Since d1 <= n-1 (max distance is n-1), and n + d2 >= n (since d2>=1). Typically, n + d2 > d1 because n >= 2. So T2 > T1. So the later visit yields a larger time due to the n added. So the maximum would be determined by the candy assigned to the latest visit (largest k) regardless of distance? But note that d1 could be close to n-1, and d2 could be 1. Then T1 = base + (k-1)n + (n-1) = base + kn -1, T2 = base + kn + 1 = base + kn + 1. Actually, careful: if k is the last visit (say cnt[i] = K), then the largest k among visits is K. For the last visit, we have time base + (K-1)*n + distance. For a candy assigned to the last visit, its delivery time = base + (K-1)*n + dist. For any earlier visit, the delivery time = base + (k-1)*n + dist. Since (K-1)*n is the largest multiple, the delivery times for the last visit are generally the largest, unless distance for earlier visits are huge? But distances are at most n-1, so adding distance can't compensate for missing n factor. So indeed, the maximum delivery time among candies from station i will be for the candy picked up on the last visit (k = cnt[i]), because it has the largest multiple of n. However, there is nuance: if cnt[i]=0, ignore.
Thus, for station i, the last candy we pick up will be delivered at time = d + (cnt[i]-1)*n + maxDist? Actually, since we can choose which candy to pick up on the last visit, we should pick the one with the smallest distance to minimize the completion time from that station. Because the last visit's delivery time = base + (cnt[i]-1)*n + dist_assigned. To minimize the overall maximum, we would assign the candy with the smallest distance to the last visit, because that reduces the time for the last delivered candy from this station. Similarly, for earlier visits, we can assign larger distances, because they will be delivered earlier anyway. So the best strategy for each station i is: sort candies by distance (clockwise from i to destination) descending, and assign the largest distances to earliest visits, and smallest distance to the latest visit. Then the maximum delivery time for station i will be for the last visit (k = cnt[i]) with the smallest distance, i.e., T_i_max = d + (cnt[i]-1)*n + minDist_i? Wait: careful: we want to minimize the max across all candies; for station i, the largest delivery time among its candies will be the maximum over k of (d + (k-1)*n + dist_assigned(k)). Since (k-1)*n increases with k, the maximum is likely for k = cnt[i] (the last), because adding n per later k. But is it possible that for k=cnt[i] we assign a very small distance, and for k=cnt[i]-1 we assign a very large distance, making that larger? Compare:
For k = K-1: T_{K-1} = d + (K-2)*n + dist_large
For k = K: T_K = d + (K-1)*n + dist_small
We want to know which is larger: T_{K-1} vs T_K. T_K - T_{K-1} = n + dist_small - dist_large. Since dist_small <= dist_large (if we assign small to last, large to earlier), dist_small - dist_large ≤ 0, so T_K - T_{K-1} could be less than n, maybe even negative? Actually dist_small could be much smaller than dist_large. The difference is n + dist_small - dist_large. Since dist_large could be up to n-1, dist_small could be as low as 1. Then n + 1 - (n-1) = 2. So it's positive. But could it be negative? That would require dist_large > n + dist_small. But dist_large ≤ n-1, and n + dist_small ≥ n+1 (if dist_small>=1). So dist_large cannot exceed n+dist_small. Actually max dist_large = n-1, min dist_small = 1, n+dist_small = n+1, so difference is at least n+1 - (n-1)=2. So T_K > T_{K-1}. So indeed T_K is larger. If we assigned differently, say small to earlier and large to later, then T_K would be even larger because then dist_large is on later visit, making T_K = d + (K-1)n + large, which is even larger than T_K with small. So the optimal assignment to minimize the max is indeed to put the smallest distance on the last visit. Then T_i_max = d + (cnt[i]-1)n + (min distance among candies from i). Let's denote for each station i: let minDist[i] be the minimum clockwise distance from i to destination for candies starting at i? Actually we need the smallest distance. Because we want the smallest distance to assign to last pickup, so the max delivery time from i is based on the smallest distance. However, is it always smallest? Let's test with an example: Suppose station i has two candies with distances 2 and 5, and cnt=2. Then if we assign distances: last visit get smallest (2), first visit get largest (5). Then delivery times: first visit: d + 0n +5 = d+5; last visit: d + 1n +2 = d+n+2. Since n≥? For n≥6? Actually n is given, but n may be small. If n=5, then d+n+2 = d+7, and d+5 = d+5, so max = d+7. If we swapped assignment (last get 5, first get 2): then times: d+0n+2 = d+2; d+1n+5 = d+5+5? Actually d+n+5 = d+10? Wait n=5: d+5+5 = d+10, so max = d+10 > d+7. So indeed assigning smallest to last yields lower max. Good.
What if distances are equal? doesn't matter.
Thus, for station i, the contribution to the overall makespan (if we consider only candies from i) is: T_i = dist(s, i) + (cnt[i] - 1) * n + t_i, where t_i = minimum distance among candies from i to their destination? Actually check: T_i = first arrival to i (dist(s,i)) + (cnt[i]-1)*n (the extra laps to come back cnt[i] times) + the distance for the candy delivered last from i. Since we assign the smallest distance to last pickup, that distance is the minimal distance among candies starting at i. But wait: The last pickup occurs at time d + (cnt[i]-1)*n. Then we add the distance to its destination, which is the minimal distance. So T_i = d + (cnt[i]-1)*n + minDist[i].
But is that always the time when the last candy from i is delivered? Yes, because that candy is delivered at that time, and earlier candies from i are delivered earlier. So the last delivery from station i occurs at that time.
However, we must also consider that the train may be delivering candies from multiple stations interleaved. The overall completion time is the maximum over all candies of their delivery time. So it's max over i of T_i (for stations with cnt[i]>0). Because for stations with no candies, they contribute no deliveries. So the answer for start s should be max_{i: cnt[i]>0} ( dist(s,i) + (cnt[i]-1)*n + minDist[i] ). But is this always achievable? We must also consider that while we are picking up candies from station i, we may also be picking up from other stations, and the schedule must be consistent: the train's movement is fixed; it arrives at stations at fixed times. The assignment we described for each station i assumes that on the first cnt[i] arrivals at i, we will pick up a candy. But what if those arrivals are also needed for picking up candies from other stations? That's fine, because at each station, we can pick up at most one candy each visit. So as long as we schedule for each station to use its first cnt[i] arrivals for pickups, that is feasible provided these arrivals are distinct in time (they are, because each station has its own arrival times). But there is a potential conflict: The train may need to be at two stations at the same time? No, train is at one station at a time; arrivals at different stations are at different times. So it's okay.
But is there any interaction that might require us to skip picking up a candy at some arrival to some station, because we might need to prioritize something else? No, we can pick up as we like. There's no capacity limit except per station one per visit. So we can always pick up a candy at a station if there is a candy left and we choose to. The train can hold unlimited candies. So the simple schedule: at each station arrival, if there are remaining candies at that station, load one (any). But we need to ensure that we deliver all candies, and the order of loading might affect delivery times of candies from other stations? Actually, loading a candy from one station doesn't affect the train's movement or its ability to load from other stations. It only adds a candy to the train which will be delivered when the train passes its destination. So the only effect is on the time when each candy is delivered. But the maximum delivery time is determined by the last delivery among all candies. Our plan for each station uses a specific set of arrival times (the first cnt[i] arrivals). We can certainly load on those arrivals. There's no reason to delay loading beyond those arrivals because that would only make the delivery later (since we would have to wait for later arrivals, which are later in time). So to minimize the maximum, we should load as early as possible, i.e., on the first cnt[i] arrivals. However, one nuance: If we load a candy very early, it might get delivered early, which is fine. So indeed, the optimal strategy is to load each candy at the earliest possible opportunity, i.e., on the first arrival at its start station after it becomes available (which is from the beginning). Since all candies are initially at their start stations, the earliest possible pickup for a candy at station i is the first time the train visits i. So if we have cnt[i] candies, we need to pick them up on the first cnt[i] visits; any delay would push its delivery later, potentially increasing the maximum.
But is there a scenario where picking up a candy later could allow an earlier pickup for another station? No, because pickups are per station independent; picking up at a station does not affect the train's ability to pick up at other stations. So no conflict.
Thus, the minimal achievable maximum delivery time seems to be exactly the maximum over i of (dist(s,i) + (cnt[i]-1)*n + minDist[i])? But I recall in Codeforces solutions, they often use something like: For each station i, compute (cnt[i]-1)*n + dist(i, farthest? Actually let's check with sample.
Let's compute cnt[i]:
Station1: no candy start? a_i: none start at 1? Actually check: candies: (2,4), (5,1), (2,3), (3,4), (4,1), (5,3), (3,5). So starts: at 2: two candies (to 4 and 3). at 3: two (to4 and5). at 4: one (to1). at5: two (to1 and3). Station1: 0.
cnt[2]=2, cnt[3]=2, cnt[4]=1, cnt[5]=2.
Now compute minDist[i] (minimum clockwise distance from i to destination among its candies). Need to compute distances on circle length 5, clockwise. Distance from i to j: (j - i mod n) with mod 5 such that result between 1 and 4 (since j != i). For n=5:
For station2: candies to 4: dist= (4-2)=2; to 3: dist= (3-2)=1 (since 3>2, just 1). minDist[2]=1.
For station3: to4: dist=1; to5: dist=2. minDist[3]=1.
For station4: to1: dist= (1-4 mod5)= (1+5-4)=2. So minDist[4]=2.
Max = max(7,8,5,10)=10. So answer for s=1 would be 10. Sample output first number is 10. Good.
s=2:
dist(s,i):
dist(2,2)=0 (ignore), 2->3:1, 2->4:2, 2->5:3, 2->1: (1-2 mod5=4) Actually need dist to 1: from 2 to 1 clockwise: 2->3->4->5->1 = 4 steps? Indeed (1-2+5)=4. So dist(2,1)=4 but cnt[1]=0 ignore.
Now T_i:
i=2: cnt2: dist=0 => 0+5+1=6
i=3: cnt2: dist=1 => 1+5+1=7
i=4: cnt1: dist=2 => 2+0+2=4
i=5: cnt2: dist=3 => 3+5+1=9
Max = max(6,7,4,9)=9. Sample output second number is 9. Good.
Now sample 2:
n=2, m=3 candies: all (1,2), (1,2), (1,2). So cnt[1]=3, cnt[2]=0. Distances: from 1 to 2 clockwise: since n=2, distance from 1 to 2 =1? Actually 1->2 is 1 step. Counterclockwise would be also 1? But we only go clockwise. So dist(1,2)=1. minDist[1] = distance from 1 to 2 =1? But note that for the candy at station 1, its destination is 2, so distance = 1. So minDist[1]=1.
Now compute for s=1:
dist(s,1)=0 (since we start at 1). T_1 = dist(s,1) + (cnt[1]-1)*n + minDist[1] = 0 + (3-1)*2 + 1 = 0+4+1=5. Max =5. Output first is 5. Good.
For s=2:
dist(2,1): clockwise from 2 to 1: 2->1? Since 2 then next is 1 (because after n we wrap). So distance =1 (since 2->1 takes 1 step). Actually compute: (1-2+2)=1. So dist=1. Then T_1 = 1 + (3-1)*2 +1 = 1+4+1=6. Output second is 6. Good.
So the formula works for the samples.
Thus, the answer for each start s is max_{i=1..n, cnt[i]>0} ( (cnt[i]-1)*n + dist(s,i) + minDist[i] ), where dist(s,i) is clockwise distance from s to i.
But we need to be careful: dist(s,i) is defined as the smallest nonnegative number t such that after moving t steps from s, we reach i. Since we only move clockwise, it's (i - s mod n) where mod yields in range [0, n-1], but if i==s, dist=0. However, if i==s and cnt[i]>0, then we are already at i at time 0, so first arrival is at time 0. That matches.
Now, is there any scenario where the formula fails? Perhaps when there are multiple candies and we might be able to deliver some candies by loading them after the first visit? But we already accounted for that.
Consider scenario where some candy is at station i and its destination is i? But problem states a_i != b_i, so no.
Another nuance: The train can only load at most one candy per station before it leaves. That means if there are multiple candies at a station, we cannot load them all on the same visit. So indeed we need at least cnt[i] visits. But could we ever need more than cnt[i] visits? No, because we can load each on a separate visit, and we can use the first cnt[i] visits. That is sufficient.
But we must also consider that the train might not be able to pick up a candy on the first arrival if we have to load another candy? No, you can always load one if available. So yes.
Thus, the problem reduces to computing for each station i: cnt[i] and minDist[i] (the minimum clockwise distance from i to b among candies starting at i). Actually is it minDist or something else? Let's double-check the logic: The last delivery from station i occurs at time d + (cnt[i]-1)*n + dist_of_candy_picked_last. To minimize that, we want the smallest possible dist for the last picked candy. So we assign the candy with smallest travel distance to the last pickup. So indeed we use minDist[i].
But what if there is no candy at i? Then we ignore.
Thus, answer for start s = max_{i: cnt[i]>0} ( dist(s,i) + (cnt[i]-1)*n + minDist[i] ).
Now, we need to output for each s from 1 to n.
Given constraints n ≤ 100, m ≤ 200, we can compute this easily.
But we must verify that the formula always yields the minimal possible time, and there is no scenario where we can do better by not picking up some candies on the first arrival or by delaying some pickups to reduce the maximum? Possibly if we delay a pickup from a station, that would increase its own delivery time, but might decrease the max by allowing to reorder pickups across stations? However, the max is taken over all candies; delaying any pickup can only increase the delivery time of that candy (or keep same) and cannot reduce any other candy's delivery time (since movement is independent). So it cannot improve the max. Unless there is some interaction: if we delay picking up a candy from station i, then maybe we can pick up a different candy from another station earlier? But the train's arrival times are fixed; at each station, we can load at most one candy regardless of what else we do. Loading decisions at different stations are independent. Delaying loading at i does not affect ability to load at j; you can always load at j on its arrival times regardless. So no synergy.
But there is a subtlety: The train can unload candies at destinations. Could loading later from i affect the time when a candy from j gets delivered because the train might be carrying more candies? No, unloading is instantaneous and independent. The train's movement is unaffected by cargo. So no.
Thus, the formula is correct.
But wait: Is there any scenario where we might want to skip loading a candy on a particular visit to i, because we want to use that visit to load a candy from a different station? That doesn't make sense because visits to different stations are at different times. The train arrives at i at a certain time; at that moment, if there are candies at i, we can choose to load or not. If we don't load, we lose that opportunity, and we will need to load later. So it's never beneficial.
Thus, the optimal is to load on the first cnt[i] visits.
Now, the only thing to ensure is that we have enough time to also deliver the candies after pickup; but that's accounted.
Thus, the answer is as above.
But is there any scenario where the minimum distance from i to destination might be zero? No, because a_i != b_i, so distance is at least 1. So minDist[i] ≥ 1.
Now, we must compute dist(s,i) correctly. For each pair (s,i), we can compute as (i - s + n) % n, but careful: if i == s, distance is 0, but we need to treat that as the first arrival at time 0 (the train is already at station s at time 0). So yes.
Implementation: Input n,m. Initialize cnt[1..n]=0, and for each candy, read a,b. Increment cnt[a]. For each station, we need the minimum clockwise distance from a to b among its candies. So for each candy, compute distance d = (b - a + n) % n. But since a != b, distance is between 1 and n-1. For each station a, maintain minDist[a] as the minimum of these distances across candies starting at a. Initialize minDist[a] = INF (maybe n+1). Then for each candy, update minDist[a] = min(minDist[a], d).
After processing all candies, for each start station s from 1 to n, compute ans[s] = max over i with cnt[i] > 0 of ( (cnt[i]-1)*n + ((i - s + n) % n) + minDist[i] ). But careful: The term (i - s + n) % n yields 0 when i == s, which is correct: first arrival at s is at time 0. However, we need to consider if we start at s and there is a candy at s, we can load it immediately at time 0. Yes.
But double-check: When we start at station s, at time 0, we are at station s. According to process, we can load at most one candy before leaving. So we can load a candy from s at time 0. That's consistent with first visit (arrival) at time 0. So dist(s,s)=0.
Now, but we need to verify that this holds even if there are multiple candies at the start station. For s=1 in sample1, cnt[1]=0, so fine. For s=2 in sample1, cnt[2]=2, minDist[2]=1, dist(2,2)=0 so T_2 = 0+5+1=6. However, we have two candies at station2, distances 2 and 1. The optimal schedule: first visit at time 0, load one candy (which one?). We want to load the candy with larger distance first? Actually to minimize max, we want to load the larger distance on first visit and smaller on last. Since we have cnt=2, we have two visits: at times 0 and 5 (since n=5). At time 0, we load the candy with distance 2 (to station4). Then the train moves, it will deliver that candy at time 0+2=2 (when it reaches station4). Then later at time 5, we are back at station2, load the candy with distance1 (to station3) and deliver it at time 5+1=6. Meanwhile, other candies are also delivered. The max is 6? But sample answer for s=2 is 9, not 6. Wait T_2=6, but the overall max was 9 from station5. So from station2, we computed T for station5 gave 9. That's the bottleneck. So indeed the max is 9, not 6. So our schedule is consistent.
Now, check the correctness of the T_i formula for i=2 when s=2: The two candies from station2: one with distance 2, one with distance 1. With our assignment (largest on first visit, smallest on last), we get delivery times: first candy at 0+2=2, second at 5+1=6. The last delivery from station2 is 6. However, we computed T_2 = 0 + (2-1)*5 + minDist=0+5+1=6, matches. Good.
Now, consider a scenario where minDist[i] might be not the smallest distance? Could it be that we don't need to pick up all candies from i? No, we must deliver all.
Thus, the answer is straightforward.
But wait, there might be a nuance: The train can unload candies at destinations as soon as it arrives. If we pick up a candy at station i at time t, and its destination is j, then if j is reached after traveling dist(i,j) steps. However, what if the train passes j before picking up the candy? Since we only pick up at i, and then move forward, it's impossible to pass j before picking up if j is between i and i+dist(i,j) inclusive? Actually, since we pick up at i, we start from i. The train then moves to i+1, i+2, ... and after dist(i,j) steps, it arrives at j. That is the first time after picking up that we reach j (assuming we don't loop because dist(i,j) < n). So it's fine.
Now, confirm with an edge case: n=2, and candy from 1 to 2. Starting from 2. dist(2,1)=1, cnt[1]=1, minDist[1]=1. T_1 = 1 + (1-1)*2 + 1 = 2. But is that the time needed? Starting at 2 at time 0, we move to 1 (1 sec), arrive at 1 at time 1, load candy, then move to 2 (1 sec), arrive at 2 at time 2, deliver. So total 2 seconds. That matches.
But what if there are multiple candies at station1? For n=2, cnt[1]=2, minDist[1]=1 (since both go to 2). Starting at 1: T_1 = 0 + (2-1)*2 + 1 = 3. Let's simulate: Time0 at 1, load candy1, move to 2 (1 sec) arrive at2 time1, unload candy1, move to1 (1 sec) arrive at2? Actually after 1->2, we can unload, then we continue? At time1 at station2, we unload candy1, then we have to load? There's no candy at 2 (maybe). Then we move to1: time2 arrive at1, load candy2, then move to2: time3 arrive at2, unload candy2. Total time 3. So matches.
But is there any scenario where we can deliver the second candy earlier by loading both at the same time? Not allowed: only one per station per visit.
Thus, formula works.
Now, we need to output for each station 1..n the answer.
But we should also consider that the train may have to do extra empty laps after all pickups to deliver the last candy? That's already accounted: The last delivery time is computed as above, which may include extra laps.
One more nuance: The train can unload candies at any pass through the destination. So if we pick up a candy on the last visit to its start station, it may be delivered after traveling its distance; but that time may be after we have already visited other stations. That's fine.
Thus, the answer is as described.
Now, confirm with another custom test: Suppose n=3, m=2: candy1: 1->2, candy2: 1->3. So cnt[1]=2, minDist[1] = min(dist(1,2)=1, dist(1,3)=2) = 1. Starting from s=1: T_1 = 0 + (2-1)*3 + 1 = 4. Let's simulate: time0 at1, load candy1 (dist1) or candy2? To minimize max, load farthest first? Let's see: distances: candy2 (to3) distance2, candy1 distance1. Strategy: load candy2 first at time0, then move: time1 at2 (nothing), time2 at3, deliver candy2 (so delivered at time2). Then continue: time3 at1, load candy1, move: time4 at2, deliver candy1. So final time 4. If we loaded candy1 first, then candy1 delivered at time1, candy2 picked at time3, delivered at time5, so max=5. So optimal is 4. Our formula gave 4. Good.
Starting from s=2: dist(2,1) = (1-2+3)=2? Actually 2->? clockwise: 2->3->1, so distance 2. T_1 = 2 + (2-1)*3 + 1 = 6. Simulate: Start at2 time0. Need to go to 1 to load. Move: time1 at3, time2 at1 (arrive). Load candy2 (farthest) first? At time2 at1, load candy2 (to3). Then move: time3 at2, time4 at3, deliver candy2 (time4). Then continue: time5 at1, load candy1, time6 at2, deliver candy1. Total 6. So matches.
Now, if there are candies from multiple stations, the max might be from a station with larger cnt or minDist. So formula seems correct.
Thus, solution: For each station i, compute cnt[i] and minDist[i] (if cnt[i]>0). Then for each s compute max over i ( (cnt[i]-1)*n + dist(s,i) + minDist[i] ). Output.
But we should be cautious: The distance dist(s,i) is the time to first arrival at i from start s. However, if we start at s and there is a candy at s, we can load immediately at time0. That's correct. So dist(s,i) = (i - s + n) % n. That's a number between 0 and n-1 inclusive.
Now, what about stations with cnt[i]=0? They don't contribute to max, but if all stations have cnt[i]=0? But m≥1, so at least one station has candies. So max over non-empty is defined.
Now, let's verify if there is any hidden nuance about loading only one candy per station before the train leaves. The train is at a station, we can load one candy. But we can also unload any number. So no further constraints.
Now, consider that the train might need to do some extra laps after the last pickup to deliver the last candy; the formula includes that via minDist.
Thus, answer is straightforward.
But I recall that in the original Codeforces problem "Toy Train", there is a twist: The answer is max_{i} ( (cnt[i]-1)*n + dist(s,i) + something ), but the something might be the "minimum distance to destination among candies starting at i" OR the "maximum distance"? Actually, I've seen solutions where they compute for each station i the maximum of distances? Let's double-check: In some CF editorial, they said: For each station i, compute the minimal time to deliver all candies from that station if we start at i (or something). Actually, I think they compute: For each station i, let t[i] = (cnt[i]-1)*n + minDist[i]. Then answer for start s = max_i ( (s->i distance) + t[i] ). This matches.
But I recall there is a version where we need to compute the maximum of (cnt[i]-1)*n + dist(s,i) + maxDist? No. Let's think: Could there be a case where assigning the smallest distance to last pickup is not optimal for minimizing the max? Let's test with a scenario: Suppose station i has cnt=3, distances: 1, 100? But distance cannot exceed n-1, so max is limited by n. So n is like the cycle length. For n=100, distances are at most 99. So (cnt[i]-1)*n is multiples of n. Since n is relatively large compared to distances, the multiple of n dominates. So the last visit (largest multiple) will have the largest contribution regardless of distance, as long as distances are less than n. Indeed, if (cnt[i]-1)*n is large, adding a small distance won't exceed the next multiple minus something. Actually, consider cnt=2, n=5, distances: 4 and 5? distance can't be 5 because max is 4 for n=5. So maximum distance is 4. Then T_last = d + n + dist_small. T_prev = d + dist_large. Since n > dist_large (max 4) then T_last > T_prev. So last is always larger. For cnt=3, T_last = d + 2n + dist_small, T_mid = d + n + dist_mid, T_first = d + dist_large. Since 2n > n+dist_mid? For n=5, 2n=10, n+dist_mid ≤ 5+4=9, so T_last > T_mid. So yes, last is largest.
Thus, the maximum for station i is determined by the last pickup, with the smallest distance. So minDist is correct.
Thus, answer formula stands.
Now, we need to implement.
But before finalizing, let's test with some tricky cases.
Case: n=3, m=2: candies: (1,2) and (2,1). Then cnt[1]=1, minDist[1]=dist(1,2)=? (2-1)=1. cnt[2]=1, minDist[2]=dist(2,1)= (1-2+3)=2. For start s=1: compute T1: (1-1)*3? cnt[1]-1=0 so 0 + dist(1,1)=0 + minDist[1]=1 =>1. T2: (1-1)*3=0 + dist(1,2)=1 + minDist[2]=2 =>3. Max=3. Simulate: Start at1: time0, load candy1 (1->2). Then move to2 (1sec), arrive at2 time1, unload candy1 (delivered). Also at station2 at time1, we can load candy2 (2->1). Then move to3 (time2), then to1 (time3), unload candy2. Total time=3. So correct.
Start s=2: T1: cnt1=1: dist(2,1)= (1-2+3)=2 + 0 +1 =3? Actually T1 = dist(2,1)=2 + 0 + minDist[1]=1 =>3. T2: dist(2,2)=0 + 0 + minDist[2]=2 =>2. Max=3. Simulate: start at2: time0, load candy2? Actually candy2 is at2? Yes, at station2 there is candy to1. So load it at time0. Then move to3 (1sec), arrive at3 time1, then to1 time2, unload candy2. Meanwhile candy1 is at station1. At time2 we are at1, load candy1, then move to2 time3, deliver candy1. Total time=3. So matches.
Start s=3: dist(3,1)= (1-3+3)=1? Actually 3->1: steps: 3->1? Since after 3 next is 1? Yes, distance 1. So T1: dist=1 +0+1=2. T2: dist(3,2)= (2-3+3)=2? Actually 3->2: 3->1->2 =2 steps, so dist=2+0+2=4. Max=4. Simulate: start at3: time0, no candy at3. Move to1 (1sec) time1, load candy1 (1->2). Then move to2 (1sec) time2, unload candy1. Also at time2 at2, load candy2 (2->1). Then move to3 (1sec) time3, then to1 (1sec) time4, unload candy2. Total time=4. So matches.
Thus formula holds.
Now, we must also consider that if a station has cnt[i] candies, the minDist[i] may not be defined if cnt[i]=0; we skip.
Now, note: The formula uses (cnt[i]-1)*n, which for cnt[i]=0 would be negative; but we only consider cnt[i]>0.
Now, implementation details: We'll loop for s from 1 to n. For each s, compute ans = 0; for each i from 1 to n if cnt[i] > 0: compute d = (i - s + n) % n; value = (cnt[i] - 1) * n + d + minDist[i]; ans = max(ans, value). Then output ans.
But careful: For stations with cnt[i] > 0, minDist[i] is defined.
Now, edge case: Could there be a scenario where the maximum is not achieved at a station with cnt>0? No.
Now, complexity: O(n^2) which is fine (n <= 100).
Now, need to ensure we correctly compute minDist: For each station, among its candies, we need the minimum clockwise distance from that station to the candy's destination. So for each candy, compute dist = (b - a + n) % n. But if b == a, not possible, but if it were, distance 0, but not allowed. So distance >=1. However, mod operation yields 0 if b == a, but we won't have that. So fine.
Now, what about the possibility of using the same arrival to both load and unload? At a station, we can both unload and load (order doesn't matter) instantly. In our model, we assume we can unload any candies whose destination is the station before we load. But could that affect the ability to load? No. Also, unloading may happen at same time as loading; but in terms of time, we consider delivery happening at the arrival time. So if we pick up a candy at station i and its destination is also i? Not allowed. So no.
Now, one more nuance: At the very start (time 0), we are at start station s. We can load a candy if available. Also, if any candy's destination is s? Possibly some candy has b_i = s, but that candy is initially at a_i, not s. So we don't have any candy to unload at start unless we have picked it up earlier; none initially on train.
Thus, fine.
Now, this seems solved.
But is there any scenario where we can deliver some candies without picking them up? No.
Now, we should also consider if the formula might overcount because we might be able to load and deliver in the same lap more efficiently by picking up a candy and delivering it later in the same lap before coming back to the start station? That's already accounted in the distance addition.
Now, let's think about potential pitfalls: For stations with cnt[i] > 0, we assume we always can use the first cnt[i] arrivals. But what if on some of those arrivals, we are also at that station after having just loaded a candy from that same station on a previous visit? That's fine. The train's schedule is fixed.
But consider the following: The train starts at s. Suppose there is a station i with cnt[i] = 1. The first arrival at i is at time d. We load the candy. Then the train moves forward; we might pass through i again after n steps. But we don't need to load again. So fine.
Now, could there be an issue if the train has to deliver a candy whose destination is the same as the station where we will later load another candy, and we might want to unload before loading? That doesn't affect time.
Thus, all good.
Now, I think the solution is correct.
But let's check with another custom test: n=4, m=3: candies: (1,2), (1,3), (2,4). So cnt[1]=2, minDist[1] = min(dist(1,2)=1, dist(1,3)=2) =1. cnt[2]=1, minDist[2]=dist(2,4)=2. For start s=1: T1: (2-1)*4=4 + dist(1,1)=0 + minDist1=1 =>5. T2: (1-1)*4=0 + dist(1,2)=1 + minDist2=2 =>3. Max=5. Simulate: start at1 time0: load candy to3 (farthest). Move: time1 at2: maybe unload? none; but there is candy at2 still to load? Actually we have candy at2 to4; we could load it at time1? But we can only load one per station; we can load at2 at time1 if we want. But our schedule says for station2, we have cnt=1, we will load it on first arrival which is at time1. So we could load candy2 at time1. But we need to also load candy1 to2 at some point? Actually we have two candies at1: one to2 and one to3. At time0 we loaded the one to3. At time1 at2 we load candy to4. Then continue: time2 at3, unload candy to3 (delivered). time3 at4, unload candy to4 (delivered). Now we still have candy to2 from station1. We need to load it. The next time we are at1 is at time4 (since full lap: after time3 at4, move to1: time4). At time4 at1, load candy to2, then move to2: time5, deliver. So total time =5. That's exactly the max. Could we do better? If we loaded the candy to2 first at time0, then at time1 at2 we could deliver it immediately? Actually if we load candy to2 at time0, then at time1 at2, unload it. But then we still have candy to3 to load. But we haven't loaded candy to3 yet. At time1 we are at2, we could also load candy to4 (since it's at2). But we can only load one at station2 per visit, so we can load candy to4 at time1. Then at time2 at3, nothing to unload (candy to3 is not loaded). Then at time3 at4, unload candy to4. Then at time4 at1, load candy to3, then time5 at2, time6 at3 deliver. So total time=6, worse. So optimal is 5. So our formula works.
Now, check start s=2: dist(2,1)= (1-2+4)=3, T1 = (2-1)*4=4 + 3 +1=8; T2 = (1-1)*4=0 + dist(2,2)=0 +2=2; max=8. Simulate: start at2 time0: maybe load candy to4? Actually station2 has candy to4, load it at time0. Then move to3 (time1), to4 (time2) unload candy to4. Then continue to1 (time3), to2 (time4), to3 (time5), to4 (time6), to1 (time7), to2 (time8)? That seems long. Actually we also need to pick up two candies from station1. Let's plan optimally: Start at2, we want to minimize time. At time0 at2, we have one candy at2; we load it. Then move to3 (1), to4 (2) unload. Now we are at4, need to go to1 to pick up candies. Continue: to1 (3), at time3 we are at1. At station1, there are two candies. We need two visits to 1. First visit at time3, load one (say to3, the farther). Then move to2 (4), to3 (5) unload candy to3. Then move to4 (6), to1 (7) second visit to1, load candy to2, then move to2 (8) unload. So total time=8. Could we do better by not loading at2 at time0? If we skip loading at2 at time0, we could go directly to1 and start picking up candies earlier? Let's try: start at2, time0, don't load. Move to3 (1), to4 (2), to1 (3). At time3 at1, load candy to3. Move to2 (4), to3 (5) unload. Then at time5 at3, continue to4 (6), to1 (7) second visit to1, load candy to2. Move to2 (8) unload. Then we still have candy at2 (to4) not loaded yet. We need to load it. When can we load it? Next visit to2 after time8? At time8 we are at2, we could load candy to4 at that same time? At time8, we arrived at2 and unloaded candy to2. At that moment, we are at station2; we can also load the candy to4. But careful: At time8, we have just arrived at2; we can unload and also load one candy. So we can load candy to4 at time8. Then we need to deliver it. After loading, we move to3 (9), to4 (10) deliver. So total time would be 10. Worse. If we loaded candy to4 at time0, we delivered it early at time2, so that seems better. So our schedule with loading at time0 yields total 8, consistent with formula. Could there be a schedule that gives less than 8? Maybe if we load candy to4 later but still deliver before the final? But if we load later, delivery later. The bottleneck appears to be the two candies at station1 requiring two visits to1. The first visit to1 occurs at time3 (since start at2, distance 3). The second visit to1 occurs n=4 seconds later at time7. Then the candy loaded at second visit (to2) gets delivered at time7+1=8. So total 8. So formula yields 8.
Thus consistent.
Now, we should consider if the formula can be simplified: For each i, precompute base_i = (cnt[i]-1)*n + minDist[i]. Then answer for s = max_i (base_i + dist(s,i)). So we can compute base_i for each i (with cnt[i]>0). Then for each s, we need to compute maximum over i of (base_i + dist(s,i)). Since dist(s,i) = (i - s + n) % n. That's like adding a function that depends on s and i. We can compute in O(n^2) by brute force.
Given n<=100, O(n^2)=10^4, trivial.
Now, is there any scenario where we might need to consider that the train may not need to wait for full laps if we can deliver last candy earlier by not doing a full lap? The formula already accounts for that: The number of laps between first and last visit to i is cnt[i]-1. Since each full lap takes n seconds, that is the minimum possible, because after first visit, to come back to i, you must travel around the whole circle. Could you come back to i in less than n seconds by going the other way? No, train only moves clockwise, so it must go through all n stations to return to i. So that's fixed.
Thus, formula stands.
Now, double-check constraints: n up to 100, m up to 200, so fine.
Now, let's code. We'll read n,m. Initialize cnt = [0]* (n+1), minDist = [inf]* (n+1). For each candy: a,b; cnt[a]++ ; compute d = (b - a + n) % n; if d ==0? but a!=b, so d between 1 and n-1. Update minDist[a] = min(minDist[a], d). After reading all, for i from 1 to n, if cnt[i]==0 we can ignore minDist (but minDist stays inf). Then for s in 1..n: ans = 0; for i in 1..n: if cnt[i] > 0: d = (i - s) % n; but careful in Python: (i - s) % n yields 0..n-1. However, we need to compute modulo that yields the clockwise distance. Since we are enumerating stations 1..n, moving clockwise from s to i: if i >= s, distance = i - s; else distance = i - s + n. So (i - s + n) % n works. So compute dist = (i - s) % n? In Python, negative modulo gives positive, so (i - s) % n indeed gives (i - s) mod n, which yields 0 for i==s, and for i<s yields i-s+n. That's correct because we want the number of steps to go from s to i moving forward. Example: n=5, s=4, i=2: (2-4)%5 = (-2)%5 = 3, which is correct (4->5(1),5->1(2),1->2(3) => 3). So (i - s) % n works.
Then val = (cnt[i] - 1) * n + dist + minDist[i]; ans = max(ans, val). Print ans.
But we must ensure that for cnt[i]==0 we skip; also note that minDist[i] is defined only when cnt[i]>0; we set it to some large initially but we skip anyway.
Now, test with sample.
Now, is there any scenario where the answer can be less than the formula due to clever ordering across stations? Suppose we have candies at station i and station j, and the distances from s to i and s to j differ. The max is the maximum of their individual completion times. Could we sometimes reduce the max by delaying a pickup at i to allow an earlier pickup at j? No, because pickups at j are independent; you can always pick up at j on its first arrival regardless of what you do at i. So the completion time for station j is determined solely by its own schedule and s, not by what we do at i. So max is indeed the max over i.
Thus, answer is max_i T_i(s).
Now, we should consider if there is any interdependence: The train might be carrying many candies; but could that cause a delay at a station because unloading takes time? No, unloading is instantaneous.
Thus, solution is correct.
Now, we produce final code.
But wait, in some solutions, they compute not minDist but something like "the minimum time to deliver all candies from that station if starting at that station" which is (cnt[i]-1)*n + minDist[i]. Then answer for s is max_i ( (cnt[i]-1)*n + minDist[i] + dist(s,i) ). So same.
Now, ensure we handle the case where there are no candies at all? But m>=1, so not needed.
Now, output: Print n space-separated integers.
Now, note: The answer could be up to? n max 100, cnt[i] max maybe m up to 200, so (cnt[i]-1)n could be up to 199100 = 19900. minDist up to 99, dist up to 99, so answer up to about 20000. So fits in int.
Now, we will implement.
But before finalizing, let's think about a possible nuance: Could we ever need to wait at a station? The train always moves; we cannot "wait". But we could decide not to load a candy, but that doesn't make the train wait; the train leaves immediately. So we always move each second. So the schedule of arrival times is fixed.
Now, could we ever need to load a candy and then unload it at the same station in the same stop? Not possible because a_i != b_i.
Now, could we ever need to unload a candy before loading another at the same station? Doesn't affect.
Thus, all good.
Now, implement solve().
Edge Cases:
n=2, with many candies at station1 and station2. Works.
All candies at same station. Then answer for start s: T = dist(s, that station) + (cnt-1)*n + minDist. That's consistent.
Now, we should also consider that minDist may be computed incorrectly if we don't handle mod properly when b < a. We'll compute (b - a + n) % n. But if b > a, (b - a) is positive less than n; (b - a + n) % n = (b-a) because <n; if b < a, (b-a+n) % n = (b-a+n) which is between 1 and n-1. So correct.
Now, confirm with n=100: (b - a + 100) % 100 yields correct distance.
Now, we might also want to precompute base_i = (cnt[i]-1)*n + minDist[i] for each i with cnt>0.
Then for each s, compute max over i of (base_i + ((i - s) % n)). This is O(n^2). Good.
Now, let's check with an example where minDist is not unique: fine.
Thus, code.
But wait, there is a subtlety: The train can load at most one candy per station before it leaves. However, consider the following: When we arrive at a station, we can both unload and load. Suppose there is a candy to be delivered at that same station (from some previous pickup). We unload it. Then we load a candy from that station if any. That's allowed. So no issue.
Now, confirm that the first arrival at station i after start s is indeed at time dist(s,i). That is correct if we start moving immediately. At time 0, we are at s. We can choose to load one candy (if any) and then we move to next station after 1 second. So the time to reach i is the number of steps taken. So indeed, first arrival at i is at time = number of stations passed? Starting at s, after t steps we are at station (s-1 + t) % n + 1. So to be at i, we need t such that (s-1 + t) % n = i-1 => t ≡ (i-1) - (s-1) mod n => t ≡ i-s mod n. The smallest non-negative t is (i-s mod n). So dist(s,i) as defined.
Now, are we allowed to load a candy at time 0? Yes.
Now, is there any scenario where we could load a candy at the start and then immediately unload it if its destination is the start? Not possible.
Thus, solution correct.
Now, we should produce answer.
But I recall that in some variants of Toy Train problem (maybe with multiple tracks), the answer might involve considering also the farthest distance among candies from a station. But I'm convinced it's minDist.
Let's double-check with a scenario where the smallest distance is very small, but the largest distance is huge (but less than n). As argued, the last visit adds n, which is bigger than any distance difference. So the max is last visit + smallest distance. However, what if cnt[i]=1? Then (cnt[i]-1)*n = 0, so the only visit is the first. Then the delivery time for that candy is dist(s,i) + distance. To minimize max across all stations, we have no choice. So for cnt=1, we use the distance of that candy (which is its own distance). But if there are multiple candies at a station with cnt=1? That can't happen; cnt=1 means only one candy, so its distance is both min and max. So we use that distance.
So formula works.
Now, let's test a scenario with n=5, station i has cnt=3, distances: 4, 3, 2. minDist=2. Then T_i = d + (3-1)*5 + 2 = d+10+2 = d+12. If we assigned differently, worst-case might be larger. So minDist correct.
Thus, solution.
Now, we can produce final answer.
Implementation in Python:
def solve():
import sys
input = sys.stdin.readline
n, m = map(int, input().split())
cnt = [0](n+1)
minDist = [n+1](n+1) # large
for _ in range(m):
a,b = map(int, input().split())
cnt[a] += 1
d = (b - a) % n
if d == 0: # should not happen but if a==b, skip? problem says a_i != b_i, so ignore.
d = n # but not needed.
else:
# d already in 1..n-1
if d < minDist[a]:
minDist[a] = d
# for stations with cnt=0, we will ignore.
# Precompute base
base = [0]*(n+1)
for i in range(1, n+1):
if cnt[i] > 0:
base[i] = (cnt[i]-1)*n + minDist[i]
else:
base[i] = -1 # mark ignore
ans = []
for s in range(1, n+1):
best = 0
for i in range(1, n+1):
if cnt[i] > 0:
dist = (i - s) % n
val = base[i] + dist
if val > best:
best = val
ans.append(str(best))
print(' '.join(ans))
But note: In computing d = (b - a) % n, for a=2, b=1, n=5: (1-2)%5 = (-1)%5 = 4, which is the clockwise distance? Wait, from station 2 to 1 clockwise: 2->3->4->5->1 = 4 steps. So yes, that's correct. So we can compute distance as (b - a) % n, which yields number in 0..n-1. Since a!=b, distance >0? Not always: If a=1,b=2, (2-1)%5=1 >0. If a=2,b=1, (1-2)%5=4 >0. So distance is non-zero. So we don't need to treat d==0. So we can just compute d = (b - a) % n; it will be between 1 and n-1. So update minDist[a] = min(minDist[a], d).
But careful: If we compute d = (b - a) % n, for n=2, a=1,b=2 -> (2-1)%2=1; a=2,b=1 -> (1-2)%2 = (-1)%2 = 1. So correct.
Thus, code works.
Now, about base[i] for cnt[i] > 0: (cnt[i]-1)*n + minDist[i]. Since minDist[i] is defined.
Now, test with sample1 manually with code.
Now, one more test: n=3, m=1, candy (1,2). cnt[1]=1, minDist[1]=1. For s=1: base=0+1=1, dist(1,1)=0 => val=1. ans=1. But is that correct? Starting at 1, time0 load candy, move to2 (1 sec), deliver. So time=1. But wait, we need to deliver the candy; after delivering, we might consider task complete as soon as delivered. So yes, total time is 1. So answer for start 1 is 1. For s=2: base=1, dist(2,1)= (1-2)%3=2? Actually (1-2)%3 = 2 (since -1 mod3 =2). So val=1+2=3. That means from start 2, we need 3 seconds? Let's simulate: start at2, move to3 (1 sec), to1 (2 sec), load candy, move to2 (3 sec), deliver. So yes, 3 seconds. So answer (for stations: s=1 ->1, s=2->3, s=3->? dist(3,1)= (1-3)%3=1? -2 mod3=1 => val=1+1=2. Simulate: start at3, move to1 (1 sec), load, move to2 (2 sec), deliver. So 2. So outputs: 1 3 2. That seems plausible.
But is it consistent with the requirement that the train may need to continue moving until all candies delivered? Yes.
Now, is there any nuance about when the train "finishes"? The problem says: "the minimum time the train would need to deliver all the candies were it to start from there." So as soon as the last candy is delivered, we can stop counting. The train doesn't need to return to start. So our computed delivery times are fine.
Thus, final answer.
But recall the sample outputs: they have numbers like 10 9 10 10 9. So that's consistent.
Now, let's also test edge where multiple candies have same start and dest? For example, n=5, m=2, both (1,2). cnt[1]=2, minDist[1]=1. For s=1: base=(2-1)*5+1=5+1=6, dist=0, val=6. So ans=6. Simulate: time0 load candy1; then move: time1 at2 deliver candy1; time2 at3; time3 at4; time4 at5; time5 at1, load candy2; time6 at2 deliver candy2. So total 6. Works.
Now, check if we could load both at time0? Not allowed. So correct.
Now, check if we might deliver second candy earlier by loading it at time0 and delivering at time1? But we can only load one at time0. So no.
Thus, fine.
Now, one more nuance: The train might be able to deliver a candy that was picked up earlier at the same time as it loads another candy. That's fine.
Now, think about the maximum possible answer: n=100, m=200, all candies at same station, say station 1, with minDist = 1 (to station2). cnt=200. Then base = (199)*100+1=19900+1=19901. For start s=1, dist=0 => ans=19901. For start s=50, dist = (1-50)%100 = ( -49 mod 100)=51? Actually (1-50)%100 = 51? Let's compute: 1-50 = -49, mod100 = 51? -49+100=51. So ans = 19901+51=19952. So within int.
Now, final step: Write code with solve() and main call.
Now, confirm that we are using Python's modulo with negative numbers correctly: (i - s) % n gives non-negative remainder. For i=1, s=50, n=100: (1-50) = -49, -49 % 100 = 51 (since 100-49=51). That's the clockwise distance: from 50 to 1: 50->51->...->100->1: that is 51 steps? Actually 50 to 1: 50->51 (1), 51->52 (2), ... up to 100 (51? Let's count: from 50 to 100 inclusive steps: 50->51 is step1, ... 99->100 is step50? Hmm need to compute: Number of steps from station x to station y clockwise if y < x: steps = n - (x - y). For x=50, y=1: steps = 100 - (50-1)=100-49=51. Yes. So correct.
Thus, all good.
Now, deliver final answer.
But wait, there is an alternative perspective: In some editorial, they compute for each station i a value t_i = (cnt[i]-1)*n + w_i, where w_i is the minimum distance among candies from i to their destination. Then answer for start s is max_i (t_i + dist(s,i)). That is exactly our approach.
Thus, finalize.
We can also possibly precompute for each s the maximum more efficiently using convolution but not needed.
Now, code.
Potential pitfalls:
Initialize minDist properly. Since distances are at most n-1, we can set INF = n+5 or 10**9.
When cnt[i]==0, we skip; but ensure we don't accidentally use minDist[i] which might be INF.
Now, test with the second sample using code: n=2,m=3 all (1,2). cnt[1]=3, minDist[1] = (2-1)%2=1. For s=1: base=(3-1)*2+1=4+1=5, dist=0 ->5. s=2: base=5, dist=(1-2)%2 = (-1)%2=1 ->6. Output "5 6". Good.
Now, consider if there is a station with cnt[i] > 0 but minDist[i] not updated because we set INF? We update for each candy. So ok.
Now, let's run a quick mental test: n=3, m=2, (1,2),(2,3). cnt[1]=1, minDist[1]=1; cnt[2]=1, minDist[2] = (3-2)%3=1. For s=1: T1=0+0+1=1; T2=dist(1,2)=1 +0+1=2 => max=2. Simulate: start1: time0 load candy1? Actually there is also candy2 at2. We can do: time0 load candy1 (1->2). Move to2 (time1): unload candy1; also load candy2 (2->3). Move to3 (time2): unload candy2. So total 2. Good. For s=2: T1=dist(2,1)= (1-2)%3=2 +0+1=3; T2=0+0+1=1 => max=3. Sim: start2: time0 load candy2? Actually candy2 is at2, load it. Move to3 (time1) unload candy2. Then move to1 (time2) load candy1, move to2 (time3) unload candy1. So 3. Good.
Thus, correct.
Now, produce final answer.
But wait, I recall there is a twist: The problem statement says: "only at most one candy can be loaded from a station onto the train before it leaves the station." It doesn't say we cannot load and then immediately unload if the destination is same? Not relevant.
Thus, final solution.
Now, let's think if there might be a case where the optimal schedule involves not loading the candy with smallest distance on the last visit, but rather loading a candy with larger distance on the last visit, to allow an earlier delivery of some other candy from a different station? No, because loading decisions at one station do not affect deliveries from another station. The only thing that could affect others is if the train is carrying many candies? No, they don't interfere. So no.
Thus, final.
Now, I'll write the final answer with explanation.
But before final output, let's check that the problem statement says: "You can choose any candy at this station." So we can pick which one to load.
Now, code.
Potential edge: if cnt[i] is large, (cnt[i]-1)*n may overflow 32-bit? In Python it's fine.
Now, we will implement solve() accordingly.
Now, final answer.
I'll produce a well-commented code.
But also, note that hacks are disabled for this version, but we should be correct.
Now, write final message. ```python
import sys
def solve() -> None:
it = iter(sys.stdin.read().strip().split())
try:
n = int(next(it))
m = int(next(it))
except StopIteration:
return
code
cnt = [0] * (n + 1)
min_dist = [n + 1] * (n + 1) # distances are in [1, n-1]
for _ in range(m):
a = int(next(it))
b = int(next(it))
cnt[a] += 1
d = (b - a) % n # clockwise distance from a to b (1..n-1)
if d < min_dist[a]:
min_dist[a] = d
# pre‑compute the fixed part for each station that has candies
base = [0] * (n + 1)
for i in range(1, n + 1):
if cnt[i] > 0:
base[i] = (cnt[i] - 1) * n + min_dist[i]
else:
base[i] = -1 # will be ignored
ans = []
for s in range(1, n + 1):
best = 0
for i in range(1, n + 1):
if cnt[i] == 0:
continue
dist = (i - s) % n # time of first arrival at i
cand = base[i] + dist
if cand > best:
best = cand
ans.append(str(best))
print(' '.join(ans))