We are given an integer N and an integer K. Operation: replace x with |x - K|. Can do zero or more times. Find the minimum possible value that can be obtained.
This is a classic problem from AtCoder: ABC 161 C? Actually it's similar to "ABC 117 C"? Not exactly. There's a known problem: With operation: x -> |x - K| repeatedly, the possible values are essentially N mod K? But careful: We can also get values less than K. The operation is essentially moving on the number line: subtract K and take absolute value. This is reminiscent of the process of repeatedly subtracting K and taking absolute value. Eventually the value becomes N mod K? Actually consider: if we start with N, and we do x -> |x - K|. This is like repeatedly applying f(x)=|x-K|. It tends to oscillate between two values when N >= K? Let's analyze.
We want min possible value. Since we can do zero or more operations, we can apply the operation any number of times. The process is deterministic given the sequence, but we can choose the number of operations, not the operation itself (only one operation possible). But we could also choose to stop at any time. So we can consider reachable values.
We need to find the minimum reachable value.
Let’s denote f(x)=|x-K|.
Observations: If we apply f repeatedly, we get a sequence: x0 = N, x1 = f(N), x2 = f(x1), etc.
We can also think of this as: The operation effectively computes the distance between x and K on the number line. So the value is always non-negative.
If N >= K: then |N - K| = N - K if N >= K, but that could be less than K or greater. But careful: if N >= K, then N-K is nonnegative and absolute gives N-K. So the operation becomes subtract K (if N >= K). However, after subtraction, you might get a value that could be less than K. But then subsequent operations: if x < K, then |x-K| = K - x. So it's like bouncing.
Actually, if we repeatedly apply f, the behavior is similar to taking N mod K? Let's test examples:
Example 1: N=7, K=4.
x0=7
x1=|7-4|=3
x2=|3-4|=1
x3=|1-4|=3
x4=1, etc. So we can get 1,3,1,3,... So min possible = 1.
If we consider N mod K: 7 mod 4 = 3, but min is 1. So not exactly mod.
But notice that after first step we get 3, then 1, then 3,... So the eventual minimum is min( N mod K, K - (N mod K) )? 7 mod 4 = 3, K - 3 = 1, min = 1. That matches.
Example 2: N=2, K=6. N < K, so x1=|2-6|=4; x2=|4-6|=2; x3=4; etc. So we can get 2 and 4. Minimum = 2. Compute N mod K = 2, K - N = 4, min = 2. So matches.
Third example: N=10^18, K=1. Then N mod 1 = 0, K - 0 = 1, min=0. Yes, min 0.
Seems plausible that answer is min( N % K, K - (N % K) ), but careful with N < K? Then N%K = N, and K - N%K = K - N. So min(N, K-N). That matches.
But is that always the minimum possible after any number of operations? Let's consider other cases: N=0? Then min = min(0%K=0, K-0=K) = 0. Operation on 0 gives |0-K| = K. So we can get K. But can we get back to 0? If we apply again: f(K)=|K-K|=0. So we can get 0 again. So min = 0, correct.
N=K? Then N%K =0, K -0 = K, min = 0. But we can get 0? Starting at K, f(K)=0, so yes min 0. So formula works.
But we need to check if there is any possibility to get lower than min(N%K, K - N%K). Could we get something like using multiple steps that leads to a value less than both? Suppose N=10, K=6. N%K=4, K-4=2, min=2. Sequence: 10->4 (since 10-6=4)->2 (|4-6|=2)->4->2... So yes min=2. Could we get 0? 2->|2-6|=4, 4->2, never 0. So not.
N=12, K=8: N%K=4, K-4=4, min=4. Sequence: 12->4 (12-8=4) ->4? Actually |4-8|=4, so stuck at 4. So min=4.
N=9, K=5: N%K=4, K-4=1, min=1. Sequence: 9->4->1->4->1... min 1.
N=17, K=9: 17%9=8, K-8=1, min=1. Sequence: 17-9=8->|8-9|=1->|1-9|=8->1... min1.
Seems consistent.
But wait, what if we can apply the operation an even number of times to get a different residue? Actually the process seems to converge to a 2-cycle between a and K-a for some a in [0, floor(K/2)]. The value after many steps is essentially N modulo K but also taking the complement relative to K.
More formally, consider the transformation as moving in the group? Equivalent to repeatedly applying x -> |x - K|. This can be thought as: x -> (x+K) mod (2K) but with absolute? Alternatively, we can see that if we define x' = x mod K, but also consider the distance to K. Actually, after first operation if N>=K, we subtract K, which yields N-K. That's same as N mod K? No: if N >= K, N-K might be less than K, and it's not necessarily N mod K because N mod K = N - floor(N/K)*K. If we subtract K only once, we get N-K, which may be >K? Actually if N>=K, N-K could still be >=K if N>=2K. For example N=10, K=4: 10-4=6 >=4. So after one operation, x=6, not less than K. Then next: 6-4=2, now < K. So after sufficiently many subtractions, you'll reduce to N mod K? Actually if we only subtract when x>=K, then repeatedly subtracting K is like Euclidean algorithm to get remainder. But here the operation is absolute difference, not subtraction. If x >= K, then f(x)=x-K. So we can subtract K as many times as we want until we get a value less than K. But note that we could also do other moves: When we are less than K, then f(x)=K-x, which gives a value that could be less than K or maybe >K? Actually K-x is positive and less than K if x>0. So it stays less than K. So once we get into [0, K], we bounce between two numbers: r and K-r, where r = current value (0 <= r < K). If r=0, then f(0)=K, which is >=K, so then from K we go to 0, so it's a cycle 0<->K. But note that if we go from something < K to K - r, that is also < K (since r>0 => K-r < K). So the process once in [0,K) stays in [0,K]? Actually if r=0, then f(0)=K which is >=K, then f(K)=0, so we can have K appear but K is equal to K. So essentially values are within [0, K] but could be exactly K. But K is >=1. So the state space is nonnegative integers.
Thus, the process: Starting from N, we can repeatedly subtract K as long as current value >= K. That is allowed because if x>=K, f(x)=x-K. So we can effectively perform floor division: we can reduce N to N mod K by repeatedly applying the operation (subtracting K each time) until we get a value < K. But careful: After each subtraction, we get a new number; we can decide to stop or continue. So certainly we can achieve N mod K (the remainder after dividing by K) by applying the operation floor(N/K) times? Actually each operation subtracts K only when the current value is >=K. Starting from N, if we keep applying f, the sequence will be: N, N-K, N-2K, ..., until it becomes r where 0 <= r < K. But is that always true? Let's test with N=10, K=4: N=10 >=4 => 10-4=6; 6>=4 => 6-4=2; 2<4 => next operation would be |2-4|=2? Actually if we continue, we would get 2 -> |2-4|=2? Wait, 2<4, so f(2)=|2-4|=2? No, |2-4|=2? 4-2=2, yes it's 2. So after we hit 2, applying f gives 2 again? Actually 2<4 => |2-4| = 2? 4-2=2, so it's 2, which is same as original. So we get stuck at 2? But earlier we thought we get 2->4? Let's recalc: For x=2, K=4: |2-4| = 2? Wait absolute difference between 2 and 4 is 2, correct. So f(2)=2. That means 2 is a fixed point? That can't be right because |2-4| = 2 indeed. So if we get to 2, the next operation yields 2 again, not 4. Let's check: |2-4| = | -2 | = 2, yes. So indeed 2 is a fixed point. But earlier I said 2 -> 4 -> 2? Let's recalc: 2 -> |2-4| = 2. So it doesn't change. So I was mistaken. In example 2: N=2, K=6 gave sequence: 2->|2-6|=4, 4->|4-6|=2, 2->4, etc. So that's because K=6, 2<6 => |2-6|=4, which is >? 4<6, so 4->|4-6|=2. So that cycles between 2 and 4. Why is 2 not fixed for K=6? Because |2-6| = 4, not 2. Wait, I must check: |2-6| = 4, correct. So for K=4, |2-4| = 2, because 4-2=2. So the difference is that if K is even, a number equal to K/2 yields fixed point? Because |K/2 - K| = K/2. Indeed if x = K/2, then f(x) = |K/2 - K| = K/2. So K/2 is fixed. So that matches.
Thus, after we subtract K enough times to get r = N mod K, we might then have a value r (0 <= r < K). Then if we apply f on r, we get either r-K? No, if r < K, f(r)=K-r, because |r-K| = K-r (since K>=r). So we get K-r. Then if we apply f on K-r: if K-r >= K? Typically K-r <= K. If K-r = K, then r=0, then f(K)=0. Otherwise K-r < K? Actually if r>0, then K-r < K. So then f(K-r)=|(K-r)-K| = | -r | = r. So we get back to r. So the system will oscillate between r and K-r, unless r = K-r (i.e., r = K/2) which is a fixed point, or r=0 which leads to 0<->K cycle (but 0 and K are distinct; but from 0 we get K, from K we get 0; so it's a 2-cycle, but 0 is minimal). So reachable values from N include r (obtained by subtracting K repeatedly) and K-r (by one more operation) and also possibly further? But after we have r and K-r, further operations just go between them. So the minimum reachable is min(r, K-r). However, is it possible to get a value even smaller by not going directly to r? For instance, what if we intentionally not go all the way to remainder, but stop at some intermediate value and then do something? Since we can apply any number of operations, we can generate a sequence of values; but all values are reachable? Let's analyze the state space more thoroughly.
Define operation f(x)=|x-K|. The key is that this operation is linear mod something? Actually, consider representing x in terms of its distance to multiples of K. There's a known result: The process eventually cycles between two numbers: x mod K and K - (x mod K), unless x mod K = 0 then eventually you get 0 and K, or x mod K = K/2 then fixed. So the minimal reachable is min(x mod K, K - (x mod K)).
But is it always reachable? Yes, we can get r = N mod K by repeated subtractions. Then from r, we can get K-r by one operation (if r != 0). So min(r, K-r) is reachable. Could there be a path to a number even smaller than that? That would require some number less than both r and K-r. Since r and K-r are complementary and sum to K, the smaller is at most floor(K/2). So any smaller number would be less than that. But can we get, say, some number s < min(r, K-r)? To get s, we might need to do some combination of subtractions and absolute differences. However, note that the operation essentially reduces the value mod 2K? Another approach: consider the mapping on the integer line modulo 2K? Because f(x) = |x-K| can be seen as x' = (x+K) mod (2K), but then if the result is >=K, subtract? Actually, note that for any integer x, define y = x mod (2K) in the range [0, 2K). Then f(x) corresponds to? Let's test: If x mod (2K) is between 0 and K inclusive? Hmm.
We can think of reflecting x around the point K/2? Actually f(x) = distance from x to K. It's similar to folding the number line at K. Repeatedly applying f corresponds to bouncing between 0 and K. Equivalent to taking x modulo (2K) but then mapping to the distance from the nearest multiple of K? Not exactly.
Alternatively, consider that f(x) = min( x mod K, K - (x mod K) )? Actually, if we take x mod K, but also if we consider x modulo (2K): For any x, define r = x % K, and also consider whether floor(x/K) is even or odd? Because subtracting K flips parity? Let's simulate: Starting from N, we can represent N as a = q*K + r, with 0 <= r < K. If q is even? Not relevant because operation f changes q.
Better: Observe that after each operation, the value always lies in [0, K] except possibly when we have K itself (which is exactly K). Actually, if x >= K, then f(x) = x - K. So if x >= 2K, then x-K >= K, and we could still have >=K. So values can be >=K for many steps until they become < K. So the state can be any nonnegative integer, but the dynamics eventually bring it into [0, K] after enough steps (since subtract K repeatedly reduces it until it falls below K). Once it's in [0, K], it stays in [0, K] (or possibly K, which is at the boundary). Actually careful: Suppose x is in (0, K). Then f(x) = K - x, which is also in (0, K] (if x>0, K-x < K; if x=0, f(0)=K; if x=K, f(K)=0). So from (0,K) we get another number in (0,K). So indeed after the first time we drop below K (including possibly hitting exactly K), we will remain in [0, K] forever. So the eventual minimal value must be in [0, K]. So we can think that we can reduce N to some value in [0, K] by repeatedly subtracting K (i.e., using the operation when N>=K). Then we can further apply operations that bounce within [0, K] to get other numbers. So the reachable set from N includes all numbers that can be obtained by starting from N, doing some operations, but we can decide when to stop. The question: what is the minimum possible value among reachable numbers?
Since after we are in [0, K] we can only get either the current r or K - r (if we apply operation). But can we get numbers other than these two? Suppose we are at r in [0,K]. Then one operation gives either: if r <= K, f(r) = K - r, which is also in [0,K]. Then applying again gives f(K - r) = |(K - r) - K| = r. So the orbit is {r, K - r}. So from r we only can get r and K - r. So if we want to get a different number s, we would need to leave the [0, K] region? But once we are in [0, K], we cannot leave? Actually, if r = 0, f(0) = K, which is >=K. Then from K, f(K) = 0, so we bounce between 0 and K. K is in [0, K]? K is at boundary, but it's equal to K, so it's allowed. But from K, we could also consider doing something else? There's only one operation. So the orbit is {0, K}. So still two numbers. If r = K, similarly. So from within [0, K], we only get at most two values.
But could we have a scenario where we do not go all the way to the remainder r but stop at some intermediate value that is > K, then apply operation to get something else? For instance, suppose we subtract K a few times but not until < K, and then at some value x >= K, we might have f(x) = x - K (since x>=K). That just continues subtracting. So the only alternative to the simple subtract-K process is when we encounter a value x < K, we get K - x, which might be > K? Actually if x < K, then K - x > 0 and could be > K? No, because x >= 0 => K - x <= K; if x is negative? No, x is nonnegative. So K - x is <= K. So it's <= K. So from x < K, we get a number <= K. So we might go from x < K to a number that is exactly K if x=0, otherwise less than K. So we never go from a number < K to a number > K (except 0 -> K, which is exactly K). So indeed once we get below K, we stay at or below K.
But maybe we could start with N, do a series that includes both subtractions and reflections before fully reducing below K. For example, N = 10, K = 6. Standard route: 10 -> subtract 6 = 4 (since 10>=6 => 10-6=4) -> 4 < 6, so next operation gives 2 (|4-6|=2) -> 2 -> 4, etc. So reachable: 10,4,2. So min 2.
But could we do something else? Suppose we do: 10 -> |10-6| =4, but before that we can't do anything else. Only one operation per step. There is no choice: it's deterministic given x. So the process is deterministic? Actually the operation is defined: replace x with |x - K|. There's no alternative; we must apply that transformation. So the sequence of values from a given starting N is deterministic: x0 = N, x1 = f(N), x2 = f(x1), ... So it's not that we can choose different operations; we only can choose when to stop. So the reachable set is exactly the set of values that appear in that deterministic sequence. So we cannot choose to not subtract K when x>=K; the operation forces subtraction. So the process is fully determined.
Thus the reachable values are those in the orbit of N under f. So we need to find min over the orbit.
So we just need to compute the eventual cycle and find the minimum. As argued, eventually the orbit becomes periodic with period at most 2 (except maybe fixed points). So the minimum is simply the smaller of the two numbers in the cycle, which is determined by the remainder when N is reduced modulo K.
But careful: It's not exactly remainder after one division; after many steps, the value may be something else. Actually, the process can be described by:
Let a0 = N.
For i >= 0:
if a_i >= K: a_{i+1} = a_i - K
else: a_{i+1} = K - a_i.
Thus, we can think of it as: while a_i >= K, subtract K. Once a_i < K, then we toggle between a_i and K - a_i.
But note: The subtraction step can be applied repeatedly while >=K. So essentially we can consider the process: we keep subtracting K until we get a value r in [0, K). The number of subtractions is the integer part floor(N/K)? But careful: If N is large, we subtract K each time until we get below K. However, what if during subtraction we cross from exactly K to 0? Actually if we hit exactly K, then a_i = K, then since a_i >= K, next is a_i - K = 0. So we get 0. Then 0 < K, so next is K - 0 = K. So we get a 2-cycle 0,K. So that's fine.
Thus, after sufficiently many steps, the value becomes r = N mod K? Let's check: If N = q*K + r, with 0 <= r < K. Then by subtracting K q times, we get r. But is that the actual sequence? Because each subtraction is applied only if current value >= K. Starting from N, since N >= K (if q>0), first subtract gives N-K = (q-1)K + r. Continue subtract as long as >=K. After q steps, we get r. But is there any possibility that after some subtractions we might get a value that is not simply N - tK? No, because while >=K, f(x) = x - K exactly. So the sequence from step 0 to step q is exactly N, N-K, N-2K, ..., N - qK = r. So indeed we will reach r at step q.
Now from r, we have r < K (except possibly r = 0? but r=0 is <K). So next operation gives f(r)=|r-K| = K - r (since K > r). So a_{q+1} = K - r. Then a_{q+2} = f(K - r). Now check if K - r >= K? If r > 0, then K - r < K, so it's less than K, so f(K - r) = K - (K - r) = r. So we get back to r. So indeed after q+1 steps, we have either r (if we stop at step q) or K - r (if we stop at step q+1). After that, we cycle between r and K - r. So the reachable values are all the numbers encountered: N, N-K, ..., r, K-r, r, K-r,... So the minimum among these is min( min over N - iK for i=0..q, r, K-r). But N - iK are all >= r (since r is the remainder, the smallest among them is r). Actually N - iK for i from 0 to q: the smallest is r (when i=q). So the overall minimum is min(r, K-r). Also possibly 0 if r=0, then K-r = K, so min is 0. So answer is min(r, K - r) with r = N mod K.
But is there any scenario where during the subtraction steps we might get a value lower than r? No, because r is the remainder, the final one after subtracting as many K's as possible. Since we subtract exactly K each time, we get decreasing values; the smallest we get before hitting less than K is exactly r. Could we ever get a value less than r by doing something else? The process forces subtraction while >=K; we cannot do reflection while >=K because reflection only occurs when x<K. So before we hit r, all values are >=K? Actually N - iK might be >= K for i < q. For i = q-1, we get N - (q-1)K = K + r. If r > 0, then that value is > K? Actually K + r > K because r>0, so it's >= K? Yes, it's > K if r>0, so the next step will subtract K again, giving r. If r = 0, then at i = q-1 we get K, which is = K, so next step subtracts K to get 0. So in either case, we never get a value below r before reaching r. So the minimum is indeed min(r, K - r).
But wait, could K - r be less than r? That would be the case if r > K/2. So answer is min(N mod K, K - (N mod K)).
Edge case: N = 0? Then N mod K = 0, min(0, K) = 0. Works.
N < K: Then r = N, and we can also get K - N. So answer min(N, K-N). Works.
But what about K = 0? Constraint says K >= 1, so fine.
Thus the solution: compute r = N % K, answer = min(r, K - r).
But careful: when r = 0? Then K - r = K, min(0,K)=0. That's fine.
But is it always reachable? Yes, we can stop at step q (giving r) or step q+1 (giving K - r). So we can achieve the smaller of the two. If r <= K/2, then r is the minimum, we can stop at step q. If r > K/2, then K - r < r, so we can stop at step q+1. So answer is correct.
But is there any nuance with large numbers? N and K up to 1e18, which fits in 64-bit integer. We just compute remainder and compare.
Thus output min(N%K, K - N%K).
But wait, consider N huge and K huge, but N%K might be zero? fine.
But check a case: N = 10^18, K = 3. r=1, K-r=2, min=1. So answer=1.
But is there any possibility to get 0? Let's see: N = 10^18 (even). Subtract K repeatedly: after many steps, we get remainder 1, then from 1 we get 2, from 2 get 1, etc. So never 0. So min=1 correct.
Alright.
But the problem statement: "Given any integer x, Aoki can do the operation below. Operation: Replace x with the absolute difference of x and K. You are given the initial value of an integer N. Find the minimum possible value taken by N after Aoki does the operation zero or more times." This is exactly AtCoder ABC 179 C? Actually it's ABC 161 C - "Replacing Integer". I recall a problem: "Replacing Integer" from AtCoder Beginner Contest 161 C: Given N and K, find minimum possible value after repeatedly replacing N with |N-K|. The answer is min(N % K, K - N % K). Yes, that's the well-known solution.
Thus we can implement.
But wait, is there any nuance when N is extremely large and K is 1? N%1=0, K-0=1, min=0. That's fine.
Now, check another edge: N = 10^18, K = 10^18? Then N%K = 0 because N=K? Actually N=10^18, K=10^18: N%K = 0, min(0,10^18)=0. So answer 0. Starting from N=K, we can get 0. So correct.
What about N = 10^18, K = 10^18-1? Then N mod K = 1 (since 10^18 = (10^18-1)+1), r=1, K-r = 10^18-2? Actually K - r = (10^18-1)-1 = 10^18-2. min=1. But can we get 0? Sequence: subtract K: 10^18 -> 1 (since 10^18 - (10^18-1) = 1). Then from 1, get (K-1) = 10^18-2, then from that, get |(10^18-2) - K| = |(10^18-2)-(10^18-1)| = 1, so cycle between 1 and 10^18-2, min=1. So yes.
Thus answer stands.
Now we need to implement. Input: N K. Compute r = N % K. ans = min(r, K - r). Print ans.
But we must be careful with data types: up to 1e18, use 64-bit (long long in C++). Python can handle big ints.
Potential pitfalls: K could be 0? Not allowed. Also if r = 0 then K - r = K, and min(0,K)=0, fine.
If K is huge, say K > N? Then r = N, K - r = K - N, which could be > N? So min(N, K-N). That's fine.
One more nuance: When N is extremely large, computing N % K is fine.
Now I'd like to double-check the process for N multiple of K: e.g., N = 2K. Then r = 0, we get answer 0. Sequence: N=2K -> subtract K: K -> subtract K: 0 -> 0 -> K? Actually from K, next is 0; from 0, next is K. So we reach 0. So min=0.
All good.
Thus solution is straightforward.
Now maybe there's a more tricky scenario: Because operation is defined as replace x with |x - K|. But if we apply it many times, we can eventually get a value less than K, but could we get a value that is not simply r or K-r? For example, N = 8, K = 3. N mod K = 2, K - 2 = 1. min=1. Sequence: 8->5 (8-3) ->2 (5-3) ->1 (|2-3|) ->2 ->1... So reachable: 8,5,2,1. min=1. Good.
N = 11, K = 3: 11 mod 3 = 2, K - 2 = 1. min=1. Sequence: 11->8->5->2->1->2... min=1.
N = 12, K=3: mod 0 -> min 0. Sequence:12->9->6->3->0->3... min 0.
All matches.
So answer is indeed min(N % K, K - N % K).
But I recall that the original AtCoder problem had the same input format and constraints. So this is the solution.
Thus we can implement.
Edge Cases: K = 1? Already handled.
Now, what if K is 0? Not allowed.
Now implementation details:
Read N and K as integers. Compute r = N % K. Compute ans = min(r, K - r). Print ans.
But be careful: when r = 0, K - r = K, but min(0, K) = 0. So that's fine.
Potential overflow: In C++ using long long, K - r could be computed safely as K - r, both up to 1e18, result up to 1e18, which fits in long long (max 9.22e18). So fine.
In Python, no issue.
Thus final answer.
But maybe there is an alternative nuance: The problem statement: "Given any integer x, Aoki can do the operation below. Operation: Replace x with the absolute difference of x and K." It says "Aoki can do the operation", implying he can choose whether to apply it at each step? Actually "can do" meaning he may perform the operation zero or more times. So he can decide to stop at any point. So indeed we can stop at any step. So we choose the minimum along the deterministic sequence. So we just computed the minimum among all values in the orbit.
But is there any possibility that he can apply the operation to any integer x? The wording: "Given any integer x, Aoki can do the operation below. Operation: Replace x with the absolute difference of x and K." I think it's like: At each step, he can replace the current integer with its absolute difference with K. So it's a single operation applied to the current value. So deterministic.
Thus done.
Now we need to output the minimum possible value.
Given examples:
Input: 7 4 -> 7%4=3, K-3=1, min=1 -> output 1.
Input: 2 6 -> 2%6=2, K-2=4, min=2 -> output 2.
Input: 1000000000000000000 1 -> 0, output 0.
All match.
Thus final code: read N K; r = N % K; ans = min(r, K - r); print ans.
Now I should consider if there is a special case when K = 0? Not needed.
Potential: When K is huge, N % K = N if N < K. So ans = min(N, K - N). If K - N could be negative? No, because N <= K, so K - N >= 0. So fine.
Alright.
Now I'm done. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
N = int(data[0])
K = int(data[1])
r = N % K
ans = min(r, K - r)
print(ans)
if name == "main":
solve()