We need to find the smallest positive integer n such that the number consisting of n sevens (i.e., repunit with digit 7: 7, 77, 777, ...) is divisible by K. If none exists, output -1.
Let X_n = 7 * (10^n - 1) / 9? Actually repunit: R_n = (10^n - 1)/9 is 111...1. Then 7 repeated n times is 7 * R_n = 7 * (10^n - 1)/9. But we can also treat as number A_n = 777...7 (n times) = 7 * (10^{n-1} + 10^{n-2} + ... + 1) = 7 * (10^n - 1) / 9.
We need A_n ≡ 0 mod K.
But careful: If K and 9 have gcd? Since A_n = 7*(10^n-1)/9, we need integer divisibility. A_n is integer because 10^n-1 is divisible by 9 (since 10 ≡ 1 mod 9). So the fraction yields integer.
We want K | A_n.
That is, K divides 7*(10^n-1)/9 => 9K divides 7*(10^n-1) ??? Not exactly. Condition: (7*(10^n-1)/9) ≡ 0 mod K => 7*(10^n-1)/9 = K * m for some integer m => 7*(10^n-1) = 9 K m => So 9K divides 7*(10^n-1). But since we need divisibility of the integer A_n by K, we can work modulo K.
Better: A_n mod K = 0.
Compute A_n modulo K efficiently. A_1 = 7 mod K. A_2 = 77 = 710 + 7 = 10A_{n-1} + 7? Actually recurrence: A_{n} = 10 * A_{n-1} + 7, with A_0 = 0 (maybe). So we can iterate n from 1 upwards, computing remainder r = (r*10 + 7) % K, starting r=0. Then we need r==0. That's straightforward, but K up to 1e6, and we may need to go up to maybe K or beyond? In worst case, if K and 10 are coprime, the order of 10 modulo K may be up to φ(K) which could be large (K up to 1e6 -> φ(K) ≤ 1e6). Actually, since we're looking for n such that A_n ≡ 0 mod K. The recurrence may cycle eventually, but the maximum n needed is at most K? Because there are only K possible remainders for r, so if we haven't found 0 by the time we've done K steps, by pigeonhole principle we'll have a repeat remainder, which leads to a cycle. But we need to know if it's guaranteed that if we never hit 0, we will eventually cycle and never hit 0, so answer -1. So we can simulate up to K steps, if we hit 0, output n; else -1. That's O(K) which is fine for K ≤ 1e6.
But careful: The recurrence r_{n} = (r_{n-1}10 + 7) % K. Starting with r_0 = 0? If we start with n=1: r_1 = 7 % K. So we can loop n from 1 to K: compute r = (r10+7)%K, check if r==0, then output n and exit. After loop, output -1.
But need to be careful: Starting with r = 0 for n=0. Then for n=1: r = (0*10+7)%K = 7%K. So that's correct. So we'll iterate n=1..K inclusive.
But is there a possibility that the answer > K? Since remainders modulo K are from 0 to K-1, if we haven't encountered remainder 0 after K steps, then some remainder repeated, which means the sequence becomes periodic without hitting 0. But is it possible that the first occurrence is exactly at n > K? That would mean the remainder 0 occurs for the first time after we've already seen all other remainders? But if 0 hasn't occurred in the first K steps, the recurrence eventually repeats a previous remainder (since there are only K possible values). Suppose r_i = r_j for some i<j. Then from then on the sequence repeats. If 0 is not in that cycle, it will never appear. Could it be that 0 appears after the first repetition? That would mean at step j, we have same remainder as at i, and later we get 0 at step >j. But if the recurrence is deterministic and depends only on previous remainder, then the sequence from i onward is same as from j onward, so we would get the same pattern. If 0 appears at step t > j, then from i we would also get 0 at step t - (j-i)? Actually, careful: The recurrence is a first-order linear recurrence. If we have r_i = r_j, then r_{i+1} = (r_i10+7)%K = (r_j10+7)%K = r_{j+1}, etc. So the sequence from i onward is identical to from j onward. If there is a 0 at position t (t>j), then we also have r_{i+(t-j)} = r_t = 0, which would be earlier if i < j. That would mean 0 would have occurred earlier at step i+(t-j) which is less than t if i<j. Since we assumed first occurrence is at step t, that would contradict minimality. Therefore, if 0 ever appears, it will appear within at most K steps because before hitting 0, all visited remainders must be distinct (otherwise if a repeat occurs before 0, then 0 would never occur because the cycle doesn't contain 0? Actually, the argument shows that if a repeat occurs before hitting 0, then 0 cannot appear later because the state has repeated and the deterministic sequence would then repeat and never hit 0? Let's examine: Suppose we have indices i<j, r_i = r_j, and no r_k=0 for k ≤ j. Then for all k≥i, the sequence repeats with period (j-i). Since 0 is not among r_i,..., r_{j-1}, it will never appear later because the sequence from i onward is periodic and does not contain 0. So indeed, if we have any repeat before hitting 0, then 0 never appears. Therefore, if 0 ever appears, it must appear before any repeat, meaning it appears within the first K steps (since after K+1 steps, by pigeonhole principle there must be a repeat). So maximum n needed is at most K. So we can simulate up to K times. That's fine.
But be careful: The initial remainder for n=0 is 0? Actually, the number with zero sevens? Not considered. We want n≥1. So we start with r=0 for n=0. Then iterate. So if K=1? For K=1, 7 is divisible by 1, so answer should be 1. Check: r = (0*10+7)%1 = 0, so n=1 -> remainder 0, output 1. Good.
If K=7? 7 is divisible, answer 1. Works.
If K=2? r1 = 7%2=1 not 0; r2 = (110+7)%2 = 17%2=1; then remainder stays 1? Actually compute: r2 = (110+7)%2 = 17%2=1, so we get stuck at 1; never 0. So after K=2 steps, no 0, answer -1. Works.
Edge: K=10^6, simulation 1e6 steps fine.
But we must consider that the recurrence (r*10+7) mod K might overflow if using 32-bit integer? In Python it's fine. We'll use Python.
However, there is also a more mathematical approach: solve 7*(10^n-1)/9 ≡ 0 (mod K). That implies 10^n ≡ 1 (mod K') after adjusting for gcd with 9 and 7? Let's derive: We need (7/9)(10^n - 1) ≡ 0 mod K. Multiply both sides by 9: 7(10^n - 1) ≡ 0 (mod 9K). But careful: The condition A_n integer divisible by K means 9K | 7*(10^n - 1)??? Actually if A_n = 7*(10^n-1)/9, then K | A_n means there exists integer m such that A_n = Km => 7(10^n-1) = 9Km => 9K divides 7(10^n-1). So we need 9K | 7*(10^n-1). But 7 and 9K may have gcd >1. Write g = gcd(7,9K). Then condition becomes (9K/g) divides (10^n - 1). Because 7*(10^n-1) divisible by 9K => (9K) / d divides (10^n - 1) * (7/d) maybe? Actually, we can avoid this complexity and just simulate.
But simulation is straightforward. We'll do that.
Now, check sample: K=101 -> output 4. Let's test: n=1:7 mod101=7; n=2: (710+7)=77 mod101=77; n=3: (7710+7)=777 mod101? 1017=707, 777-707=70; so r=70; n=4: (7010+7)=707 mod101? 101*7=707 -> r=0. So answer 4. Works.
K=2 -> output -1.
K=999983 (a prime?) Output 999982. That is K-1. So matches expectation: 10 is primitive root mod prime? Maybe. But simulation would find n=999982.
Thus, algorithm: read K. Set rem = 0. For i in range(1, K+1): rem = (rem * 10 + 7) % K; if rem == 0: print(i); return. After loop, print(-1).
But careful: If K is large, the loop may be up to 1e6, which is fine.
However, there is a subtlety: The recurrence might need more than K steps if we start at n=0? Actually we argued that if we haven't seen 0 within K steps (from n=1 to K), then we must have a repeat among the remainders r_1,...,r_K. But r_0 = 0 is also a remainder, but we ignore it because it corresponds to n=0. However, r_0 = 0. But that's the remainder 0 at n=0, but we only care about n>=1. So if we hit 0 again at n>0, that's fine. But note that r_0=0 could be the repeat. If after some steps we get r_i = 0 for i>0, we are done. If we never get 0, the sequence of remainders r_1,...,r_K are all non-zero, and there are K of them, but there are only K-1 non-zero remainders possible. So by pigeonhole principle, among the K non-zero remainders, at least two are equal. Thus we have a repeat before hitting 0. So indeed we only need to check up to K steps. Good.
But one more nuance: When K=1, r_1 = (0*10+7)%1 = 0, so we output 1. That's within K steps (K=1). So loop up to K works.
Thus solution is trivial.
We should implement in Python.
But note that K can be up to 10^6, but we need to be careful if K is multiple of 2 or 5? In those cases, 10 and K may not be coprime, but the recurrence still works. Actually, if K shares factors with 10, then 10 is not invertible mod K. But the sequence still cycles. The answer may still be found; for K=5, 7 mod5=2; 2*10+7=27%5=2; so stuck at 2; never 0 -> -1? Check: 7,77,777,... Are any multiples of 5? Numbers ending with 7 are not multiples of 5, so indeed -1. For K=25? 7,77,777... Not multiples of 25? Possibly some may be? Let's test: 777... maybe multiple of 25? Number ending with 7 cannot be divisible by 5, so cannot be divisible by 25. So answer -1 for K divisible by 5 or 2? Actually, for K even, numbers ending with 7 are odd, so cannot be even. So for K even, answer -1. However, there is nuance: If K is even, then no multiple of K can end with 7 because any multiple of an even number is even (ends with even digit). So indeed -1 for even K. But our simulation will correctly find no 0 and output -1. But we could early-exit: if K % 2 == 0, print -1. Similarly, if K % 5 == 0, print -1. However, not necessary but may speed up. But careful: If K is a multiple of 2 or 5, it's not necessarily always -1? Consider K = 1? 1 is odd and not multiple of 5, fine. K=2: even -> -1. K=4: 4 even, could there be a number of all sevens divisible by 4? Check: 7 mod4=3; 77 mod4=1; 777 mod4=... 7 mod4=3; actually 777 = 777 mod4? 776 divisible by 4, so 777 ≡1. So pattern maybe cycles; does it ever become 0? Since 4 even, number must end with an even digit to be divisible by 4. A number ending with 7 is odd, so cannot be divisible by 4? Actually, divisibility by 4 depends on last two digits. Last two digits of numbers with all sevens: ...77 always ends with 77. 77 mod4 = 1. So never divisible by 4. So indeed -1. So even K always yields -1 because any integer ending with digit 7 is odd, so cannot be divisible by any even number. Therefore answer -1 for even K. Similarly, for K multiple of 5, number ending with 7 is not 0 or 5, so not divisible by 5, thus not divisible by any multiple of 5. So answer -1 for K % 5 == 0. So we could quickly output -1 for those cases. But careful: What about K = 1? not even. K = 25? multiple of 5 -> -1. So those are safe early exits. But we don't need to, as simulation will also yield -1 quickly because the recurrence will cycle among some remainders not including 0. But simulation might run up to K steps, which for K=1e6 and K even or multiple of 5, will be wasted. But K=1e6 is still only 1e6 iterations, which is trivial. However, if we have many test cases? Only one. So it's fine.
But I recall that this problem might appear from AtCoder Beginner Contest (maybe "Multiple of 7"? Actually there is a problem "Multiple of K" with 7's. It's typical to use BFS for remainder? Actually, many solutions use the iteration with up to K steps.
Thus, answer: implement as described.
Now, let's double-check the third sample: K=999983 (prime). The answer is 999982, which is K-1. That matches that the order of 10 modulo K/g? Actually, we need 10^n ≡ 1 mod (9K/gcd(7,9K)?). For prime not 2 or 5, it's plausible.
Now, let's ensure there is no off-by-one: The first element is 7 (n=1). So if we find n where remainder becomes 0, that's the answer. Good.
Now, think about potential pitfalls: In Python, using large integer operations is fine. However, the loop (for i in range(1, K+1)) for K up to 1e6 is okay.
But there is also a risk if K=0? K≥1, so fine.
Now, we need to output -1 if not found.
Thus, the solution is straightforward.
But the problem statement says "If the sequence contains no multiples of K, print -1 instead." That's what we do.
Now, I'll provide a solution in Python:
def solve():
import sys
K = int(sys.stdin.readline())
rem = 0
for i in range(1, K+1):
rem = (rem * 10 + 7) % K
if rem == 0:
print(i)
return
print(-1)
That's it.
But there might be a more optimized approach using BFS or Floyd's cycle detection to avoid the worst-case O(K) if K were larger, but K=1e6 is fine.
Thus final answer.
But the user asked to provide answer with explanation? The instruction: "Takahashi loves the number 7 and multiples of K. ..." They expect the code.
Given the chat, I should output final answer: a Python solution.
But note: The platform may require a full program with input reading and output. So I'll provide that.
But also note: There might be a hidden nuance: K may be up to 10^6, but the answer could be up to K? In the worst-case, like prime K not dividing 7? Actually, if K is a prime not equal to 2,5,7? Let's think: The condition reduces to 10^n ≡ 1 mod something. The maximum possible n is at most λ(9K/gcd(7,9K)). That could be as high as maybe 9*K? But our earlier reasoning said at most K. But is that always true? Let's re-examine the pigeonhole principle carefully.
We are iterating the recurrence: r_{n} = (10 * r_{n-1} + 7) mod K, with r_0 = 0. We are looking for smallest n>=1 with r_n = 0.
We have K+1 states (r_0, r_1, ..., r_K) if we consider K+1 steps from 0 to K inclusive. Since there are only K possible remainders (0 to K-1), by pigeonhole, among these K+1 states, there exist i? But we are interested in whether 0 appears among r_1..r_K. Suppose we haven't seen 0 up to r_K. Then we have K states r_1..r_K all non-zero, plus r_0=0. That's K+1 states, but there are only K possible values, so there must be a repeat among r_0..r_K. Since r_0=0 and all r_i for i>=1 are non-zero (we assumed), the repeat cannot involve r_0 because r_0 is 0 and none of the others are 0. So the repeat must be among r_1..r_K. So there exists i<j with 1 ≤ i < j ≤ K such that r_i = r_j. Then by the deterministic recurrence, as argued, if 0 hasn't appeared by step j, it never will. Thus the maximum n we need to check is at most K. So indeed we only need to iterate up to K.
Thus solution is valid.
Now, let's test some small values:
K=1 -> 1
K=3 -> 7 mod3=1; 77 mod3=2? 7 mod3=1; 110+7=17 mod3=2; 210+7=27 mod3=0 -> n=3. So answer 3. Actually, 777/3=259, yes.
K=6 (even) -> should be -1. Simulate: 7%6=1; 110+7=17%6=5; 510+7=57%6=3; 3*10+7=37%6=1; cycle length 3, no 0. So -1.
K=9 -> 7%9=7; 710+7=77%9=5; 510+7=57%9=3; 310+7=37%9=1; 110+7=17%9=8; 810+7=87%9=6; 610+7=67%9=4; 410+7=47%9=2; 210+7=27%9=0? 210+7=27%9=0 -> n=9? Wait compute steps: starting n=1:7; n=2:5; n=3:3; n=4:1; n=5:8; n=6:6; n=7:4; n=8:2; n=9:0. So answer 9. Indeed 777777777/9 = 86419753? Actually, 986419753=777777777, yes. So answer 9. So K=9 is odd, not multiple of 5, works.
Thus algorithm fine.
Now, I'll produce final answer.
But the user might be expecting a C++ solution? The problem statement is from AtCoder typical. But we can provide Python.
Thus final answer: Provide code.
I'll also include some comments.
Now, the conversation: The user posted a problem description and expects a solution. I'll output the solution.
But note: The system may want the final answer to be just the code? Usually in these contexts, we give a solution with explanation. But the instructions: "You are an AI assistant. Provide answer." Possibly they expect code. I'll provide the code and maybe a brief explanation.
Given the format, I'll provide the solution in a code block.
Now, to be thorough, I'll also discuss potential pitfalls: Since K up to 1e6, using Python loops is fine.
Now, answer. ```python
import sys
def solve() -> None:
K = int(sys.stdin.readline())
remainder = 0
for i in range(1, K + 1):
remainder = (remainder * 10 + 7) % K
if remainder == 0:
print(i)
return
print(-1)
if name == "main":
solve()