We need to analyze this problem. We have an operation: take the k-th element (1-indexed?) of the current sequence and append that same number to the end, then delete the first element. So effectively, we shift left: we have a queue. Each operation, we read the element at position k (from front, 1-indexed), and push it to the back, while popping the front.
Given initial sequence of length n, we repeat. We ask: after how many operations will all numbers become equal? That is, the sequence becomes all the same number. And if never, output -1.
We need minimal number of operations required.
Constraints: n up to 1e5.
We need to compute efficiently.
Let's denote the sequence as a[1..n]. After each operation, the sequence length remains n. Operation: suppose we have sequence b[1..n] (current). Step 1: find b[k] and add same number to the end -> sequence becomes b[1..n] + b[k]. Then step 2: delete first element -> new sequence = b[2..n] appended with b[k]. So new sequence c where c[i] = b[i+1] for i=1..n-1, and c[n] = b[k].
Thus it's a left rotation but with the new last element being the old k-th element.
Alternatively, we can think of an index mapping. Let the current state be represented by the starting index in the original array? However note that the array changes over time because we are overwriting the tail with some element from earlier. So it's not simply rotating a fixed array, because the k-th element used might come from somewhere.
But we can simulate the process as a queue: we have a deque of length n. Each operation: pop front; then peek the element at position k-1 (since front removed?) Actually careful: if we pop front after pushing? The description: step1: find the number that goes k-th in the current sequence and add the same number to the end; step2: delete the first number. So we first append copy of k-th element (keeping the sequence length n+1 temporarily), then delete first, resulting in length n. Equivalent to: remove first, then insert at end the element that was at position k before deletion? But if we remove first, indices shift: originally indices 1..n. After removing first, indices become original 2..n (now length n-1). But we need to add the original k-th element to the end. But if we remove first before taking the element, the k-th might change. However the order of steps is as given: we first take k-th element from the current sequence (before deletion), then append it, then delete the first. So in effect, after operation, the new sequence is: original[2..n] then original[k]. So that's exactly.
Thus it's like: new[i] = old[i+1] for i=1..n-1, new[n] = old[k].
We can denote the state as an array of length n.
We need to know after how many steps the state consists of all equal numbers.
Observation: This process is deterministic. It's essentially a linear cellular automaton? Not exactly.
We can try to model the transformation as a function f on the sequence.
One way: Because we only care about when all numbers become equal, maybe we can derive conditions.
First, note that the process preserves the multiset? Let's check: At each step, we remove the first element, and add a copy of the k-th element. So the multiset can change because we replace one element (the removed one) with a copy of another. So unless the removed element equals the copied one, the counts change. So it's not a rotation.
We need to see if it's possible for all numbers to become same, and if so, minimal steps.
Simplify: Suppose we want all numbers to be x. For that to happen, at some step, the sequence is [x, x, ..., x]. Consider the transformation from a state to the next. If all are equal, then the operation will keep them equal because the k-th is x and after removing first and appending x, we still have all x's. So once all equal, it remains so.
Now, we need to find if we can reach an all-equal state.
Observation: The operation essentially moves a "window"? The new sequence after t operations can be expressed in terms of the initial sequence a and some indices? Let's try to derive the state after t steps.
Let the state after t steps be S_t, with S_0 = a. Operation: S_{t+1}[i] = S_t[i+1] for i=1..n-1, and S_{t+1}[n] = S_t[k].
So this is a linear recurrence on the sequence. We can think of each position i in S_t as some S_0[j] after some combination? Since it's a deterministic update, we can compute S_t[i] as a function of initial a's. However because the update uses S_t[k] for last element, it might be like a delay or shift.
Let's denote index mapping. Represent S_t as an array of length n. For t=0, S_0[i] = a_i. For t>=0:
S_{t+1}[i] = S_t[i+1] for 1 <= i <= n-1
S_{t+1}[n] = S_t[k]
This is reminiscent of a linear feedback shift register of length n, where the new last is the old k-th.
We can try to find an expression for S_t[i] in terms of a_j. For i and t, we can trace backwards: S_t[i] = ?
We could attempt to compute S_t[i] recursively:
- If i < n, then S_t[i] = S_{t-1}[i+1] = S_{t-2}[i+2] = ... until we either reach a step where the index exceeds n? Actually careful: When we apply the recurrence repeatedly, the shift effect: For i from 1 to n-1, each step reduces t and increases i until i+something exceeds n? Actually because the recurrence for positions not the last: S_t[i] = S_{t-1}[i+1] if i <= n-1. So if we iterate s steps, we get S_t[i] = S_{t-s}[i+s] as long as i+s <= n. Once i+s = n, then S_t[i] = S_{t-s}[n] but then further shifting? If we go beyond n, we need to handle the wrap because when i+s > n, the recurrence changes because the value would have come from the last position? But last position has a different recurrence: S_{t}[n] = S_{t-1}[k].
Thus it's a kind of shift with a feedback from position k. This is similar to a linear system where we treat the sequence as a queue and we push the element that was at position k at previous step.
Alternate perspective: Consider the process as a queue of length n. Initially queue: a1, a2, ..., an from front to back. Each operation: we look at the element at position k (1-indexed from front). Then we remove front, and add the copy of that k-th element to the back. So we can simulate queue operations efficiently? Since n up to 1e5, and number of steps might be large, but maybe we can find a bound.
We need the minimal t such that all elements in the queue are equal. Could be at most something like O(n)? Or could be large? Let's try to think.
If all numbers become equal, then obviously the initial multiset must contain at least one number that can "take over". The operation copies the k-th element and discards the first. So essentially, the element at position k is being "propagated" to the back, and it also stays in its position unless it becomes the front later.
We can think of it as: the process gradually replaces elements with copies of the element that appears at position k over time.
Maybe we can think in terms of a sliding window: Since we remove the front, after t steps, the front of the original queue (first t elements) are discarded. Meanwhile, new elements appended are copies of whatever was at position k at each step. So after t steps, the queue consists of: original elements from index t+1 to n (those that haven't been popped yet), plus t appended elements (each being the k-th element at the time of its generation). However careful: The order after t steps is: the elements that remain from original after popping t from front are original[t+1], original[t+2], ..., original[n]. Then we have the appended elements from step 1 to step t, but note that at each step, the appended element goes to the back, and later steps may pop some of those appended elements if they become front. So after many steps, some of the appended elements might also be popped.
Thus the composition after t steps is not simply original tail + new elements; it's like we have a queue that evolves.
But we can keep track of the queue as a double-ended queue and simulate? But t could be large, up to maybe 10^5? Possibly more? But maybe we can find that if it's possible to get all equal, the minimal steps is bounded by n? Let's test small examples.
Example 1: n=3, k=2, a=[3,1,1]. After 1 operation: pop 3, append copy of k=2 which is 1 -> queue becomes [1,1,1]. So t=1, which is less than n.
Example 2: n=3, k=1, a=[3,1,1]. Operation: k=1 -> copy first element (3), pop first (3) -> queue becomes [1,1,3]? Actually: initial: [3,1,1]; step1: k=1 is 3, append 3 -> [3,1,1,3]; delete first -> [1,1,3]. So after 1 step: [1,1,3] (two 1s, one 3). Step2: now sequence [1,1,3]; k=1 is 1, append 1 -> [1,1,3,1]; delete first -> [1,3,1]. So after 2: [1,3,1]. Step3: [1,3,1]; k=1=1 append 1 -> [1,3,1,1]; delete first -> [3,1,1]. That's back to original? Indeed after 3 steps we get original? Actually original was [3,1,1]; step3 gave [3,1,1] yes. So it cycles with period 3? So never all equal. So -1.
So minimal steps could be 0? If initial already all equal, answer 0. So we need to consider that.
Let's try to think of necessary condition for all numbers to eventually become equal. Since the operation copies the k-th element, the only number that can spread is the value at position k at each step. However the value at position k changes over time.
If at some point the k-th element is x, then we append x to the back. Meanwhile, we remove the front element. So if we want to eventually have all x, we need to have x be the only value left.
Thus, we need that there exists some x such that we can eliminate all other values. The process eliminates front elements one by one. So eventually, after enough steps, all original elements that are not x will be popped from front, provided they are not inserted again as copies of x? But copies are x, so they are x. However, there is also the possibility that some non-x elements might be replicated if they become the k-th element at some step. So the spread of x requires that at the times when the front is a non-x, the k-th element is x, so that we replace the front removal with x. But if the k-th is non-x, then we append a non-x, and pop the front (which could be x or non-x). This could increase non-x count.
Thus we need to examine the dynamics. Perhaps we can reframe: At each step, the queue evolves. Let’s denote the queue as Q_t of length n. Operation: read Q_t[k] (0-index? Let's use 0-index for convenience: positions 0 to n-1). Then do: Q_{t+1} = Q_t[1..n-1] + [Q_t[k]]. In terms of Python slicing.
We want to know minimal t such that all elements of Q_t are same.
Observation: The process is deterministic and the state space is finite (at most 10^5 positions each with value up to 10^5 but state count huge). However we might be able to reason about the possible transformations.
Another approach: Consider the positions of a particular value. Maybe we can treat the process as a "majority" propagation. There is known problem: "Shooshuns and Sequence" maybe known from Codeforces? Indeed I recall a Codeforces problem: "B. Shooshuns and Sequence" or something similar? Let's search memory: There is a Codeforces Round #137 problem "B. Shooshuns and Sequence" maybe? Actually I recall a problem: "B. Little Elephant and Shifts"? Not sure.
Let's think: We have operation: remove first, add copy of k-th. If we think of the sequence as circular? Not exactly.
Maybe we can consider the "distance" that an element moves. The element at index i (starting from 0) after one step: if i < n-1, it moves to index i-1 (since we delete first and shift left). Actually: old index i becomes new index i-1 if i >= 1. Old index 0 disappears. Old index k becomes also appears at new index n-1 (the last). So the element at old k is both at its old position (shifted to k-1 if k>0) and also a copy at last. So duplication.
Maybe we can think of the process as building a new sequence each time. The key is that after each step, the element that was at position k gets "propagated" to the back, and the front is removed. So if we look at the queue from front to back, the front is being replaced by whatever is at position k at that time. If we want to achieve all equal, perhaps we need that after some steps, the first n-k+1? Actually, note that position k is fixed index from front. So the element at position k at time t influences the back. Over time, the front moves forward.
Maybe we can think about the condition for impossibility: If there exists a value that appears less than k times? Hmm.
Consider the operation: we copy the k-th element. If the k-th element is x, we add an x at the end. So if we want to eventually have all x's, we need that at some point, the k-th element is x and remains x thereafter, and we need to pop all non-x's. Since front gets popped each step, the only way a non-x can survive is if it is positioned after the k-th? Actually, any element that is not at the front can survive until it becomes front. So to eliminate all non-x, we need to ensure that by the time any non-x reaches the front, the k-th element is x so that the appended element is x and the popped non-x is removed, decreasing the count of non-x. However, if when a non-x is at front, the k-th element is also non-x, then we would append non-x and pop non-x, so non-x count remains same? Actually, we remove the front (non-x) and append a copy of k-th (non-x), so the total number of non-x stays same (one removed, one added). But the positions shift. If the k-th is x, then we remove non-x and add x, decreasing non-x count by 1. So to eliminate all non-x, we need that whenever a non-x is at front, the k-th is x.
But is it necessary that the k-th is x at all such times? Perhaps we can also reduce non-x count by having the non-x at front and the k-th being x. So we want to ensure that before any non-x reaches front, we have made the k-th x, and maintain it as x until all non-x are eliminated.
Now, what determines the value at position k? Over time, as the queue evolves, the element at position k changes. Initially, it is a[k]. After each step, the new sequence's k-th element is? Let's compute: For new sequence S', for i from 1..n, S'[i] = old[i+1] for i=1..n-1, and S'[n]=old[k]. So what is S'[k]? Two cases:
- If k < n: then S'[k] = old[k+1] (since k <= n-1). So the k-th element becomes the old (k+1)-th element.
- If k = n: then S'[n] = old[k] = old[n], but also S'[n] is the last element, which is old[k] (old[n]), so S'[n] = old[n]. So if k=n, then S'[n] = old[n]; so the k-th (which is n) remains the same as before? Actually careful: When k=n, the operation: copy the n-th (last) element and append it, then remove first. So after operation, the new last element is old[n], and the new sequence's n-th element is that old[n]? Wait, S'[n] = old[k] = old[n]. So yes, the element at position n remains old[n]. So if k=n, the last element is fixed? Let's see: Initially a[n] is some value. After step, new sequence = old[2..n] + old[n]; So the last element becomes old[n], but also the element that was at position n-1 becomes new last? Actually old[2..n] includes old[n] at the end? Wait: old[2..n] means indices 2,3,...,n. So the last element of old[2..n] is old[n]. Then we append old[n] again. So new sequence has length n: first n-1 elements are old[2..n] (which ends with old[n]), and then we add old[n] as last? That would give two copies of old[n] at the end? Let's write: old = [x1, x2, ..., xn]. old[2..n] = [x2, x3, ..., xn]. Then append old[k] = old[n] = xn. So new sequence = [x2, x3, ..., xn, xn]. That's length n? Actually old[2..n] has n-1 elements, plus one more gives n. So new sequence = [x2, x3, ..., xn, xn]. So indeed, the last two elements are xn (from old[n]) and xn (copy). So the new n-th element is the appended copy, which is xn. And the (n-1)-th element is old[n] which is xn as well? Actually old[n] is xn, and it appears as the (n-1)-th element because old[2..n] ends at old[n] which is xn. So after operation, the sequence has xn at positions n-1 and n. So S'[n] = xn, same as old[n]. So if k=n, the last element stays the same value forever? Let's check next step: Now S' = [x2, x3, ..., xn, xn]. Its n-th element is xn (the last). The operation: copy S'[n]=xn, append xn, then delete first: new sequence = [x3, ..., xn, xn, xn]. So it becomes all xn eventually? After enough steps, all become xn if xn is the last. Indeed, if k=n, we just keep appending the last element (which is constant) and popping the front. So eventually after n-1 steps, the original elements will be popped, and we will have all xn. So answer would be n-1? But maybe less if earlier all equal. Example: n=3,k=3, a=[3,1,1] => initial [3,1,1]. Step1: copy last (1), append 1, delete first -> [1,1,1]. So answer 1. So not necessarily n-1. But it's possible to have all equal earlier. So k=n is always solvable? Let's check: if k=n, we always append a copy of the current last element, which is originally a[n]. But note: after first step, the last element becomes a[n] (appended), but the element at position n-1 may be different. Actually as we saw, after step, we have a[2..n] and then a[n]. So the last element is a[n]; the second last is a[n] as well? Actually a[2..n] includes a[n] as its last element, so positions n-1 and n are both a[n] after first step if n>=2. So after first step, the last two are a[n]. Then after second step, the last three? Let's think: The process for k=n essentially appends a[n] each time, and the queue gradually becomes all a[n] after at most n-1 steps (since after t steps, first t elements are removed, and we have appended t copies of a[n], but some original may remain if not all popped). Actually after t steps, the queue consists of original indices from t+1 to n, and then t copies of a[n] at the end? But careful: the appended copies may be popped later if they become front. The worst case: The only non-a[n] elements are among original a[t+1..n] that are not a[n]. As we continue, we pop front, so eventually after we have popped all original elements (n steps? Actually original elements count is n; we pop one per step, so after n steps all original elements are gone; but in those n steps we appended n copies of a[n]; after n steps, the queue is all appended copies? But also some original may have been a[n] already. But minimal steps to become all a[n] could be when the last original non-a[n] is popped. That's at most n-1 steps if the only non-a[n] are in front positions. For example, if a[n] is already all? Actually if all are a[n] initially, answer 0. If not, we need to have the first occurrence of a non-a[n] from the front? Let's analyze: After each step, the front element is removed. So if we want all elements to be a[n], we need to remove all elements that are not a[n]. But if at some step, the front is a[n] but there are non-a[n] later, we still need to eventually remove them. The process will keep popping front (which may be a[n]) and appending a[n] at back. So effectively we are shifting the queue left; the non-a[n] elements will eventually reach front and be popped. However, we also append a[n] at back. So after each step, we pop one element (maybe a[n] or not) and append a[n]. So the count of a[n] can only increase or stay same? Let's check: If we pop a[n] and append a[n] (since k=n always appends a[n] because the last element remains a[n]), then a[n] count stays same. If we pop non-a[n] and append a[n], then a[n] count increases by 1. So a[n] count is non-decreasing. And it will eventually reach n when all are a[n]. The time to reach all a[n] is equal to the number of steps needed to pop all non-a[n] elements. Since each step pops one element, and we only increase a[n] count when we pop non-a[n], we need to pop all non-a[n] initial ones plus any non-a[n] that appear later? But can non-a[n] appear later? Only if the k-th element is not a[n], but with k=n, the k-th element is the last, and after first step the last is a[n] and stays a[n] because we keep appending a[n] and the last remains a[n] (since we always append a[n]? Let's verify: After step1, last element is a[n] (appended). After step2, last element is again a[n] (appended copy of last which is a[n]). So indeed, for k=n, the last element is always a[n] after step1 onward. So the k-th element is always a[n]. So all appended copies are a[n]. So non-a[n] only come from original elements that haven't been popped. Thus the number of non-a[n] elements strictly decreases by 1 each time we pop one (since we append a[n] always). So after t steps, number of non-a[n] = initial count of non-a[n] - (number of steps where front was non-a[n]). But the front might be a[n] sometimes, which doesn't reduce non-a[n] count. So we need to pop all non-a[n] elements; the steps needed could be more than the count of non-a[n] because we might pop a[n] in between. However, we can think: The queue is moving left; non-a[n] elements are at certain positions. They will eventually become front. The minimal steps to make all a[n] is the maximum index (distance from front) of a non-a[n] element? Because the element at position i (1-indexed) will become front after i-1 pops. After that, when it becomes front, it will be popped. But if at that time we pop it, we also append a[n]. So after the element at the farthest position (from front) has been popped, all original non-a[n] are gone. So the number of steps needed is the index of the last (rightmost) non-a[n] element in the original sequence? However, after some steps, the positions shift because we append a[n] at the back. The non-a[n] elements move left. But the farthest non-a[n] from the front originally at position p will need p-1 steps to become front. However, if we pop a[n] from front before that, it doesn't affect the position of that non-a[n]? Actually popping front moves all elements left by one; so each step reduces the index of every element by 1 (except the last which is replaced). So after t steps, an original element that started at index i (if it hasn't been popped) will be at index i - t (if i > t). If i - t <= 0, it has been popped (since front is index 1). So the original element at index i will be popped exactly at step i (since after i-1 steps it becomes at index 1, and then at step i it is popped). At step i, we pop the element that was originally at index i (if it hasn't been replaced by something else). But careful: The original element could be overwritten? Actually the process doesn't overwrite elements except that the last element is replaced each step? The last element is replaced by the copy of k-th. But that means the original element that was at last position gets shifted left? Actually, at each step, we remove first and append a new element at the end. So the elements that remain are the ones from original that haven't been popped and haven't been overwritten? But they are not overwritten; they just shift left. The only new elements are those appended at the end. So original elements shift left until they reach front and are popped. They are never overwritten in positions before becoming front. So yes, original element at index i will be popped at step i (if it is still there). But is it possible that it gets popped earlier because of something else? No, because the only element removed each step is the first element. So the original element at index 1 is popped at step 1; original index 2 becomes index 1 after step1, then popped at step2; original index 3 becomes index 2 after step1, index1 after step2, popped at step3; etc. So indeed, the original element at index i is popped exactly at step i. However, there is a nuance: After we pop some elements, we also append new elements at the back. Those new elements are not original; they may be inserted after the original tail. So the original elements remain in order and are popped one by one at steps equal to their initial index. This is true because the operation does not remove any element other than the first, and does not insert any element in the middle. So the sequence of original elements (the ones present at time 0) are shifted left each step, and the first original element (index 1) leaves at step1, the second at step2, etc. Meanwhile, new elements are appended to the right, after the original tail. So after t steps, the queue consists of original elements with indices t+1 to n (still in order), followed by t new elements (the ones appended at each step). So indeed, after t steps, the sequence = [a_{t+1}, a_{t+2}, ..., a_n] + [b_1, b_2, ..., b_t] where b_s is the element appended at step s. b_s = the value that was at position k of the queue at step s-1 (since we append before popping? Actually at step s, we take the k-th element of current queue and append it. But careful with indexing: At step 1, we take k-th element of S_0 and append it as b_1, then pop front. So after step 1, we have original a_2..a_n then b_1. At step 2, we take k-th element of that queue (which could be either from the remaining original part or from b_1) and append it as b_2, then pop front. So b_s depends on the state.
Thus, the observation that after t steps, the queue is original tail of length n-t (if t < n) plus t appended elements, but the order among appended elements is as appended: b_1 first (closest to the front among appended? Actually after step1: order is a_2, a_3, ..., a_n, b1. After step2: we pop a_2, then append b2, so sequence becomes a_3,...,a_n, b1, b2. So indeed, after t steps, the sequence is: a_{t+1}, a_{t+2}, ..., a_n, b_1, b_2, ..., b_t. That is, the appended elements are in order of creation at the end.
Thus, after t steps, the first part (original tail) are the original elements from index t+1 onward. The second part (appended) are the b_i.
Now, for all numbers to become same at time t, we need that all elements in this queue are equal to some value x. That means:
- All a_i for i > t must be equal to x.
- All b_i for i = 1..t must be equal to x.
So a necessary condition: There exists some x such that for all i > t, a_i = x, and also all b_i = x for i=1..t.
But b_i depend on the process. They are the values at position k of the queue at step i-1.
We can also express b_i in terms of the original a and previous b's.
Now, we want minimal t.
Observation: If we want all equal to x, then at time t, all a_i for i>t are x. That means that the suffix of the original array from index t+1 to n must be all x. So the original array must have a suffix of all x of length at least n - t.
Conversely, if we have a suffix of length L (positions n-L+1 .. n) all equal to x, then after t = n - L steps, the original tail will consist only of those L elements, which are all x. So condition: after t steps, the original tail is all x.
Now we also need that the appended b_i are all x. So we need that during the process, when we took the k-th element at each step s from 1 to t, that element was x.
Thus, the problem reduces to: Find minimal t such that:
- The suffix of a of length n-t (i.e., positions t+1..n) are all equal to some x.
- For each step s = 1..t, at that step, the k-th element of the queue (which at step s-1) is x.
We need to check if there exists x (which will be the value that the suffix consists of) satisfying these.
Moreover, t can be 0 (if entire array already all same). So we need to handle t=0.
Now we need to compute the condition for steps. We need to simulate? But maybe we can characterize when the k-th element is x during the process, given that we know the state: after s-1 steps, the queue is: a_{s}...a_n, b_1...b_{s-1}. So at step s, the queue length n, with positions 1..n. We need its k-th element to be x.
We want to ensure that for each step s from 1 to t, the k-th element equals x.
We have control over t and x (x must be the value of the suffix). And we can perhaps determine for a given x, the minimal t such that conditions hold.
We need minimal t overall.
Let's denote L = n - t, the length of the original suffix that remains (all x). Then t = n - L, where L is length of suffix all equal to x. Since t must be >=0, L <= n.
We need that for s = 1..t, at step s, the k-th element is x.
Let's attempt to express the queue at step s-1 (0-indexed). For step s, we consider state after s-1 operations.
State after s-1 operations: Q_{s-1} = [a_s, a_{s+1}, ..., a_n, b_1, b_2, ..., b_{s-1}], where b_i are the appended elements from previous steps. Note: When s-1 = 0, Q_0 = a_1..a_n.
Now, we need that the k-th element of Q_{s-1} is x.
Given that we want all b_i for i=1..s-1 to be x (since we require eventually all b_i = x, but at step s we haven't yet ensured that all b_i up to t are x; however we need that for the process to yield x at each step, b_i themselves must be x because they are appended copies. Actually we have a circular condition: To have b_s = x, we need that at step s, the k-th element is x. And b_s is that x (since we append it). So b_s = x if condition holds. So recursively, if we ensure that at each step the k-th element is x, then b_s = x automatically. So condition 2 (b_i = x) is equivalent to: at each step s, the k-th element is x. Because b_s is exactly the element we append, which must be x.
Thus we need that for s=1..t, the k-th element of Q_{s-1} is x. And also at the final state after t steps, all a_i for i>t are x and all b_i for i<=t are x (which is consistent if condition holds).
So the problem is: find minimal t (or maximal L) such that:
- The suffix a_{t+1}..a_n are all equal to some x.
- For each s=1..t, the k-th element of Q_{s-1} is x.
Now, Q_{s-1} = [a_s, a_{s+1}, ..., a_n, b_1,...,b_{s-1}], and we already will have that if condition holds for earlier steps, then b_1..b_{s-1} are x. So we can assume b_i = x for i < s when checking condition for step s. But we need to check consistency: the condition that k-th element is x must hold assuming b_1..b_{s-1}=x? Actually it's a simultaneous requirement: for the process to actually produce x at each step, we need that at step 1, the k-th element of Q_0 is x, which then sets b_1 = x. Then at step 2, using b_1 = x, the k-th element of Q_1 must be x, etc. So the condition is that for s=1..t, given that b_1..b_{s-1}=x, the k-th element of Q_{s-1} is x.
Thus we can formulate as: For s from 1 to t, we require:
Let the state after s-1 steps be: prefix of original from s to n, then s-1 copies of x at the end.
So Q_{s-1} = [a_s, a_{s+1}, ..., a_n, (x repeated s-1 times)].
We need that the element at position k (1-indexed) in this list is x.
Now, this condition must hold for each s.
Thus, the problem reduces to: Given a, n, k, find the smallest t (0 <= t <= n) such that:
- For all i > t, a_i = x (some common value, which will be the final uniform value). (Note: If t = n, then there is no original suffix condition? Actually if t=n, then after n steps, the original tail is empty; all elements are b_i. Then condition reduces to: for s=1..n, the k-th element of Q_{s-1} is x, and then b_i all x, so all elements become x. Also we need to check that after n steps, the queue consists only of b's, all x. So t=n is possible if we can have x such that for s=1..n, condition holds. But note t cannot exceed n? Actually t could be larger than n? Let's see: after n steps, all original elements are gone. After that, the queue consists solely of appended elements. Then further steps: if all are x, they remain x. So minimal t might be > n? Could we need more than n steps? Possibly, if after n steps we still have some non-x because the appended elements were not all x. But t is the step at which all become equal. Since after n steps, if we haven't achieved all x, we might still achieve later? But after n steps, the queue consists entirely of b_i for i=1..n. Those b_i are the values appended at steps 1..n. If they are not all equal to some x, then the state is not all equal. However, the process continues. The next steps will have queue of all b_i (some values). The process could eventually make them all equal? Possibly yes, if the condition can be satisfied later. But note that after n steps, there are no original elements left; the queue is entirely determined by the b_i (which are the results of previous steps). Then the process continues: we have a sequence of length n of some values. Then it's like starting from a new sequence, but with the same operation. So t could be > n. But we are asked minimal operations; we must consider all t >=0.
Nevertheless, the condition we derived for t steps assumed that after t steps, the original tail (of length n-t) is all x and the appended b_i (for i <= t) are all x. But if t > n, then the original tail length is negative, meaning no original elements left. So after t steps, the queue is entirely composed of b_i for i = t-n+1 ... t? Actually careful: After n steps, all original elements are popped. After further steps, the queue consists of b_i from some range. Let's generalize: After t steps, the queue is: original elements from indices t+1 to n (if t < n) plus b_1..b_t (the appended ones). But if t >= n, then there are no original elements left; the queue is b_{t-n+1} ... b_t (the most recent n appended elements). Because each step we pop one and append one, so it's like a sliding window over the infinite sequence of appended b_i. Actually we can define b_i for i >=1 as the appended element at step i. Then after t steps, the queue consists of the last n elements of the sequence: a_{t+1}..a_n (which only exist if t < n) concatenated with b_1..b_t? Hmm, not exactly. Let's derive properly.
Let's index steps from 1. At step 0: Q_0 = a[1..n].
After step 1: Q_1 = a[2..n] + [b1], where b1 = Q_0[k].
After step 2: Q_2 = a[3..n] + [b1, b2], where b2 = Q_1[k].
After step t (t <= n): Q_t = a[t+1..n] + [b1, b2, ..., bt].
After step n: Q_n = [b1, b2, ..., bn].
After step n+1: We have Q_{n+1} = ? It would be: we pop first element of Q_n, which is b1, and append b_{n+1} = Q_n[k]. So Q_{n+1} = [b2, b3, ..., bn, b_{n+1}].
Similarly, after step t > n: Q_t = [b_{t-n+1}, b_{t-n+2}, ..., b_t].
Thus after t steps, the queue is the last n elements of the infinite sequence b_1, b_2, ..., with b_i defined recursively: b_i = element at position k of Q_{i-1}.
But the condition that all numbers are equal at time t is that all elements in Q_t are equal to some x. For t <= n, we have the condition that the suffix of a (positions t+1..n) are all x and b_1..b_t are all x. For t > n, we need that the n most recent b_i (from t-n+1 to t) are all x. However, if all b_i from some index onward become x, then eventually the queue will be all x. So t could be larger.
But perhaps we can prove that if it's possible to achieve all equal at all, then it can be achieved within n steps? Let's check with examples. Is there a case where minimal t > n? Let's attempt to construct. Suppose n=3, k=2. Consider a = [1,2,3]. Can we ever get all equal? Let's simulate quickly? Operation: k=2. Q0=[1,2,3]; b1 = Q0[2]=2, Q1=[2,3,2]; b2 = Q1[2]=3, Q2=[3,2,3]; b3 = Q2[2]=2, Q3=[2,3,2]; cycles maybe? Does it become all same? Possibly not. But could there be a scenario where after >n steps we get all equal? For instance, suppose after n steps, we get a mixture, but later due to the dynamics it becomes uniform. Since the process is deterministic and the state space is finite (there are at most (max value)^n states, but practically infinite), it could eventually reach a uniform state after some transient. But can that happen only after >n steps? Possibly if the uniform state is reachable but the initial suffix condition fails? Let's try to think: The uniform state is a fixed point. Starting from any state, if we eventually reach the uniform state, then we will be there. But is it possible that we need more than n steps to reach it? For example, suppose n=3, k=1. We saw it cycles and never becomes uniform. So no. But maybe some other values: n=4, k=2, a = [1,2,3,4]. Let's simulate manually a bit? Not sure.
We need to find if there exists a case where t > n. Possibly not because the condition for all equal seems to be tied to the suffix being all same after certain steps. Let's attempt to reason: Suppose at time t all elements are x. Then looking backwards, what must be true? At time t-1, we had Q_{t-1} which after operation becomes Q_t all x. The operation: Q_t = Q_{t-1}[2..n] + [Q_{t-1}[k]]. For Q_t to be all x, we need Q_{t-1}[2..n] all x and Q_{t-1}[k] = x. Therefore Q_{t-1} must have: positions 2..n all x, and position k = x. This implies that Q_{t-1}[1] could be anything? Because Q_{t-1}[1] is removed. So Q_{t-1} could have any value at position 1, but positions 2..n must be x, and position k must be x (which is already covered if k>=2). So if k>=2, then Q_{t-1} must have all positions except possibly position 1 equal to x. If k=1, then we need Q_{t-1}[1] = x as well because Q_{t-1}[k] = Q_{t-1}[1] must be x to append x; and also Q_{t-1}[2..n] must be x. So for k=1, Q_{t-1} must be all x as well. That suggests that if k=1, the only way to get all x at step t is that Q_{t-1} is already all x. Because Q_{t-1}[1] is removed, but we need Q_{t-1}[1] = x to have the appended element x (since k=1). And positions 2..n must be x. So Q_{t-1} must be all x. Thus if k=1, once you are all x, the previous state must also be all x. Therefore the only way to reach all x from a non-all-x state is if k>1? Actually if k=1, the all-x state is a fixed point, but to reach it, you would need to have the previous state also all x (since from non-all-x, you cannot become all x in one step because to get all x at next step, you need positions 2..n all x and position1 = x; thus entire previous state all x). Therefore if k=1, the only way to have all x at time t>0 is if already all x at time 0. Because if at some step we become all x, then the step before must also be all x, and so on. Thus if initial not all same, it's impossible. That matches example 2 (k=1 gave -1 unless initially all equal). So for k=1, answer is 0 if all equal initially, else -1. Good.
Now for k>1, we can have transitions that lead to all x even if previous state not all x. In particular, the condition is: Q_{t-1} has all x from positions 2 to n, and Q_{t-1}[k]=x (which is automatically true if k>=2). Then Q_{t-1}[1] can be anything. So it's possible to have a state where only the first element is non-x, and all others are x, and then after one operation we get all x (since we remove the first non-x and append x). Indeed, example 1: n=3,k=2, a=[3,1,1] -> Q0: [3,1,1] -> not all 1 because first is 3. But positions 2..3 are 1, and k=2 is 1, so after operation we get all 1. So that matches.
Thus, for k>1, we can have a "pre-uniform" state where only the first element may differ.
Now, can we have a scenario where the uniform state is reached after more than n steps? Let's think backwards: To have Q_t all x, the previous state Q_{t-1} must have positions 2..n all x. This implies that at step t-1, all but possibly the first element are x. So if we go backwards further, what must Q_{t-2} be? Q_{t-1} itself must have been generated from Q_{t-2} by operation. So we need to find a sequence of states backwards that leads to initial state. This is like a reverse process.
But maybe we can determine minimal t directly by analyzing forward: We need to ensure that for steps s=1..t, the k-th element of Q_{s-1} is x. And also that after t steps, the original suffix of length n-t is all x. So t cannot be greater than n because after n steps, the original suffix is empty, which trivially is all x (vacuously true). So t = n is possible if we can ensure that for s=1..n, the k-th element of Q_{s-1} is x, and then b_i are all x, so all elements are x. But could t be larger than n? Suppose t = n+1. Then after n+1 steps, the queue is b_2..b_{n+1} (or the most recent n b's). For it to be all x, we need that b_{2}..b_{n+1} are all x. But also we need that the process conditions hold for s=1..t? Actually to achieve all x at step t, we don't necessarily require that at earlier steps the k-th element was x? Wait, we require that the process actually takes place; the b_i are determined by the actual process. If we want Q_{n+1} all x, we need that the process from step 0 to n+1 yields that state. It might be possible that Q_n is not all x but Q_{n+1} becomes all x. Let's check if that can happen.
Suppose at step n, Q_n = [b1, b2, ..., bn] is not all x, but after one more operation we get Q_{n+1} all x. For that to happen, from Q_n we need positions 2..n all x and Q_n[k] = x. So Q_n must have: positions 2..n all x, and element at k is x. This implies that b2..bn are all x, and b_k = x (since positions are 1..n correspond to b1..bn). Also b1 could be anything? Since position 1 is b1, which is removed. So Q_n could have b1 arbitrary, and b2..bn all x, and b_k = x (automatically if k>=2). So Q_n could be: [y, x, x, ..., x] with all x except possibly first. Then after operation, we get: Q_{n+1} = [x, x, ..., x, Q_n[k]] = [x, x, ..., x, x] if Q_n[k]=x, which holds. So it's possible that Q_n is not all x but Q_{n+1} is all x. In that scenario, the minimal t could be n+1. However, is it possible to have such a Q_n from the initial a? Q_n = [b1,...,bn] where b_i are defined recursively. Could we have b1 not equal x, but b2..bn = x? That would require that for steps s from 1 to n, the conditions on k-th elements hold appropriately, leading to b_i values. Let's see if that could happen with t = n+1 as minimal.
But we should check if such a scenario can actually occur given the initial a. Let's attempt to construct a small example with n>1, k>1.
We need to find a sequence a such that after n steps we get Q_n = [y, x, x, ..., x] (with b1 = y != x, and b2..bn = x) and then after one more step we get all x. That would mean minimal t = n+1 if earlier steps not all x.
But is it possible that b2..bn become x while b1 is not x? b1 is determined at step 1: b1 = a[k] (since Q_0[k] = a_k). So b1 = a_k. For b2 to be x, we need at step 2, the k-th element of Q_1 to be x. Q_1 = [a_2..a_n, b1]. So the k-th element of Q_1 must be x. That may impose constraints on a. And so on. Could it be that a_k != x but still we can have b2..bn = x? Possibly, but we also need that after n steps, the original suffix condition for t=n? For t=n, the original suffix is empty, so condition is just that b1..bn all x? Actually for t=n, the condition for all equal is that Q_n is all x. So if we have Q_n not all x, then t != n. But we might achieve all x at t=n+1.
Thus, t could be > n. However, note that if we can achieve Q_n having all x except possibly the first element, then we can achieve all x at n+1. But could we also achieve all x earlier? Possibly t = something else.
But let's see if such a scenario is possible given the recursion. We need to analyze forward conditions.
We can think of the process as building b_i. b_i = value at position k of the queue at step i-1.
We have Q_{s-1} = [a_s,...,a_n, b_1,...,b_{s-1}].
Thus, for step s, condition: Q_{s-1}[k] = x.
Now, note that the queue length is n, and its composition depends on s.
We need to find smallest t such that for all s=1..t, Q_{s-1}[k] = x, and also after t steps, the remaining original tail (if any) are all x. Actually, the condition for final all equal is exactly: after t steps, Q_t is all x. But if we have ensured that for s=1..t, Q_{s-1}[k] = x, then b_s = x for s=1..t. So Q_t = [a_{t+1}..a_n, x, x, ..., x] (t copies of x). For Q_t to be all x, we need a_{t+1}..a_n all x as well. So that's the condition we had earlier. But wait, if t > n, then a_{t+1}..a_n is empty, so Q_t = [b_{t-n+1}..b_t]. And we also have that b_s = x for s=1..t (if we ensured Q_{s-1}[k]=x for s=1..t). But is it true that b_s = x for all s up to t? If we ensure that for each step s, Q_{s-1}[k]=x, then indeed b_s = x. So for t > n, we would have that all b_i for i=1..t are x. Then Q_t = the last n b_i, all x. So the condition reduces to: for s=1..t, Q_{s-1}[k] = x. And for t > n, there is no original suffix condition because it's empty. So the condition for all x at time t (any t) is: There exists x such that for all s=1..t, Q_{s-1}[k] = x, and also for the original elements that remain at time t (which are a_{t+1}..a_n) they must all be x. But if t >= n, then there are no original elements, so only need for s=1..t, Q_{s-1}[k]=x, which implies all b_i = x, and then Q_t will be all x (since Q_t consists solely of x's). However, note that for t > n, the condition Q_{s-1}[k]=x must hold for s up to t. But if we already have Q_{s-1}[k]=x for all s up to some T, then for any t >= T, Q_t will be all x? Let's check: If we have that for s=1..T, Q_{s-1}[k]=x, then b_1..b_T are x. But for steps beyond T, we need to check whether the condition continues to hold automatically? Not necessarily. Because after step T, if Q_T is all x, then for s = T+1, Q_T[k] = x, so b_{T+1}=x, and condition holds. So once we reach a state where Q_T is all x, it will stay all x. So the minimal t is the first time when Q_t is all x. And Q_t becomes all x exactly when both (i) all b_i up to t are x (ensured by condition for steps 1..t) and (ii) the remaining original tail (if any) are x. But if we have condition for steps 1..t, then b_i are x for i<=t. However, does condition for steps 1..t guarantee that the remaining original tail is x? Not necessarily. Because Q_t includes a_{t+1}..a_n. Those could be non-x even if b_i are x. To have Q_t all x, we need a_{t+1}..a_n all x as well. So the condition for steps 1..t alone is not sufficient; we also need that the original tail is all x at time t.
But if t >= n, the original tail is empty, so condition reduces to having b_i = x for i = 1..t. But to have b_i = x for i=1..t, we need the condition Q_{s-1}[k]=x for s=1..t. In particular, for s <= n, this condition involves original elements. So for t >= n, we need that for s=1..n, Q_{s-1}[k]=x, which will force b_1..b_n to be x, and then for s>n, since Q_{n} is all x? Actually if b_1..b_n are x, then Q_n is all x (since Q_n = [b_1..b_n] and all x). Then for any s>n, condition automatically holds, and Q_t remains all x. So the minimal t for which Q_t becomes all x could be exactly n if Q_n is all x. But if Q_n is not all x, then perhaps later steps can still become all x? Let's see: Suppose Q_n is not all x. Then for t > n, we would need that eventually Q_t becomes all x. But could that happen without Q_n being all x? If Q_n is not all x, then at step n+1, we have Q_{n+1} = [b_2..b_{n+1}]. For Q_{n+1} to be all x, we need b_2..b_{n+1} all x. But b_2..b_n are part of Q_n, and b_{n+1} is new. If b_2..b_n are all x, and b_{n+1}=x, then Q_{n+1} is all x. But if b_2..b_n are all x but b_1 is not x, then Q_n = [y, x, x, ..., x] with y != x. That scenario we considered. So it's possible that Q_n is not all x, but Q_{n+1} is all x. In that case, minimal t = n+1. For that to happen, we need b_2..b_n = x, b_{n+1}=x, and b_1 != x. But note b_{n+1} = Q_n[k]. Since Q_n = [b_1, x, x, ..., x], if k>1, then Q_n[k] = x, so b_{n+1}=x. So condition holds. So it's plausible.
Thus minimal t could be n+1. Could it be larger? Possibly, if we have a longer prefix of non-x b's that get shifted out gradually. For t > n, Q_t all x would require that b_{t-n+1}..b_t are all x. That is equivalent to the last n b_i being x. Since b_i are generated by the process, if at some point we start generating x's and continue, eventually after we have generated n consecutive x's (i.e., b_i = x for a block of length n), then the queue will be all x. However, the generation of b_i is determined by the condition Q_{i-1}[k] = x. So the process of generating b_i may become x permanently after some step.
So the minimal t when Q_t becomes all x is the first time when we have generated enough x's such that the last n elements (which are the most recent b_i) are all x and also the original tail (if any) are gone or x. But since original tail disappears after n steps, for t >= n, we just need that b_i are x for i = t-n+1 .. t. That is, we need a run of n consecutive x's in the b sequence ending at t. Since b_i = Q_{i-1}[k] and Q_{i-1} includes some of the previous b's.
But maybe we can find a simpler way: The problem is known; likely the answer is either -1 or the minimal number of operations is the minimal t such that from some point onward the k-th element is always some value x and the suffix of the original of length n-t is all x. I recall a Codeforces problem: "B. Shooshuns and Sequence" (maybe problem 265A?). I think I've seen a solution: It's about checking if the last n-k+1 elements are all the same? Wait, maybe there is known result.
Let's search memory: I think there is a problem "Shooshuns and Sequence" from Codeforces Round #... Actually I recall a problem: "A. Shooshuns and Sequence" or "B. Shooshuns and Sequence" where answer is either -1 or the maximum distance of a different number from the end? Something like: Find the last position where the number differs from the last element, and then answer based on k. Let's test with examples.
Example 1: n=3,k=2, a=[3,1,1]. The last element is 1. The suffix of same numbers from the end: positions 2 and 3 are both 1? Actually from the end: a3=1, a2=1, a1=3. The longest suffix of equal numbers is length 2 (positions 2-3). Then maybe answer is something like max(0, (position of first different from the end) - k)? Not sure.
Try to derive systematically.
We need minimal t such that:
- For all i > t, a_i = x (some x).
- For s = 1..t, Q_{s-1}[k] = x.
We also need to determine x. Since the suffix a_{t+1}..a_n are all equal to x, x is forced to be that value. Typically, the only candidate for x is the value at position n? Because the suffix must be constant; if multiple suffixes possible, t minimal means larger suffix? Actually we want minimal t, so we want as large as possible t? Wait, t is number of steps. Condition: a_{t+1}..a_n all equal to x. To minimize t, we want t as small as possible, which means we want to have the suffix of equal numbers starting from some index as early as possible? Actually if we need a_{t+1}..a_n all equal, then t must be at most n - L where L is the length of the longest suffix of identical numbers. Because if the longest suffix of identical numbers has length L (i.e., a_{n-L+1}..a_n are all same), then for t = n - L, we have a_{t+1}..a_n = that suffix, all equal to x. So the smallest possible t from this condition alone is n - L, because if we take t smaller than that, then the suffix would include some index where a_i differs, violating condition. So t must be at least n - L. That is, t_min_candidate = n - L, where L = length of longest suffix of equal numbers. But we also need the other condition about k-th element during steps.
So the minimal possible t is at least n - L. It could be larger if the k-th condition forces more steps.
Thus, candidate x is the value of that suffix (i.e., a_n, since suffix is at the end). Because the suffix of identical numbers at the end: all equal to a_n (since it's the last element). So x = a_n.
Now, we need to check if we can achieve all x with t = n - L steps, or need more.
If the suffix already covers the whole array (L = n), then t_min=0, answer 0 if all equal initially.
Otherwise, we need to verify if for s = 1..t, with t = n - L, we have Q_{s-1}[k] = x.
But maybe we can compute necessary and sufficient condition for feasibility and minimal t in terms of the