We need to find minimum number of bad cells visited on any path from (x1,y1) to (x2,y2) moving in 4-direction grid (Manhattan moves). The start and end are not bad.
We need to minimize count of bad squares visited, not the path length. Essentially, we can take arbitrary long paths to avoid bad squares, but we might be forced to go through some.
We need to understand the structure of bad squares: (x,y) is bad if either condition holds:
(1) |x+y| ≡ 0 mod 2a
(2) |x-y| ≡ 0 mod 2b
Note: absolute values. So we can consider coordinates transformed: u = x+y, v = x-y. Then conditions: u ≡ 0 (mod 2a) OR v ≡ 0 (mod 2b). But note: u and v can be negative. The condition uses absolute values: |u| ≡ 0 mod 2a means u is a multiple of 2a (including negative multiples). So we can treat as u ≡ 0 (mod 2a) as integer multiple. Similarly v ≡ 0 (mod 2b). So bad squares are those where either u ≡ 0 (mod 2a) or v ≡ 0 (mod 2b).
Thus the grid lines (in terms of u, v) that are multiples of 2a or 2b define forbidden lines. But the squares are discrete lattice points. So we need to count minimum number of lattice points that lie on these forbidden lines that we must cross when going from start to end.
Because movement is Manhattan (4 directions), we can think of the path as a sequence of points with each step changing either x or y by ±1. That changes u = x+y and v = x-y accordingly:
- Move right: (x+1,y) => u+1, v+1.
- Move left: (x-1,y) => u-1, v-1.
- Move up: (x,y+1) => u+1, v-1.
- Move down: (x,y-1) => u-1, v+1.
So each step changes both u and v by ±1, but with a correlation: the parity of changes: u and v change simultaneously by either (+1,+1), (-1,-1), (+1,-1), (-1,+1). So u+v = 2x, u-v = 2y. So u and v have same parity as (x+y) and (x-y) both have same parity as (x+y) and (x-y). Actually note: u and v are either both even or both odd? Since x and y integers, x+y and x-y have same parity: (x+y)+(x-y)=2x even, so sum even, so both have same parity. Indeed, u mod 2 = v mod 2 = (x+y) mod 2. So all reachable points maintain parity relation: u ≡ v (mod 2). So the state space consists of points (u, v) where u and v have same parity. But we don't need to consider parity separately because the grid coordinates already enforce that.
Now, we need to find a path from (u1,v1) to (u2,v2) (with u1 = x1+y1, v1 = x1-y1; similarly u2, v2) such that each step changes u and v by ±1 (with correlated signs) and we want to minimize number of steps where the point (u,v) is such that u ≡ 0 mod 2a OR v ≡ 0 mod 2b. Note that the start and end are not bad, so u1 ≠ 0 mod 2a and v1 ≠ 0 mod 2b; same for end.
This is reminiscent of problem of avoiding lines in integer lattice. Possibly we can treat it as a shortest path problem in an infinite grid with costs 1 for bad squares, 0 otherwise, but we need minimum number of bad squares visited (not total steps). However, since we can take arbitrarily long paths, we can essentially try to avoid bad squares as much as possible. But there may be obstacles (lines) that separate the plane into regions; to get from start to finish, we might have to cross certain lines, each crossing may force visiting a bad square if we cross at a point where the line is bad. However, can we cross a line without stepping on a bad square? For example, the condition u ≡ 0 mod 2a defines a set of vertical lines in the (u,v) space? Actually in (u,v) coordinates, condition u = multiple of 2a is a vertical line (since v can be anything). But the moves are such that each step changes u by ±1, so to go from u < 0 to u > 0, we must step through u=0. But we might be able to cross the line at a point where u = 0 mod 2a but v is such that the square is not bad because v not 0 mod 2b? Wait: condition is OR: if u ≡ 0 mod 2a, it's bad regardless of v. So any point with u ≡ 0 mod 2a is bad. So crossing that line inevitably steps onto a square with u ≡ 0 mod 2a at some step (since u changes by ±1, it will hit exactly that multiple if we cross from below to above). However, we might cross at a point where u is exactly multiple, but we could perhaps avoid crossing by moving around? But lines are infinite, and to get from one side to the other you must cross the line somewhere, unless you can go around through infinity? Since the grid is infinite, but the lines extend infinitely, there is no way to avoid crossing a line if the start and end are on opposite sides of it. However, because the condition uses absolute values |u| ≡ 0 mod 2a, the condition is symmetric: if u = 2a * k, it's bad for any integer k (including negative). So the lines are at u = ... -4a, -2a, 0, 2a, 4a,... Similarly v = ... -4b, -2b, 0, 2b, 4b,...
Thus the plane is partitioned into rectangles by these lines. The start and end lie in some cells (regions). To go from one region to another, you may need to cross some of these lines. Each crossing forces you to step on a square exactly on that line (since you move unit steps). However, is it possible to cross a line without stepping on a bad square if the crossing occurs at a point where the other condition also holds? That doesn't matter: if u ≡ 0 mod 2a, it's bad regardless. So any crossing of a u-multiple line forces at least one bad square. Similarly for v.
But wait: maybe you can cross at a point that lies on both a u-line and a v-line, then it's still bad but might count as only one bad square even though you cross both lines simultaneously? That is an interesting nuance: If you cross a u-line and a v-line at the same step (i.e., you move from a point where u is just below a multiple and v just below a multiple to a point where both cross, e.g., u from 2a-1 to 2a and v from 2b-1 to 2b in one step? But a step changes both u and v by ±1, so it's impossible to change both u and v by more than 1. So crossing a u-line means you step from u = 2a-1 to u = 2a (or from -2a+1 to -2a etc). At that moment, the new point has u = 2a (multiple). That point may also have v being a multiple of 2b or not. If it is, it's still a bad square, counted as one. If not, it's still bad because of u condition. So crossing a u-line always results in at least one bad square (the point on the line). Similarly crossing a v-line. If you cross both a u-line and a v-line at the same step, that would require moving from a point where u = 2a-1 and v = 2b-1 to u=2a, v=2b. That step changes u by +1 and v by +1, which is allowed (right move). Then the new point is both u multiple and v multiple; it's bad, but it's just one bad square. So crossing two lines simultaneously might count as one bad square, instead of two if crossed separately.
Therefore, the problem reduces to: Starting from (u1,v1) not satisfying either condition, ending at (u2,v2) also not satisfying. We need to plan a sequence of moves (unit changes) such that we minimize the count of visited points that satisfy either condition. Essentially, we want to minimize the number of times we cross the forbidden lines, where crossing two lines at the same time costs 1, crossing one line costs 1. But careful: if we cross a line and later recross it, we may incur additional costs. We want minimal number of distinct bad squares visited, but note that we could possibly revisit a bad square; but minimizing number of bad squares visited likely means we avoid revisiting, but the minimum path would avoid unnecessary returns. So essentially the minimal number of line crossings needed, with possibility of merging crossings at intersections.
But also we could potentially cross a line without stepping on it? For example, suppose we want to go from u < 0 to u > 0. Since u changes by exactly 1 each step, you must pass through u=0. However, if a > 1, the line is u=0 mod 2a, not all integer values. Wait, condition: |u| ≡ 0 mod 2a. That means u is a multiple of 2a, not all integers. So the lines are spaced 2a apart. So to go from a region between two such lines, crossing from u = k*2a + something to u = (k+1)*2a + something, you must step through the value u = (k+1)*2a exactly? Actually, if you start at u1, not exactly on a multiple, and you want to change u by increments of 1, you will eventually hit the integer value that is exactly a multiple of 2a if the interval between u1 and u2 includes that integer. For example, a=2, so multiples of 4: ... -4,0,4,8,... To go from u=1 to u=5, you must go through u=2,3,4,5. u=4 is a multiple, so you must step on u=4 at some point. But can you avoid stepping on u=4? To go from 1 to 5, the sequence of u values you'll traverse includes all integers from 1 to 5 if you move monotonically. If you go the other direction, you might circumvent by going around? But u is determined by your path; you could go, say, from u=1 to u=2 to u=3 to u=4 to u=5; you could also go from u=1 to u=2 to u=3 to u=4 to u=5; no alternative: to increase u from 1 to 5, you must pass through 2,3,4,5. Because each step changes u by ±1, so the set of u values visited is a connected integer interval between the min and max. Similarly, if you go from u=1 to u=5 but you also change u down and then up, you might avoid u=4? For example, go 1->2->3->2->3->4->5? That still hits 4. Any path that ends at 5 must have some step that increments u from 4 to 5, so you must visit a point with u=4 or 5? Actually to go from 1 to 5, the maximum u visited is 5, minimum maybe less, but the set of u values visited includes all integers between the min and max? Not necessarily if you go down and up, the visited u values might skip some? For example, start at u=1, go down to u=0, then up to u=5. The values visited: 1,0,1,2,3,4,5? Wait, careful: each step changes u by exactly 1, so to go from 0 to 5, you must go through 1,2,3,4,5. So you cannot skip integers. The set of u values visited is a set of integers; if you ever go from a lower value to a higher value, you must pass through all intermediate integers at some point. However, if you go down first, you might avoid some intermediate? Suppose start at 1, go to 0, then to -1, then to ... eventually come back up to 5. The range of u from -1 to 5 includes 4, etc. So any integer between the minimum and maximum u visited will appear at some point? Not strictly: If you go from 1 to 0 to -1 to 0 to 1 to 2 to 3 to 4 to 5, then you visited -1,0,1,2,3,4,5. If you go from 1 to 0 to -1 to 1? Can't jump from -1 to 1 because step change is 1, so you must go through 0. So indeed, if you visit two numbers a and b, and you move stepwise, you will visit every integer between the min and max at least once if the path is a continuous walk? Actually a walk on integers where each step changes by ±1; the set of visited positions is not necessarily the whole interval, but if you go from a to b, the walk is a sequence of integers; each step is ±1. To go from a to b, the sequence must contain all integers between a and b if you move monotonically. If you go non-monotonically, you could visit numbers outside the interval, but the interval between the extremes might still be covered? For example, start at 1, go to 2, then back to 1, then to 0, then to 5? Impossible: from 0 to 5 you need to go through 1,2,3,4,5. So indeed any time you cross from a value less than M to a value greater than M, you will at some point be exactly M. Because the walk is continuous in integers, the first time you exceed M, you must go from M to M+1, so you must have been at M. More formally, by discrete intermediate value property, if you have a walk from u_start to u_end, and there exists an integer N between them (strictly between), then either the walk visits N at some point, or the walk might skip? Since steps are ±1, you cannot skip N if you go from below N to above N without hitting N because you would need to jump from N-1 to N+1, which is not allowed. So yes, you must hit N. So if you need to change u from u1 to u2, and there is a multiple of 2a between them, you will inevitably step on that multiple at some point, no matter how you wiggle, unless you can cross the line at a different multiple? But the multiples are spaced 2a apart. If the interval from u1 to u2 contains a multiple of 2a, then any path must visit that multiple. However, if u1 and u2 are on the same side of that multiple but you need to go around some region? But to get from one region to another, you might cross a different set of multiples. For instance, if you go around the world? But u is unbounded; you could go the other direction: e.g., start at u=1, end at u=5 (a=2, multiples: ..., -4,0,4,8). The interval [1,5] contains 4, so you must hit 4. But you could also go from u=1 downwards to u=-3 and then up to 5. The interval from -3 to 5 contains -4? Actually -4 is less than -3? The range would be from -3 to 5, which includes 4 but also -4? -4 is less than -3, so not necessarily. You could avoid hitting 4 by going far negative? Let's test: start u=1, go down: 1->0->-1->-2->-3->-4? That hits -4, which is a multiple. To avoid any multiple, you need to avoid all multiples of 4. Could you go from 1 to 5 without ever hitting a multiple of 4? If you go down to -2, then up to 5: the path from -2 to 5 will go through -1,0,1,2,3,4,5. 4 is hit. If you go down to -6, then up: from -6 to 5 goes through -5,-4,-3,-2,-1,0,1,2,3,4,5. So you hit -4 and 4. So it seems impossible to avoid hitting at least one multiple of 4 if you need to cross from one side to the other? Actually, consider that multiples of 4 are spaced 4 apart. The parity? The start and end both have same parity? u1 and u2 have same parity because they both equal x+y. But multiples of 4 are even. However u1 and u2 could both be odd, say 1 and 5. The interval from 1 to 5 contains 2,3,4. So you must hit 4. What if both are even but not multiples? For a=2, multiples of 4. u1=2, u2=6. The interval [2,6] contains 4. So you hit 4. u1=2, u2=10? interval contains 4,6,8? includes 4 and 8 (multiples). So you hit at least one. So essentially, if u1 and u2 are not congruent modulo 2a? Let's think more generally.
Define U = u mod (2a). Since u1 and u2 are not on the forbidden line (i.e., u mod 2a ≠ 0). But they could be in the same residue class or different. To go from u1 to u2, the walk will traverse a set of integers. The number of times you cross a multiple of 2a (i.e., step onto a point with u ≡ 0 mod 2a) is at least something related to the difference in the "floor" index of the multiples. Specifically, if we define k1 = floor(u1/(2a))? But careful with negatives. Perhaps we can think of the "signed number of multiples" between u1 and u2. The minimal number of times you must hit a u-multiple is the minimal number of boundaries you need to cross. In a one-dimensional walk, if you have to go from position u1 to u2, the minimal number of times you hit a set of points (like multiples) is at least the number of multiples that lie strictly between u1 and u2 if you go straight, but you could potentially cross the same multiple multiple times? But to minimize count, you'd want to hit each multiple at most once, and avoid extra. But could you avoid hitting a multiple that lies between u1 and u2 by going the other way around the circle? Since the line is infinite, you could go in the opposite direction and cross a different set of multiples. For example, suppose a=2, multiples of 4. u1=1, u2=3. The interval [1,3] contains 2,3? Actually multiples: 0,4,... The interval between 1 and 3 does not contain any multiple of 4. So you could go directly without hitting a multiple. But if u1=1, u2=5, the direct interval contains 4, but you could also go from 1 down to -∞ and then up to 5. But that would cross many multiples. However, you could go from 1 to 2 to 3 to 4 to 5 (crosses 4) — that's one multiple. Could you avoid 4 by going, say, 1->2->3->2->1->0-> -1 -> -2 -> -3 -> -2 -> -1 -> 0 -> 1 -> 2 -> 3 -> 4 ->5? That still hits 4. To avoid 4, you need to never have u=4. But the final u is 5. To reach 5, at some step you must go from 4 to 5 (or from 6 to 5, but to get to 6 you'd have to go through 5?). Actually you could approach 5 from above: e.g., go from 1 up to 6 then down to 5. But to get to 6, you need to cross 5,6? Starting at 1, to get to 6, you'd go 1->2->3->4->5->6. So you'd hit 4 and 5. If you go down first: 1->0->-1->...->-something->...->6? Let's try: 1 -> 0 -> -1 -> -2 -> -3 -> -4 (multiple) -> -5 -> -6 ... then eventually go up to 6. You would cross many multiples. So any path will cross at least one multiple? But could there be a path that goes from 1 to 5 without ever hitting a multiple of 4? Let's consider parity: u changes by ±1 each step, so parity flips each step? Actually u changes by ±1, so parity alternates each step. Starting at 1 (odd), after odd number of steps parity flips to even, after even steps stays odd. Since 5 is odd, number of steps parity? Not relevant. But to go from 1 to 5, you need to increase by 4. You could increase by 2, then decrease by something, then increase more, but the net change is 4. Could you end at 5 without ever being at 4? Suppose you go 1->2->3->2->3->4? Actually 4 is even. If you never land on an even number? Starting at odd, after 1 step you'll be even (since change ±1). So after the first step you are even. But 4 is even, but you might be at 2 or 0 etc. Could you avoid being exactly 4? Possibly if you go from 3 to 5 directly? Not allowed because step size is 1. You need to go from some number to 5, the previous step must be 4 or 6. So if you approach 5 from below, you must be at 4. If you approach from above, you must be at 6. To be at 6, you must have been at 5 or 7. To be at 6, you need to come from 5 or 7. If you come from 7, then you must have gotten to 7, which likely involved 6, etc. It seems that to reach 5, you will necessarily hit either 4 or 6. But 6 is not a multiple of 4? 6 mod 4 = 2, not 0. So maybe you can avoid 4 by approaching 5 from above: go to 6 then down to 5. Can you get to 6 without hitting 4? Starting at 1, you need to get to 6. That's an increase of 5. You could go 1->2->3->4->5->6 (hits 4). Or 1->2->3->2->3->4->5->6 (still hits 4). Or go down first: 1->0->-1->0->1->2->3->4->5->6 (hits 4). Could we get to 6 via negative then large positive: 1->0->-1->-2->-3->-4 (hit multiple) -> ... Actually -4 is a multiple. So you'd hit a multiple. Maybe it's impossible to avoid any multiple when net displacement is 4? Let's think more abstractly: The set of forbidden u-values is all multiples of 2a. We need to travel from u1 to u2 on the integer line with steps of ±1, and we want to minimize the number of times we land on a forbidden value. This is a one-dimensional problem: given integers u1, u2 not multiples of 2a, find the minimal number of times we must visit a multiple of 2a.
If we can go in either direction arbitrarily far, the minimal number of times we hit a multiple is either 0, 1, or maybe 2? Let's analyze.
Define the "distance" between u1 and u2 in terms of the number of multiples between them if we take the shortest path (i.e., the interval from u1 to u2). But we can also go the other way around the number circle if we consider modulo 2a? Since the line is infinite, but multiples repeat every 2a. The set of multiples partitions the integers into intervals. For a given direction, the number of multiples you must cross (i.e., step onto) when moving monotonically from u1 to u2 is the number of multiples strictly between u1 and u2. However, you could also move non-monotonically and possibly cross the same multiple multiple times, but that would increase count. To minimize, you would cross each boundary at most once, and you would try to cross as few distinct multiples as possible. The question: is it possible to go from u1 to u2 without crossing any multiple if u1 and u2 are in the same "cell" between multiples? For example, a=2, multiples at ... -4,0,4,8... The cells are intervals: (-4,0), (0,4), (4,8), etc. If both u1 and u2 are in the same open interval between multiples (excluding endpoints), then you can go from one to the other without crossing a multiple by staying within that interval, because the interval is connected and contains no multiples. Since moves are unit steps, you can traverse between any two integers within the interval without leaving the interval? But need to ensure that all intermediate integers are also within the interval. For an interval (k*2a, (k+1)2a) open at both ends, the integer points inside are from k2a+1 to (k+1)*2a-1 inclusive. That's a contiguous set of integers. So yes, you can walk from any integer in that set to any other in that set by monotonic steps without ever landing on the endpoints (multiples). So if u1 and u2 are in the same cell, minimal number of u-bad squares visited is 0 with respect to u condition.
If they are in different cells, you must cross the boundary. Could you cross the boundary without stepping on the multiple? To go from one cell to the adjacent cell, you must cross the multiple that separates them. For example, from u=3 (in (0,4)) to u=5 (in (4,8)). The cells are separated by the multiple 4. To go from 3 to 5, you must pass through 4. There's no alternative because any path from 3 to 5 must contain 4. If you try to go the long way around, you'd have to cross other multiples. For instance, go from 3 to negative infinity and then to 5: you'd cross 0, -4, -8, ... then eventually cross back positive multiples. So you'd cross many multiples. So the minimal number of distinct u-multiples you must step on is exactly the number of multiples that lie between u1 and u2 if you go the shortest way? But careful: if you go the shortest way, you cross exactly one boundary: the multiple at the edge. But if you go the other way around the circle (like wrap around via negative infinity), you'd cross many. So minimal is the minimal number of boundaries you must cross to get from the cell containing u1 to the cell containing u2. Since the cells are arranged linearly on integer line, the minimal number of multiples you must step on is the difference in cell indices, i.e., the number of multiples that lie strictly between u1 and u2 if you go the direct route. But what if u1 and u2 are on opposite sides of a multiple but one is exactly at the boundary? Not allowed because start and end aren't bad, so they are not exactly on multiples. So they are strictly inside cells.
Thus, define cell index: let k1 = floor(u1 / (2a))? But careful with negative numbers: define the cell as the integer interval (2a * t, 2a * (t+1)) for some t ∈ Z. For a given u not multiple, there is a unique t such that 2at < u < 2a(t+1) if u positive; if u negative, similar but intervals: for negative, the intervals are (2a * t, 2a*(t+1)) where t may be negative. Actually the intervals between multiples are of length 2a, but note that 0 is a multiple, so intervals: ... (-2a,0), (0,2a), (2a,4a), ... But careful: if a=2, 2a=4, intervals: (-4,0), (0,4), (4,8). Yes.
So we can compute t1 = floor division: t1 = floor((u1 + (2a-1)? Actually simpler: t1 = floor(u1 / (2a)) if u1 not multiple. But floor for negative: e.g., u1=-1, floor(-1/4) = -1 because -0.25 floor = -1? Actually floor division in programming usually truncates toward -inf. In math, t = ⎣ u/(2a) ⎦ if u>0? But for u=-1, u/(2a) = -0.25, floor = -1. Then interval (2at, 2a(t+1)) = (-4,0). Yes, that works because -1 is in (-4,0). For u negative but not multiple, floor division yields correct left endpoint index.
However, note that when u is exactly multiple, it's bad, but start not bad, so we avoid that.
Thus, the number of u-boundaries that must be crossed when moving directly from u1 to u2 is |t2 - t1|, because each step across a cell reduces/increases t by 1. But careful: if both u1 and u2 are in same cell, t1 = t2, so zero boundaries. If t2 > t1, then you must cross multiples at u = 2a*(t1+1), 2a*(t1+2), ..., 2at2. That's (t2 - t1) multiples? Actually crossing from cell t1 to cell t1+1 requires stepping on the multiple 2a(t1+1). Then to go to t1+2 you cross 2a*(t1+2), etc. So total distinct multiples stepped on = t2 - t1 (if moving increasing direction). Similarly if t2 < t1, then |t1 - t2| multiples (the negative ones). So the minimal number of u-bad squares forced by u condition alone is |t2 - t1|, provided we choose the shortest direction (i.e., direct monotonic path). But is it always possible to achieve exactly |t2 - t1| without incurring extra u-bad squares? Yes, by moving monotonically from u1 to u2. In that path, you will step on each multiple exactly once (when you cross from just below to just above). You might also step on them again if you backtrack, but you don't. So minimal u-bad count = |t2 - t1|.
But we also have v condition. So overall bad squares are those where either condition holds. So we need to minimize total number of distinct squares that are bad visited. Since a bad square can satisfy both conditions, that would count as one. So we need to consider both u and v boundaries simultaneously.
Essentially, we need to go from start (u1, v1) to end (u2, v2) in the (u,v) lattice with moves that change each coordinate by ±1 (with parity constraints). The cost is 1 for any vertex that lies on u-multiple lines or v-multiple lines. We want to minimize number of such vertices visited.
This is akin to moving in a grid with obstacles (lines) but cost for stepping on lines. The minimal cost may be less than the sum of independent minimal crossings if we can cross both lines at same point (i.e., at an intersection of a u-multiple and v-multiple, which would be a square that is bad (both) but counted once). So the problem reduces to: We have to cross a certain number of u-lines and v-lines. The minimal number of bad squares equals the minimal number of line-crossings (where crossing a line costs 1, but crossing two lines at a crossing costs 1) to go from the start region to the end region.
Let’s define:
Let u_idx = floor(u/(2a)) for u not multiple; but better define the "signed cell index" for u: i_u = floor( (u + a) / (2a) )? Actually we need a consistent mapping so that crossing a u-line increments/decrements i_u by 1. The start cell index for u: i1 = floor( (u1) / (2a) )? But careful for negative: e.g., u1 = -1, floor(-1/4) = -1, which gives cell index -1? The cells: ... cell -2: (-8,-4), cell -1: (-4,0), cell 0: (0,4), cell 1: (4,8). So i1 = -1 for u=-1, i2 for u=5 is floor(5/4)=1 (since 5/4=1.25 floor=1). So difference = 2. Indeed we need to cross multiples at 0 and 4? Wait from -1 to 5: path: -1 -> 0 -> ... -> 5. The multiples: 0 and 4. That's two multiples. But cell indices: i1=-1, i2=1. The difference = 2. But are we crossing 0 and 4? Yes. So number of u-boundaries = |i2 - i1|? In this example, i2 - i1 = 2. But we crossed two multiples: at 0 (separating cells -1 and 0) and at 4 (separating cells 0 and 1). So yes, the number of u-lines to cross is the absolute difference between the cell indices.
But is it always |i2 - i1|? Suppose u1=3 (i1= floor(3/4)=0), u2=5 (i2=1), difference=1 -> cross multiple at 4. Good.
For negative to positive crossing zero, i1 negative, i2 positive, difference = number of lines.
Now what about the case where both u1 and u2 are in same cell but one is above a multiple and the other below? Then i1 = i2, diff=0 -> no crossing. So that works.
Thus minimal number of u-line crossings required is D_u = |i_u2 - i_u1|, where i_u = floor(u / (2a)). However careful: floor for negative numbers: In programming, integer division truncates toward zero in C++? But we can define properly.
Similarly for v: j_v = floor(v / (2b)). Then minimal number of v-line crossings required is D_v = |j_v2 - j_v1|.
But is it always necessary to cross exactly that many lines? Could we reduce total cost by combining some u and v crossings at intersections? For example, if we need to cross D_u u-lines and D_v v-lines, the minimal number of bad squares we must visit might be max(D_u, D_v) or something? Let's reason.
Consider the grid of lines: vertical lines at u = 2a * i, horizontal lines at v = 2b * j (in (u,v) coordinates). Actually v-lines? In (u,v) plane, condition v ≡ 0 mod 2b corresponds to horizontal lines? Since v is the other coordinate, so v = constant lines are horizontal? Usually x horizontal, y vertical, but here u and v axes are orthogonal. So we have a grid: vertical lines at u = 2a * k, horizontal lines at v = 2b * l. Bad squares are those lying on any vertical line or any horizontal line. So the "safe" region consists of open rectangles (cells) where u is not a multiple of 2a and v is not a multiple of 2b. The start and end are inside such safe rectangles.
We need to move from start rectangle to end rectangle. Each time we cross a vertical line (i.e., step onto a point with u multiple), we incur cost 1. Each time we cross a horizontal line (v multiple), cost 1. But if we cross both at a grid intersection (i.e., a point that lies on both a vertical and horizontal line), the cost is 1 for that point, and it counts as crossing both lines simultaneously.
Our moves: each step changes u and v by ±1. So we can only cross one line at a time normally, but at an intersection we cross both at the same step: from a point where u is just below multiple and v just below multiple, stepping to the point where both become multiple. That step crosses both lines.
Thus, the problem resembles: we need to go from a starting cell (i1, j1) (where i1 = floor(u1/(2a)), j1 = floor(v1/(2b))) to an ending cell (i2, j2). However note: the cell indices are for the rectangle between lines. Actually the safe rectangle is (2ai, 2a(i+1)) × (2bj, 2b(j+1)). The start point (u1,v1) lies in the interior of that rectangle. The goal is to reach the rectangle containing (u2,v2). We can move in the continuous space but discrete points. The cost is incurred when we step on any point that lies on a boundary line (i.e., u multiple or v multiple). Essentially crossing from one rectangle to an adjacent rectangle (sharing a side) forces us to step on the boundary line (the side). The cost of crossing a side is 1, regardless of where we cross it. However, if we cross through a corner (i.e., move from a rectangle to a diagonally adjacent one by crossing both a vertical and horizontal line in one step), we would step on the corner point which lies on both lines, costing 1, but we effectively cross both boundaries in one step. Is that allowed? To go from rectangle (i,j) to (i+1, j+1) in one step? Let's see: starting from a point inside rectangle (i,j), we want to end up inside rectangle (i+1,j+1) after a sequence of moves. Could we do it in one move? No, because a single step changes u by ±1 and v by ±1. To go from inside (i,j) to inside (i+1,j+1), we would need to cross at least one boundary. Actually consider the coordinates: rectangle (i,j) has u in (2a i, 2a (i+1)), v in (2b j, 2b (j+1)). Rectangle (i+1, j+1) has u in (2a (i+1), 2a (i+2)), v in (2b (j+1), 2b (j+2)). To go from a point inside the first to a point inside the second, the u must increase by at least something > 0, and v by >0. A single step changes u by 1 and v by 1, but could that take you from inside one to inside the other? For example, suppose a=2, b=2, so 2a=4, 2b=4. Rectangle (0,0): u in (0,4), v in (0,4). Rectangle (1,1): u in (4,8), v in (4,8). Could we move from u=3.5, v=3.5? But coordinates are integer only. Actually u and v are integers because x,y integers. So u and v are integers. The interior of rectangle (0,0) for integer points means u ∈ {1,2,3} and v ∈ {1,2,3} (since multiples are 0,4). The interior of (1,1) is u ∈ {5,6,7} and v ∈ {5,6,7}. A single step changes u by ±1 and v by ±1. From u=3,v=3, a step (+1,+1) gives u=4,v=4, which is exactly on the corner (both multiples). That's a bad point, and it's on the boundary. From there, another step could take us into (1,1) e.g., (5,5). So it takes at least two steps to go from (0,0) interior to (1,1) interior, crossing the corner point (4,4). That crossing point counts as one bad square, but we have crossed both a vertical line (u=4) and a horizontal line (v=4) at that same point. So effectively, to go from rectangle (i,j) to (i+1, j+1), we would cross one vertical and one horizontal line, but we can do it by passing through the corner, incurring cost 1 instead of 2 if we crossed them separately (first cross vertical, then horizontal, costing 2). But is it always possible to cross both at a corner? That depends on parity? The corner point (u=2a i, v=2b j) is an integer point? Since 2a and 2b are even multiples, but a,b are integers >=2, so 2a, 2b are even integers, thus multiples are even integers. So the corners are integer points. Are they reachable from interior points? Starting from an interior point (u,v) in rectangle (i,j), we need to step to the corner (2a*(i+1), 2b*(j+1))? Actually corner between rectangles (i,j) and (i+1,j+1) is at (u=2a*(i+1), v=2b*(j+1)). But is that point adjacent to some interior point? For instance, from u=2a*(i+1)-1, v=2b*(j+1)-1, a step (+1,+1) leads to the corner. Are those coordinates inside rectangle (i,j)? For rectangle (i,j), u ∈ (2a i, 2a (i+1)). The maximum u inside is 2a(i+1)-1, since it's integer and must be less than 2a(i+1). So yes, u = 2a(i+1)-1 is inside. Similarly v = 2b(j+1)-1 is inside. So the point (2a(i+1)-1, 2b(j+1)-1) is inside rectangle (i,j) (provided those are integers, they are because 2a and 2b are even, but 2a(i+1)-1 is odd? Actually 2a*(i+1) is even multiple, minus 1 gives odd. That's an integer, yes). So that point is reachable (since we can walk inside the rectangle). So from that point, a single step (+1,+1) takes us to the corner (2a(i+1), 2b(j+1)). That corner is a bad square. Then from that corner, we can step into rectangle (i+1,j+1) by another step (+1,+1) to (2a(i+1)+1, 2b(j+1)+1) which is inside the next rectangle. So crossing both lines via the corner costs 1 bad square (the corner) plus maybe we could also step directly from interior of (i,j) to interior of (i+1,j+1) in two steps with the corner as intermediate. So the cost is 1. Alternatively, we could cross vertical first: go to point (2a(i+1), something) then cross horizontal: that would be two bad squares. So crossing diagonally can save one cost.
Thus, the problem reduces to: we need to change the cell indices (i, j) from (i1,j1) to (i2,j2). Each step in the cell grid (i.e., moving to an adjacent cell) may correspond to crossing a vertical line, horizontal line, or both. The cost associated with crossing a vertical line (change i by ±1 while j unchanged) is 1 (since you must step on that vertical line). The cost for crossing a horizontal line (change j by ±1) is 1. The cost for crossing both (change i and j by ±1 simultaneously) is 1 (if you go through the corner). However, is it always possible to achieve a simultaneous crossing? We need to check parity constraints: the moves in (u,v) space must have the property that u and v have same parity? Actually from any point (u,v) reachable from start, u and v maintain parity relation: u ≡ v (mod 2) because u = x+y, v = x-y, so u and v have same parity. This parity is invariant? Let's check: starting from (x1,y1), u1 and v1 have same parity (since x1+y1 and x1-y1 have same parity). A move changes both u and v by ±1, so parity of u flips, parity of v flips. So both flip, thus they remain equal. So the invariant is that u ≡ v (mod 2) throughout the walk. So all visited points satisfy u ≡ v (mod 2). This is important.
Now, consider the corner point (u=2ai, v=2bj). Does it satisfy u ≡ v mod 2? Not necessarily. Since 2ai is even (2a is even), so u even. Similarly 2bj is even, so v even. So u and v are both even, so they have same parity (even). So corner points satisfy the parity condition. Good.
What about points on a vertical line but not on horizontal: u even, v can be odd or even? But if v is odd, then u even, v odd, parity differs, so such a point would not satisfy u ≡ v mod 2. But are such points reachable? If we ever step onto a point with u multiple (even) and v odd, that would break the parity invariant. Since start has u1 ≡ v1 (mod 2), and each step flips both parities, the parity equality holds. So at any step, u and v must have same parity. Therefore, any point we visit must have u and v same parity. So we cannot step onto a point where u is multiple (even) and v is odd, because that would have u even, v odd -> different parity. Similarly, we cannot step onto a point where v is multiple (even) and u is odd. Therefore, the only bad points we can possibly step on are those where both u and v have same parity, which means if u is multiple (even), then v must also be even; if v is multiple (even), then u must also be even. So effectively, we are forced to cross lines only at points where both coordinates are even. That imposes restrictions: When crossing a vertical line, we must cross at a point where v is even (i.e., the crossing point has even v). Since we can choose where to cross, we can attempt to cross at such points. Similarly, crossing a horizontal line requires u even. And crossing both at a corner automatically has both even.
Thus, the cost model may be more nuanced: Not every crossing of a line costs 1; we might be forced to cross at a point that also satisfies the other condition (making it a corner) or not? But even if we cross a vertical line at a point where v is even, that point is on the vertical line (u multiple) and v even; does that automatically make it also satisfy the v condition? v even does not mean v ≡ 0 mod 2b; v must be a multiple of 2b to be bad due to v condition. So v even is not enough; it must be multiple of 2b. So crossing a vertical line at a point where v is even but not multiple of 2b is still a bad square only due to u condition, and it's allowed because parity condition holds (u even, v even). So such a point is reachable. So crossing a vertical line at a point with v even is possible.
But is it always possible to cross a vertical line at a point with v even? Consider the rectangle. To cross from cell (i,j) to (i+1,j) (increase i by 1), we need to step from a point with u = 2a(i+1)-1 (since just left of the line) to u = 2a(i+1). The step changes u by +1; we also change v by either +1 or -1? Wait, each step changes both u and v by ±1. So if we want to cross the vertical line, we must move in a direction that changes u by +1 (to go right) and also changes v by either +1 or -1. So the point we step onto will have u = 2a(i+1) and v = v_prev ± 1, where v_prev is the v-coordinate before the step. We can choose v_prev appropriately to make the landing v even and also satisfy parity? Let's analyze parity constraints.
Before crossing, we are at some point (u_prev, v_prev). Since we are inside rectangle (i,j), u_prev is not multiple. After step, u becomes multiple. The parity: before, u_prev and v_prev have same parity. After step, u changes by +1 (flips parity), v changes by ±1 (flips parity). So after step, u and v flip both, so they remain equal. So if we end at (u_even, v_even) or (u_odd, v_odd), it's fine as long as they are equal. But u_even because u is multiple (even). So u_even implies v_even must hold. Therefore, after crossing, v must be even. Therefore, the step must be such that v_prev ± 1 is even, i.e., v_prev must be odd. Because adding/subtracting 1 toggles parity. So to land on even v, v_prev must be odd. So we need to be at a point just before crossing where v_prev is odd, and then choose the step that changes v by +1 or -1 (both toggling parity) to land on even. But note: v_prev must be inside rectangle (i,j). So we need to ensure there exists a point on the left side of the vertical line (i.e., with u = 2a(i+1)-1) that has v odd and also is reachable from our current position inside the rectangle. Since the rectangle is connected, we can move within the rectangle to any interior point, because there are no restrictions inside safe region (except we shouldn't step on bad points, but interior points are all safe because they are not on lines). So as long as the rectangle contains at least one point with u = 2a(i+1)-1 and v odd, we can get there. Does it? The line u = 2a(i+1)-1 is the rightmost column of the rectangle (since u max = 2a(i+1)-1). For that fixed u, v ranges over integers from 2bj+1 to 2b(j+1)-1 (the interior). So we need that among those v values, there is at least one odd integer. Since the interval length is (2b*(j+1)-1) - (2bj+1) + 1 = 2b - 1? Actually the number of integers is (2b(j+1)-1) - (2bj+1) + 1 = 2b - 1. For b >= 2, 2b - 1 >= 3, so there are multiple integers. The parity of v values: the interval goes from (2bj+1) to (2b*(j+1)-1). Since 2bj is even (multiple of 2), 2bj+1 is odd. Then the sequence alternates parity. Since the length is 2b-1, which is odd, the number of odd numbers is (2b-1+1)/2 = b? Let's compute: from odd to odd over an odd count, the count of odds = (length+1)/2. Since length = 2b-1, (2b-1+1)/2 = b. So there are b odd numbers. So definitely there exist odd v. So crossing a vertical line is always possible with cost 1 (by stepping onto a point with u multiple, v even). But careful: we need v_even to satisfy parity, but also we need that point is not also a v-multiple? If v_even but not multiple of 2b, then it's just a u-bad square, cost 1. If v_even happens to be a multiple of 2b, then it's a corner, still cost 1. So crossing a vertical line can be done with cost 1, and we can avoid also hitting a horizontal line if we choose v not multiple. Similarly, crossing a horizontal line can be done with cost 1 by choosing u odd before crossing (so that after crossing u becomes even), thus landing on a point with v multiple and u even (but u even not multiple? Actually u becomes the coordinate on the horizontal line? Wait: crossing horizontal line means we step onto a point with v multiple. After step, v is multiple (even). For parity, we need u also even. So we need u_prev odd, and then step changes u by ±1 to become even. That's possible if we are at a point with u odd just below/above the line? For crossing horizontal line from below (increase j), we need to go from v = 2b(j+1)-1 to v = 2b(j+1). So similar reasoning: there exists a point with v = 2b(j+1)-1 and u odd. So yes, we can cross horizontal line with cost 1.
Now, crossing both lines simultaneously (diagonally) is also possible: from a point with u = 2a(i+1)-1, v = 2b(j+1)-1, both odd (since they are one less than even multiples), step (+1,+1) gives (even, even) both multiples. That point is a corner, cost 1. So crossing both at once costs 1.
Therefore, the problem is essentially: we need to change the cell indices (i,j) from (i1,j1) to (i2,j2). Each unit change in i or j can be performed with a cost of 1, but if we change both i and j simultaneously (i.e., move diagonally to adjacent cell), we can also do it with cost 1. However, are we allowed to change both i and j in a single "crossing"? In terms of the walk, to go from rectangle (i,j) to (i+1, j+1), we need at least two steps: from interior to corner, then to interior. That's two steps, but only the corner is bad. So the cost is 1, but the number of steps is not relevant. So effectively, we can think of the cost as the number of axis-aligned line crossings, but diagonal crossings combine two line crossings into one cost.
But careful: Could we combine more than two? For example, change i by 2 and j by 1? That would involve crossing two vertical lines and one horizontal. The minimal cost might be 2 if we can cross one vertical and the horizontal together (cost 1) and then the other vertical separately (cost 1) -> total 2. So the cost seems to be the Chebyshev distance? Actually if we can cross both a vertical and horizontal together, then the cost to change (i,j) by (Δi, Δj) might be max(|Δi|, |Δj|)? Because we can pair each vertical crossing with a horizontal crossing when moving in the same direction, and the remaining unpaired crossings would be done separately. However, we must consider sign: Δi and Δj can be positive or negative. If both have same sign, we can combine them in diagonal moves. If they have opposite signs, can we combine a vertical increase with a horizontal decrease? That would be moving to (i+1, j-1). Is that possible? To go from rectangle (i,j) to (i+1, j-1) (i increases, j decreases). This involves crossing a vertical line (right) and a horizontal line (down). Can we cross both at a corner? The corner between rectangles: the meeting point of vertical line u = 2a(i+1) and horizontal line v = 2b*j? Actually, rectangle (i,j) is (u in (2a i, 2a(i+1)), v in (2b j, 2b(j+1))). Rectangle (i+1, j-1) would be u in (2a(i+1), 2a(i+2)), v in (2b (j-1), 2b j). The corner shared is at (u = 2a(i+1), v = 2b j). That's an intersection of a vertical line and a horizontal line. To go from interior of (i,j) to interior of (i+1, j-1), we could go to that corner. However, does the corner belong to which rectangles? The point (2a(i+1), 2b j) lies on the boundary. It is the top-right corner of rectangle (i+1, j-1)? Actually careful: For j-1, the v interval is (2b(j-1), 2b j). So the upper boundary is v = 2b j, which is not included in the interior. So the corner (2a(i+1), 2b j) is the top right corner of rectangle (i+1, j-1) (if we consider open interiors). Similarly, it's the bottom right corner of rectangle (i,j)? Actually rectangle (i,j) v interval is (2b j, 2b(j+1)), so its lower boundary is v = 2b j, so the corner is on its lower boundary. So yes, it's a corner between four rectangles: (i,j), (i+1,j), (i,j-1), (i+1,j-1). So we can cross from (i,j) to (i+1,j-1) via that corner: go from interior of (i,j) to the corner, then to interior of (i+1,j-1). The step to the corner: we need a point adjacent to the corner inside (i,j). For (i,j), the interior near the corner: we need u = 2a(i+1)-1, v = 2b j+? Actually the corner is at (2a(i+1), 2b j). To approach from inside (i,j), we need to be at a point with u = 2a(i+1)-1 (since u < 2a(i+1) inside) and v = something such that a step (+1,?) leads to the corner. The corner's v is 2b j. Inside (i,j), v must be > 2b j (since lower bound is 2b j, not inclusive). So v can be 2b j+1, 2b j+2, ... up to 2b(j+1)-1. To step to the corner (increase u by 1, and change v by -1 to go from v > 2b j to v = 2b j? Actually we need to land exactly on (2a(i+1), 2b j). So if we start at (2a(i+1)-1, v0), then after step (+1, Δv) we get (2a(i+1), v0+Δv). We need v0+Δv = 2b j. So we can choose Δv = -1 if v0 = 2b j+1. So from point (2a(i+1)-1, 2b j+1) a step (+1,-1) leads to the corner. Is that point (2a(i+1)-1, 2b j+1) inside rectangle (i,j)? u = 2a(i+1)-1 is inside, v = 2b j+1 is inside (since >2b j). So yes, it's interior. And parity: (2a(i+1)-1) is odd, (2b j+1) is odd? 2b j is even, +1 => odd. So both odd, step (+1,-1) changes both by +1 and -1 respectively: parity of u flips to even, parity of v flips to even? Starting odd+1 = even, odd-1 = even. So both even, parity matches. So yes, it's allowed. So crossing diagonally in opposite directions (i+1, j-1) is also possible via a corner, costing 1. So any diagonal move (Δi, Δj) with |Δi|=|Δj|=1 can be done with cost 1, regardless of signs.
Thus, we can combine one vertical and one horizontal crossing into a single cost if we move diagonally. However, note that we cannot combine two vertical crossings without a horizontal, but we can pair them.
Therefore, to go from (i1,j1) to (i2,j2), we need to achieve a total change Δi = i2 - i1, Δj = j2 - j1. The minimal number of line crossings needed is something like: we need to cross |Δi| vertical lines and |Δj| horizontal lines. But we can pair up some of them: each pair consists of one vertical and one horizontal crossing, which can be done at cost 1 by a diagonal move. After pairing, the remaining unpaired crossings must be done separately, each costing 1. So total cost = number of paired crossings + number of unpaired vertical + number of unpaired horizontal = max(|Δi|, |Δj|)? Let's check: if we pair as many as possible, we can pair min(|Δi|, |Δj|) vertical+horizontal together, each pair cost 1. Then we have |Δi| - min = |Δj| - min = |Δi| - |Δj|? Actually the remaining is |Δi| - min = |Δi| - min(|Δi|,|Δj|) = | |Δi| - |Δj| |? Wait, if |Δi| >= |Δj|, then min = |Δj|, so after pairing we have |Δi| - |Δj| vertical lines left, and 0 horizontal left. Those remaining vertical crossings cost 1 each. So total cost = |Δj| (pairs) + (|Δi| - |Δj|) = |Δi|. Similarly if |Δj| >= |Δi|, total cost = |Δj|. So total cost = max(|Δi|, |Δj|). That suggests minimal number of bad squares is max(|Δi|, |Δj|). However, is that always achievable? We must ensure that we can perform these moves respecting parity and reachability within safe regions. Also, we need to consider that the start and end are in specific positions within their rectangles, not necessarily at the corners we need to exit/enter. But we can move freely within a rectangle (safe region) without cost, because interior points are not bad. So we can position ourselves appropriately to execute the desired crossing strategy.
But there might be additional constraints due to parity of the indices? For instance, when we want to perform a diagonal move from (i,j) to (i+1, j+1), we need to exit the rectangle at a corner. To do that, we need to reach a point adjacent to the corner inside the rectangle. As argued, such points exist (odd coordinates). But are they always reachable from any starting point inside the rectangle? Since the rectangle interior is connected via moves that never step on boundaries (since boundaries are bad, but we can avoid them). The interior graph is essentially all integer points (u,v) with u between L+1 and R-1, v between B+1 and T-1, where L=2ai, R=2a(i+1), B=2bj, T=2b(j+1). All interior points are safe. The moves are allowed as long as we don't step on bad squares, but stepping on a boundary is bad, so we must stay strictly inside. The interior points form a grid of size (2a-1) x (2b-1). For a>=2, 2a-1 >=3, similarly for b. So it's at least 3x3, so there is enough room to maneuver. Moreover, the interior is connected via steps that stay inside (since you can move horizontally/vertically? Wait: a step changes both u and v simultaneously by ±1, so it's like moving on a diagonal grid? Actually in (u,v) coordinates, the moves are (1,1), (1,-1), (-1,1), (-1,-1). That's like moving on the chessboard's black squares? Actually it's like moving on a graph where you can only move to points with both coordinates changing. That graph is bipartite? But connectivity: Starting from any interior point, can we reach any other interior point while staying inside? The moves change u and v both by ±1, so parity of u+v changes? But more importantly, the set of points reachable from a given point with these moves is all points with same parity (u ≡ v mod 2) and within the same connected component of the grid when considered as a graph with edges of those four types. This graph is essentially the set of points with u and v integers; each move changes both coordinates, so it's like moving on a checkerboard rotated 45 degrees. In terms of original (x,y), it's just the standard grid moves; in (u,v) it's like moving along diagonals. But we know that from any (x,y) we can reach any other (x,y) via Manhattan moves; that corresponds in (u,v) to the ability to change u and v independently but with parity constraints. So the interior region is connected under these moves? Let's check: In (u,v) moves, to change u by +1 without changing v, you would need two steps: e.g., (+1,+1) then (+1,-1) results in net (+2,0). So you can change u by 2 while v unchanged. So you can move within the interior as long as you can adjust parity appropriately. Since interior is large enough, you can get to any point with same parity? Actually the interior points with u in [L+1, R-1], v in [B+1, T-1] and satisfying u≡v (mod 2). That's a sublattice of the integer grid. Are all such points connected? The moves are essentially steps on the diagonals. The graph is like the set of vertices of a chessboard where you can move like a bishop? Actually bishop moves change both coordinates by equal amount, not necessarily ±1; but here steps are only ±1 on each coordinate, which is like moving to a neighboring cell of opposite color in original (x,y) but in (u,v) it's like moving to adjacent diagonally adjacent squares. The connectivity: The graph of integer points with edges connecting (u,v) to (u+1,v+1), (u+1,v-1), etc., is exactly the graph of the original grid under transformation: (u,v) -> (x,y) = ((u+v)/2, (u-v)/2). Under that transformation, the moves correspond to changing x or y by ±1. So connectivity is same as original grid. So yes, the interior (safe) region is connected (as long as it's not empty). So we can navigate to any interior point with the required parity.
Thus, we can realize any sequence of crossing moves: we can go from start to an appropriate exit point, cross a line (or two), then inside new rectangle navigate to next exit, etc.
Therefore, minimal number of bad squares seems to be max(|Δi|, |Δj|), where Δi = i2 - i1, Δj = j2 - j1, with i = floor(u/(2a)), j = floor(v/(2b)). However, we need to verify with examples.
Let's test the given examples.
Example 1:
Input: a=2, b=2, x1=1, y1=0, x2=0, y2=1.
Compute u1 = x1+y1 = 1+0=1, v1 = x1-y1 = 1-0=1.
u2 = 0+1=1, v2 = 0-1 = -1.
Check: a=2 => 2a=4, b=2 => 2b=4.
Compute i1 = floor(u1/4) = floor(1/4)=0.
i2 = floor(1/4)=0.
j1 = floor(v1/4) = floor(1/4)=0.
j2 = floor(v2/4) = floor(-1/4). In integer division, floor(-0.25) = -1? Actually -1/4 = -0.25, floor = -1. But careful: we need to define floor division that gives the index of the cell. For v2=-1, the cell is (-4,0), so index should be -1. So j2 = -1.
Thus Δi = 0, Δj = -1 => |Δj|=1, max = 1.
Output expected: 1. Good.
Example 2:
Input: 2 2 10 11 0 1
a=2,b=2 => 2a=4,2b=4.
x1=10,y1=11 => u1=21, v1= -1? Actually x1-y1 = 10-11 = -1.
x2=0,y2=1 => u2=1, v2=-1.
Compute i1 = floor(21/4)=5 (since 45=20, 21 in (20,24)? Actually interval (20,24) => index 5? Wait, 2ai = 4i. For i=5, interval (20,24). Yes u=21 ∈ (20,24), so i1=5.
i2 = floor(1/4)=0.
j1 = floor(v1/4): v1=-1 => floor(-1/4). In math, floor(-0.25) = -1, so j1=-1.
j2 = floor(v2/4): v2=-1 => j2=-1.
Thus Δi = 0-5 = -5 => |Δi|=5, Δj = (-1)-(-1)=0 => max=5.
Output expected: 5. Good.
Example 3:
Input: 2 4 3 -1 3 7
a=2 => 2a=4, b=4 => 2b=8.
x1=3,y1=-1 => u1=3+(-1)=2, v1=3-(-1)=4? Actually x1-y1 = 3 - (-1) = 4.
x2=3,y2=7 => u2=3+7=10, v2=3-7 = -4.
Check start not bad: |u1|=2 mod 4? 2 mod 4 ≠0; |v1|=4 mod 8? 4 mod 8 ≠0? Actually 4 mod 8 =4, not 0, so good. End: u2=10 mod4=2 not 0; v2=4 mod8=4 not 0? Actually |v2|=4, not 0 mod8, good.
Compute i1 = floor(u1/4) = floor(2/4)=0.
i2 = floor(10/4)=2 (since 2*4=8, 10∈(8,12) => index 2? Actually interval (8,12) corresponds to i=2? Let's check: i=2 => (8,12). Yes u=10, so i2=2.
j1 = floor(v1/8): v1=4 => floor(4/8)=0.
j2 = floor(v2/8): v2=-4 => floor(-4/8) = floor(-0.5) = -1? Because -0.5 floor = -1. But check: interval for j=-1 is (-8,0). v=-4 lies in (-8,0), so j2=-1.
Thus Δi = 2-0=2, Δj = -1-0 = -1 => |Δi|=2, |Δj|=1, max = 2.
Output expected: 2. Good.
All examples match max(|Δi|,|Δj|). So that seems plausible.
But we need to be careful: Are there any cases where the minimal number is not max? Could there be additional constraints due to parity or the fact that the start and end are not on lines, causing the need to possibly cross an extra line if the parity of the number of steps or something forces you to land on a line? But we can always arrange to avoid extra bad squares.
Let's test edge cases: Suppose Δi=0, Δj=0 => max=0, should output 0. Is it always possible to go from start to end without stepping on any bad square? Since they are in same rectangle, can we always find a path staying entirely within the safe interior? The interior is all points not on u-multiple or v-multiple. The rectangle interior is safe and connected. However, is the interior connected under moves? Yes, the safe region is the set of integer points where u not multiple and v not multiple. For a given rectangle, it's a grid of size (2a-1)*(2b-1) with holes only on boundaries, but boundaries are excluded. So any two points inside should be reachable without stepping on boundaries. But we must also consider that moves might force us to step on a boundary if we try to go from one interior point to another if they are "across" a boundary? No, because both are inside same rectangle, and the rectangle is convex in terms of integer lattice? Not exactly convex, but since we can move in Manhattan steps, we can navigate staying inside. For example, a=2,b=2 rectangle (0,4) x (0,4) interior: u=1,2,3 and v=1,2,3. Can we go from (1,1) to (3,3)? Steps: (1,1)->(2,2)->(3,3) all inside (since u,v in {1,2,3}). Yes. What about (1,3) to (3,1)? (1,3)->(2,2)->(3,1). Yes. So seems fine.
Now consider a=2,b=2 but rectangle (0,4)x(0,4). Is there any pair of interior points that cannot be connected without stepping on a boundary? Possibly if the interior graph is not fully connected? Let's examine the moves: In (u,v) coordinates, moves are diagonal. The parity constraint: u≡v mod2. In the interior, points (1,2) have u=1 odd, v=2 even, not same parity. So (1,2) is not reachable from start because start must have same parity? But wait, are interior points with u not≡v mod2 even present? Actually from the transformation, any point (u,v) derived from integer (x,y) must satisfy u≡v (mod2). So the points that actually exist in the original grid are only those with u≡v (mod2). So the interior of the rectangle as a subset of the lattice only includes points with u≡v mod2. So the graph is connected among those points? Let's check: For rectangle (0,4)x(0,4), the valid interior points are those with u,v∈{1,2,3} and u≡v mod2. That gives: (1,1), (1,3?), 1 and 3 both odd, so (1,3) has u=1 odd, v=3 odd -> same parity; (2,2) even,even; (3,1) odd,odd; (3,3) odd,odd; (2,? ) (2,?) even with even: (2,2) only; (1,? ) odd with odd: (1,1),(1,3); (3,1),(3,3). So it's a set of 5 points. Are they connected? Edges are diagonal steps of ±1. (1,1) connects to (2,2). (2,2) connects to (1,1), (1,3)? (2,2) to (1,3) would be (-1,+1) => (1,3) yes, also to (3,1) (+1,-1), and (3,3) (+1,+1). So (2,2) connects to all four corners. So graph is connected. So fine.
Thus, for same rectangle, we can do 0.
Now, for different rectangles, can we always achieve cost = max(|Δi|,|Δj|)? This corresponds to a path that uses diagonal moves for min(|Δi|,|Δj|) steps, and then axis-aligned moves for the remaining difference. However, we must ensure that after each crossing, we are inside the new rectangle and can continue. Also, we need to handle signs: if Δi and Δj have opposite signs, diagonal moves still work, as we argued, by crossing to (i+sign(Δi), j+sign(Δj)) with cost 1.
So the plan: For k = 1 to min(|Δi|,|Δj|), do a diagonal crossing (cost 1 each). Then for the remaining |Δi|-|Δj| vertical moves (if any) or horizontal moves, do axis-aligned crossings (cost 1 each). Total cost = min + (|Δi|-min) + (|Δj|-min) = max(|Δi|,|Δj|). Each such crossing is possible: for a diagonal, we exit the current rectangle through a corner into the next rectangle. For an axis-aligned crossing, we exit through a side (not a corner) at a point where we land on a line but not the other line. We need to ensure that after a diagonal crossing, we are in the interior of the new rectangle and can proceed. After crossing a corner, we are on the corner (bad). Then we need to move into the interior of the new rectangle with a step that does not incur another bad square? The corner is bad, but we already counted it. After that, we take a step from the corner into the interior. That step will move to a point adjacent to the corner inside the new rectangle. Is that interior point safe? For example, from corner (4,4) (bad) we step to (5,5). (5,5) has u=5 (not multiple of 4), v=5 (not multiple of 4), and safe. That step is a move, but we don't count the destination as bad because it's safe. So we can proceed. After an axis-aligned crossing, we step onto a side point (bad), then next step into the interior. That also adds only 1 cost for that crossing. So sequence works.
One nuance: after crossing a side, we land on a point on the vertical line but not on horizontal line. That point must have v even (to satisfy parity). As argued, we can arrange that. Also, after that, we need to step into the interior. That step will change u and v by ±1. If we are on a vertical line (u multiple), to move into the interior of the adjacent rectangle, we need to step such that u changes by +1 or -1 to move away from the line, and v changes by ±1. The new point will have u not multiple, v maybe something. That point must be safe (not on any line). Since we are moving from the line, the new u will be just beyond the multiple, which is not a multiple. The v will be (v_line ± 1). v_line is even, so v_line ± 1 is odd. But does that violate parity? The new point will have u odd (since from even +1 = odd), v odd. So parity holds, and it's not on v-multiple because v is odd (unless 2b=1? but b>=2 => 2b>=4, so odd not multiple). So safe. So works.
Similarly for horizontal crossing.
Now, after a diagonal crossing, we are at a corner (both even). To step into interior, we can take a step that changes both by +1 (or appropriate) to go into the diagonal adjacent interior. That gives u odd, v odd, safe.
Thus, the plan is sound.
But is there any scenario where the minimal cost is less than max(|Δi|,|Δj|)? Could we cross multiple lines in a single bad square? For instance, crossing two vertical lines at once? Not possible because each step changes u by only 1. To go from u < 2ai to u > 2a(i+2) without stepping on intermediate multiples, you'd have to skip over the multiple, which is impossible. So each vertical line must be crossed at some step, incurring at least one bad square. However, if you cross a vertical line at a point that also lies on another vertical line? No two vertical lines are distinct. So each vertical line crossing must incur at least one bad square. Similarly for horizontal. But we can combine a vertical and a horizontal crossing into one bad square, but cannot combine two verticals. So the total cost must be at least max(|Δi|,|Δj|). Because you need to cross |Δi| vertical lines, so at least |Δi| bad squares from those, unless some of those vertical crossings coincide with horizontal crossings (i.e., at corners). But each horizontal crossing can only pair with one vertical crossing. So the total number of bad squares is at least |Δi|, and also at least |Δj|. Therefore, at least max(|Δi|,|Δj|). And we have a construction achieving exactly that. So minimal cost = max(|Δi|,|Δj|).
Wait, is there any possibility that we could avoid some vertical crossing entirely by going around through a different path that doesn't require crossing that specific line? For example, suppose Δi=2, Δj=0. According to our reasoning, we must cross two vertical lines. But could we go the other way around the world, crossing many lines but maybe using corners to reduce cost? For Δi=2, Δj=0, max=2. If we go the other direction (decrease i instead of increase), we might cross many lines of opposite sign, but the net change in i is still 2, so you must end up with i increased by 2 relative to start. The number of vertical lines you cross net is 2, but you might cross some extra lines and then come back, which would increase cost. So minimal is indeed 2.
Now, is there any subtlety with the definition of i and j due to negative numbers and floor? We must ensure that the cell index difference correctly counts the number of lines between the two points. Let's formalize.
Define for any integer u not a multiple of 2a, let i(u) = ⌊ u / (2a) ⌋ if u>0? But floor works for all, but careful: For u negative, floor division gives the index such that 2ai <= u < 2a(i+1) for non-multiples? Let's test: u = -1, 2a=4. floor(-1/4) = -1 because -1/4 = -0.25, floor = -1. Then 2ai = -4, and 2a(i+1)=0. Indeed -1 is in (-4,0). So i=-1. Good. For u = -5, floor(-5/4) = -2? -5/4 = -1.25 floor = -2, then 2ai = -8, 2a(i+1)= -4? Wait -8 to -4, but -5 is in (-8,-4). That's correct: the cell index should be -2? Actually if intervals are (-8,-4), (-4,0), (0,4), etc. For u=-5, it's in (-8,-4). The left endpoint is -8, which is 2a*(-2) since 2a=4, -2*4=-8. So i = -2. floor(-5/4) = -2? Let's compute: -5/4 = -1.25, floor = -2. Yes. So works.
But what about u = -4? That's a multiple, but start/end are not bad, so we won't have u exactly multiple. So fine.
Thus i = floor(u / (2a)) works for all u not multiple, using integer division that truncates toward -∞ (like in Python's math.floor). In C++, for negative numbers, integer division truncates toward zero, so floor division is not directly given by /. We'll need to handle properly.
Similarly for v.
Now, we must ensure that the parity condition doesn't impose additional restrictions on the ability to achieve exactly max(|Δi|,|Δj|). Let's test a tricky case: a=3, b=5 maybe. But seems general.
However, there is another nuance: The start and end are not bad, but they could be on the "boundary" of the rectangle? No, they are interior because they are not on any line, so they are strictly inside a cell.
But what about the case where the start and end are in cells that are not aligned in terms of parity of the indices? Actually indices i and j can be any integers; we can move freely.
One more nuance: In the transformation, we used u = x+y, v = x-y. But note the condition uses |x+y| and |x-y|. That's equivalent to u and v absolute values. In our analysis, we used u and v directly and considered lines at multiples of 2a and 2b, but we didn't take absolute values. However, the condition |x+y| ≡ 0 mod 2a means u ≡ 0 mod 2a OR u ≡ 0 mod -2a? Actually absolute value: |u| ≡ 0 mod 2a means u is a multiple of 2a or u is a multiple of -2a, which is the same as u being a multiple of 2a (since if u = k*(2a) for integer k, then |u| is multiple; conversely if |u| is multiple, u = ±k*(2a). So indeed it's equivalent to u ≡ 0 (mod 2a) in the integer sense (i.e., u is divisible by 2a). Because u divisible by 2a means u = 2a * t for some integer t, which includes negative t. So condition is exactly u % (2a) == 0, with the understanding that modulo operation for negative numbers yields 0 if divisible. So our treatment is correct.
Now, we must ensure that the path we construct does not step on a bad square that we haven't counted, i.e., we must ensure that the only bad squares visited are exactly the ones we intend (the crossing points). During the interior movement within a rectangle, we must avoid accidentally stepping onto a line. Since interior points are safe by definition, and we can navigate using moves that stay within the interior. But is it always possible to move from an arbitrary interior point to a desired exit point (like near a corner or side) without stepping on a line? As argued, the interior is connected. However, we must also consider that while moving inside a rectangle, we might inadvertently step on a line if we go to the boundary. But we can plan a path that stays strictly away from boundaries. Since the interior includes all points with u not multiple and v not multiple, we can navigate using only points with u not multiple and v not multiple. There is no chance of accidentally hitting a line if we never go to u = multiple or v = multiple. So we need to show we can go from any interior point to e.g., (2a(i+1)-1, 2b(j+1)-1) without hitting a line. Since that target is also interior (its u is not multiple because it's one less than multiple, and v similarly). And there is a path between any two interior points that stays within interior? Possibly some interior points may be isolated due to parity? But as argued earlier, the interior graph (points with u mod (2a) ≠ 0, v mod (2b) ≠ 0, and u≡v mod2) is connected. We should verify connectivity more rigorously.
In original coordinates (x,y), the bad squares are those with u≡0 mod 2a or v≡0 mod 2b. In original grid, moving by one step changes u by ±1, v by ±1. The set of safe squares is all (x,y) such that u not ≡0 mod 2a AND v not ≡0 mod 2b. This set might not be connected globally, but within a single cell (rectangle) it should be connected. But is it? Consider a=2,b=2: cell (0,4)x(0,4) in (u,v) corresponds to a set of (x,y) points. Let's check connectivity in original grid. For a=2,b=2, the bad squares are those where |x+y|%4==0 or |x-y|%4==0. In the cell corresponding to u in (0,4), v in (0,4), what are the (x,y)? x = (u+v)/2, y = (u-v)/2. Since u,v in 1..3 with same parity, we got points like (1,0)? Let's compute: u=1,v=1 => x=1,y=0. That's a point. u=1,v=3 => x=2,y=-1. u=3,v=1 => x=2,y=1. u=3,v=3 => x=3,y=0. u=2,v=2 => x=2,y=0. So the interior points in this cell correspond to a set of points in original grid. Are they all connected via moves that avoid bad squares? For example, can we go from (1,0) to (2,0)? (2,0): u=2+0=2, v=2-0=2 => u=2 not multiple? 2 mod4=2, v=2 mod4=2, so (2,0) is safe. But is there a step from (1,0) to (2,0)? That's a right move: (2,0). That's allowed if (2,0) is safe. Yes. So connectivity seems fine. More generally, because the moves are the original grid moves, and the safe set within a cell is just the set of points whose (u,v) are in the open rectangle and satisfy parity. This set is essentially the original grid points that are not on forbidden lines. Since the forbidden lines are periodic, the safe set within a cell should be connected via king? Actually, need to ensure that there are no isolated "holes". But given that the cell is defined by inequalities on u and v, and u and v are linear combinations of x,y, it's a rotated rectangle. But I think it's safe to assume connectivity for a>=2, b>=2 because the spacing is large enough. However, consider extreme: a=10^9, b=10^9, then the rectangle is huge. But the moves are unit steps, so connectivity over large area is fine.
But could there be a case where the interior of a cell is not connected? For instance, if a=1? But a>=2, so 2a >=4. For a=1 (not allowed) the lines would be at multiples of 2, then interior might have parity issues. Since a>=2, 2a >=4, the interior width in u is at least 3, so there is room to move.
But even with width 3, as we saw, it's connected.
Thus, the solution is: compute i1 = floor(u1/(2a)), i2 = floor(u2/(2a)); j1 = floor(v1/(2b)), j2 = floor(v2/(2b)); answer = max(|i2-i1|, |j2-j1|).
But we must be careful with floor division for negative numbers. We can define a function:
i = u // (2a) if u >=0? But need floor. In C++ we can use:
if (u >= 0) i = u / (2a);
else i = (u - (2a-1)) / (2a); // since integer division truncates toward zero, this yields floor.
Better: use floor division function: i = (u >= 0) ? u / (2a) : (u - 2a + 1) / (2*a); // because in C++ int division truncates toward zero.
But careful: for negative u, we want floor(u/(2a)). Since u and 2a are positive, we can compute as:
i = u / (2a); // for u>=0 it's correct (truncates down). For u<0, this gives truncation toward zero (e.g., -1/4 = 0). So we need to adjust: if u < 0, we can compute i = (u + 1) / (2a) - 1? Actually, floor division: floor(u/d) = (u >= 0) ? u/d : (u - d + 1)/d when d>0, using integer division truncating toward zero. Yes, standard formula: floor_div(a,b) = a/b for positive a, and for negative a: (a - b + 1)/b. But this works when b>0. Let's verify: a=-1, b=4: (-1 -4 +1)/4 = (-4)/4 = -1. Good. a=-4, b=4: (-4-4+1)/4 = (-7)/4 = -1 (since truncation toward zero gives -1? Actually -7/4 = -1.75, truncation toward zero yields -1? In C++, -7/4 = -1 (since it truncates toward zero). But floor of -1.75 is -2. So that's not correct. Let's recompute: For a = -4, floor(-4/4) = -1? Wait -4/4 = -1 exactly? Actually -4/4 = -1 exactly, so floor(-1) = -1. But is that the correct cell index for u=-4? u=-4 is a multiple, but start/end are not multiples, so we won't have u exactly -4. But we might need to compute for u=-5. Let's test formula floor_div(-5,4). Should be -2 because -5/4 = -1.25 floor = -2. Using formula (a - b + 1)/b: (-5 -4 +1)/4 = (-8)/4 = -2. Good. For a=-4, it's a multiple, not used. For a=-3, (-3-4+1)/4 = (-6)/4 = -1.5 truncates to -1? In C++ integer division -6/4 = -1 (since truncation toward zero). But floor(-0.75) is -1? Actually -3/4 = -0.75, floor = -1. So -1 is correct. So formula (a - b + 1)/b works for a not multiple? Let's test a=-2: (-2-4+1)/4 = (-5)/4 = -1.25 trunc to -1? In C++, -5/4 = -1, but floor(-0.5) = -1? Wait -2/4 = -0.5, floor = -1. So -1 is correct. a=-1: (-1-4+1)/4 = (-4)/4 = -1, floor(-0.25) = -1, correct. a=0: would be multiple, but if a=0 not. So it works for negative a that are not multiples? Actually for a=-7: (-7-4+1)/4 = (-10)/4 = -2.5 trunc to -2, floor(-1.75) = -2, correct. So the formula seems correct for all a (including multiples?) For a=-4: (-4-4+1)/4 = (-7)/4 = -1.75 trunc to -1, but floor(-1) = -1; but -4 is exactly -1*4, floor(-1) = -1. So it's fine. So we can use that formula.
But we must be careful: if u is negative and divisible by 2a, then floor division would give exactly u/(2a) (since integer). But we won't have that case because start and end not bad, so u not divisible. But if we compute for some intermediate, we might compute for multiples? But we only compute i for start and end, which are not multiples, so safe.
Alternatively, we can compute i = floor( (double) u / (2a) ) using double and floor, but with large numbers up to 2e9 maybe, double might have precision issues but probably fine? However, to be safe, use integer arithmetic.
Let's define:
long long div_floor(long long a, long long b) { // b>0
if (a >= 0) return a / b;
else return (a - b + 1) / b;
}
But check: for a = -3, b=4: (-3-4+1) = -6, /4 = -1 (since integer division truncates toward zero). That's floor(-0.75) = -1, correct. For a = -5: (-5-4+1) = -8, /4 = -2, correct.
Edge: a = -4: (-4-4+1) = -7, /4 = -1 (since -7/4 = -1 in C++? Actually -7/4 = -1 because truncation toward zero, but floor(-1) = -1, correct as -4/4 = -1, floor = -1. So it's okay even if a is multiple, but we don't rely on it.
Now, compute u = x+y, v = x-y. But careful: x and y can be up to 1e9 in absolute, so u and v can be up to 2e9, and also negative down to -2e9. 2a and 2b up to 2e9, but a,b up to 1e9, so 2a up to 2e9. Multiplying? We'll use 64-bit (long long).
Now, compute i1 = div_floor(u1, 2a); i2 = div_floor(u2, 2a); similarly j1 = div_floor(v1, 2b); j2 = div_floor(v2, 2b).
Answer = max(abs(i2-i1), abs(j2-j1)).
Check examples: They match.
Now, think about potential pitfalls: Are there any cases where we need to consider the absolute values in the condition differently? The condition uses |x+y| and |x-y|. So if u is negative, we consider absolute value. But u being multiple of 2a is independent of sign; our condition u ≡ 0 (mod 2a) already captures that because if u = -4, then u mod 2a = -4, which is 0 mod 2a? Typically we say u is divisible by 2a if u % (2a) == 0, which holds for negative multiples as well in C++ if we use % operator? In C++, (-4) % 4 = 0? Actually -4 % 4 = 0 in C++ (since remainder has sign of dividend). So it's fine. So our mapping is correct.
Now, we also need to consider the parity condition u ≡ v (mod 2). The crossing strategy relies on being able to move to points with appropriate parity. Is there any scenario where the required crossing (e.g., diagonal) cannot be performed because the necessary corner is not reachable due to parity? For instance, to exit a rectangle via a particular corner, we need to be at a point adjacent to that corner inside the rectangle, which must have u odd, v odd (for the corner at even,even). That point exists as argued (since near each corner there is a point with both coordinates odd? Let's check: For corner (2a(i+1), 2b(j+1)), the adjacent interior point with u = 2a(i+1)-1, v = 2b(j+1)-1 is both odd if 2a(i+1) is even, minus 1 gives odd; same for v. So that point has odd/odd, which satisfies u≡v mod2. So it's a valid point in the original lattice because u and v have same parity (odd). So reachable. Similarly for other corners: e.g., corner (2a(i+1), 2bj). Adjacent interior point: (2a(i+1)-1, 2bj+1). u odd, v odd? 2bj is even, +1 = odd. So yes, both odd. For (2ai, 2b(j+1)): (2ai+1, 2b(j+1)-1) both odd. For (2ai, 2bj): (2ai+1, 2b*j+1) both odd. So each corner has an adjacent interior point with both coordinates odd. So from anywhere in the rectangle, we can navigate to that point, because the interior is connected. However, we must ensure that interior connectivity includes points with odd/odd? Not all interior points have odd/odd; some have even/even? Let's see: interior points can have u even? u even but not multiple of 2a; for a=2, u=2 is even but not multiple of 4. However, u=2, v=2 gives even/even. That's also a valid point (since u≡v mod2). So interior includes both parity types? Actually u and v must have same parity, so either both even or both odd. So interior includes points of both parity types. Since we can move from an even/even point to an odd/odd point in two steps? Let's check: (2,2) -> (3,3) is (+1,+1) okay, (3,3) is odd/odd. So connectivity includes both. So we can reach the desired odd/odd point.
Thus, the crossing strategies are viable.
Now, can there be a case where the minimal cost is less than max(|Δi|,|Δj|) due to possibility of crossing more than one line at a time in a single step? We already considered that crossing two lines simultaneously is possible, but that's pairing one vertical and one horizontal. To reduce total below max, we would need to pair more than one vertical with one horizontal, which is impossible because each step only changes u by 1. So cannot reduce further.
Therefore, answer is max(|Δi|,|Δj|).
Now, we must confirm that start and end not being bad ensures that i1,i2,j1,j2 are correctly defined (since u not multiple, v not multiple). So we are safe.
Edge Cases:
-
Large values: up to 1e9, calculations within 64-bit.
-
a and b can be up to 1e9, so 2a and 2b up to 2e9, which fits in 64-bit signed (since 2e9 < 9e18). Multiplication might not be needed; we just need to do division.
-
Note: In C++, if we use int, 2a might overflow if a up to 1e9 (2e9 fits in 32-bit signed? 2,000,000,000 fits in signed 32-bit int? INT_MAX is 2,147,483,647, so 2e9 fits. However, a can be up to 1e9, 2a=2e9 fits. b same. But u = x+y can be up to 2e9, also fits. But we might need long long to be safe with intermediate multiplication? Actually 2a is within int if int is 32-bit and a up to 1e9, but 2*a = 2e9, which is less than INT_MAX, so fine. However, to avoid overflow in operations like (u - 2a + 1) when u is negative large, we might have u - (2a) could be below -2e9, but still within 32-bit? u can be as low as -2e9, 2a up to 2e9, so u - 2a + 1 could be as low as -4e9, which is less than INT_MIN (-2,147,483,648). That could overflow 32-bit signed. So we must use 64-bit (long long) for safety. Indeed, u = -2e9, 2a = 2e9, then u - 2a + 1 = -4e9 + 1 = -3,999,999,999? Actually -2e9 - 2e9 = -4e9, plus 1 = -3,999,999,999. That's about -4e9, which is less than -2^31 = -2,147,483,648, so overflow. So we need 64-bit. So we'll use long long.
Implementation:
Read a,b,x1,y1,x2,y2 as long long.
Compute u1 = x1+y1; v1 = x1-y1; u2 = x2+y2; v2 = x2-y2.
Define function floor_div(val, d) where d>0:
if (val >= 0) return val / d;
else return (val - d + 1) / d; // using integer division truncates toward zero.
But careful: In C++, for negative numbers, (val - d + 1) / d might still have overflow issues if val is very negative. But we can compute using (val + 1) / d - 1? Another common formula: floor_div(a,b) = (a >= 0) ? a/b : (a - b + 1)/b. It's okay if we use 64-bit.
Better: Use (val - (d-1)) / d? Actually need to think: For negative a, floor(a/b) = -ceil(-a/b). But maybe easier: use (long long) floor((double)val / d) but double might have precision issues for large numbers near 2e9? double can represent integers up to 2^53 exactly (~9e15), so 2e9 is fine. So we could use double safely. However, to avoid floating point altogether, we can use the formula.
Let's test with extreme values: val = -2000000000, d=2000000000 (2a could be 2e9). Then val - d + 1 = -2000000000 - 2000000000 + 1 = -3999999999. That's within 64-bit signed? 64-bit signed max is 9e18, so it's fine. So using long long we are safe.
Now compute:
long long i1 = floor_div(u1, 2a);
long long i2 = floor_div(u2, 2a);
long long j1 = floor_div(v1, 2b);
long long j2 = floor_div(v2, 2b);
ans = max(abs(i2-i1), abs(j2-j1));
Print ans.
But wait, is it possible that the minimal number of bad cells could be less than max(|Δi|,|Δj|) due to the ability to cross lines at points that are not considered because of absolute values? No.
Double-check with some random cases.
Case: a=2, b=3, start (x1,y1) = (0,1): u1=1, v1=-1; end (x2,y2) = (7,0): u2=7, v2=7. Compute i1 = floor(1/4)=0, i2 = floor(7/4)=1 (since 7/4=1.75 floor=1). j1 = floor(v1/(2b)= -1/6? 2b=6. floor(-1/6) = floor(-0.166..) = -1. j2 = floor(7/6)=1? 7/6=1.166 floor=1. So Δi=1, Δj=2, max=2. So answer 2. Could we do with 1? Need to cross one horizontal and two vertical? Actually |Δj|=2, |Δi|=1. We need to cross 2 horizontal lines (since j changed by 2) and 1 vertical line. The minimal cost would be at least 2, because need to cross 2 horizontal lines. Can we cross both horizontal lines and the vertical line using two bad squares? For instance, cross one horizontal and the vertical together (cost 1) and then the other horizontal separately (cost 1) => total 2. So max works.
Consider a=2,b=2, start (x1,y1)=(0,1) u=1,v=-1; end (x2,y2)=(4,5) u=9,v=-1. i1=0, i2= floor(9/4)=2, j1= floor(-1/4)=-1, j2=-1. Δi=2, Δj=0, max=2. Path: need to cross two vertical lines at u=4 and u=8. Can we cross both with one bad square? Possibly by crossing at a corner where also horizontal? But j unchanged, so no horizontal crossing. So need at least 2. Could we cross both at same point? Not possible because they are separate lines. So 2 is correct.
Now, consider a case where Δi and Δj have opposite signs but large magnitudes: a=2,b=2, start (0,1) u1=1,v1=-1 (i=0,j=-1). end (5, -4) => x2=5,y2=-4, u2=1, v2=9? Actually 5+(-4)=1, v2=5-(-4)=9. i2=0, j2= floor(9/4)=2. Δi=0, Δj=3? Actually j1=-1, j2=2 => Δj=3. So max=3. Could we do less? Need to cross three horizontal lines. No way to combine with vertical because Δi=0. So 3.
All good.
But there is a subtlety: The cell index calculation using floor(u/(2a)) might not correctly count the number of lines between u1 and u2 if they are on opposite sides of zero? Let's test a scenario: a=2, u1 = -3, u2 = 3. i1 = floor(-3/4) = -1, i2 = floor(3/4)=0. Δi = 1, so max maybe 1. But are there any lines between -3 and 3? Multiples of 4: 0 is a multiple, and -4, 4. The interval [-3,3] includes 0, which is a multiple. So indeed one line to cross. So correct.
What about u1 = -5, u2 = 1. i1 = floor(-5/4) = -2, i2 = floor(1/4)=0. Δi=2. Lines between -5 and 1: multiples: -4 and 0. Two lines. Yes.
What about u1 = -1, u2 = 2. i1 = floor(-1/4) = -1, i2 = floor(2/4)=0. Δi=1. Lines between -1 and 2: 0 is a multiple, so one line. Correct.
Thus, floor works.
Now, confirm that the parity condition doesn't force an extra crossing when the number of steps parity might force you to land on a line even if you don't intend to? For example, suppose we want to move from a start point to an exit point inside the rectangle, but the parity of the number of steps required to get to the exit might force you to step on a line? But we can always find a path within the safe interior that avoids lines, as argued. Let's examine a small rectangle: a=2, rectangle (0,4)x(0,4). Interior points: all (u,v) with u,v ∈ {1,2,3} and u≡v mod2. Suppose start at (1,1) (u1=1,v1=1) and we want to reach (3,1) (u=3,v=1). (3,1) has u=3 odd, v=1 odd, parity ok. Can we go from (1,1) to (3,1) without stepping on a line? Moves: (1,1) -> (2,2) -> (3,1) (using (+1,+1) then (+1,-1)). All intermediate (2,2) is safe? u=2 not multiple (4), v=2 not multiple, safe. So yes. What about (1,1) to (1,3): (1,1)->(2,2)->(1,3)?? (2,2) to (1,3) is (-1,+1) gives (1,3) safe. So okay.
What about (2,2) to (3,3): (2,2)->(3,3) safe. So connectivity seems fine.
But consider a rectangle where 2a is larger, say a=3, 2a=6. Interior u from 1 to 5. v from 1 to 5 (if b=3). Points with u≡v mod2. So possible points: (1,1),(1,3),(1,5); (3,1),(3,3),(3,5); (5,1),(5,3),(5,5). Also (2,2),(2,4),(4,2),(4,4) if they satisfy parity? (2,2) even/even ok; (2,4) even/even ok; etc. So many points. Edges are between points that differ by (±1,±1). So (2,2) connects to (1,1),(1,3),(3,1),(3,3). So graph is connected.
Thus, fine.
Now, one more nuance: The transformation to u,v and the definition of cells rely on u and v being independent. However, there is a coupling through parity. Could it be that some combinations of i and j are unreachable from start due to parity of i and j? Not relevant; we only need to change i and j by certain amounts, not the absolute indices. The parity of the cell indices might affect the ability to use diagonal moves? Let's think: The path we construct uses a sequence of steps: inside rectangle, go to a corner-adjacent point, then cross to the corner (bad), then step into new rectangle. After crossing, we are at a corner (bad). Then we step into interior. After that, we may need to continue to another crossing. But note that after stepping from a corner into the interior, we are at a point adjacent to the corner. That point will have odd coordinates (since corner even, plus 1 => odd). So after a diagonal crossing, we end up at an interior point with odd/odd. After an axis-aligned crossing (vertical), we end up at a side point (bad) then step into interior. After stepping from side to interior, the interior point will have u odd (since from even line +1 = odd) and v odd? Let's see: after vertical crossing, we land on a point with u even (multiple), v even (to satisfy parity). Then we step into the new rectangle: we move u by +1 (away from line) and v by ±1. So new u becomes odd; v becomes odd (since even ±1 = odd). So interior point after a vertical crossing also becomes odd/odd. Similarly after horizontal crossing, we land on v multiple even, u even, then step: u ±1 -> odd, v +1 -> odd. So interior point becomes odd/odd as well. After diagonal crossing, we go from corner (even,even) to interior by step (+1,+1) (or appropriate) giving odd,odd. So after any crossing, if we take one step into the interior, we end up at an odd/odd point.
Now, suppose we need to perform another crossing immediately. For the next crossing, we need to position ourselves appropriately near the desired exit point. If the next crossing is another diagonal, we need to get to a corner-adjacent point which is odd/odd (since corner-adjacent points are odd/odd). So after previous crossing we are at odd/odd interior point. So we can then move within the rectangle to the desired odd/odd corner-adjacent point. That seems fine. If the next crossing is a vertical crossing, we need to get to a point with u = 2a*(i+1)-1 (odd) and v odd? Actually for vertical crossing, we need to be at a point with u just left of the line, and v odd. That point has u odd (since one less than even), v odd. So again odd/odd. So we can navigate to it. After a horizontal crossing, need point with u odd, v just below/above line, i.e., u odd, v odd? For crossing horizontal line upward, we need v = 2b*(j+1)-1 (odd), u odd. So again odd/odd. So all exit points we need are odd/odd. And after each crossing and entering new rectangle, we end up at odd/odd. So we can chain crossings without extra parity issues.
But what about the start point? Start is not bad, but its parity could be even/even or odd/odd? Since u1 and v1 must have same parity. They could be both even or both odd. If start is even/even, can we still achieve the plan? The first crossing might require us to be at an odd/odd point to exit. If start is even/even, we can first move within the starting rectangle to an odd/odd point without stepping on bad squares. Is that always possible? The interior contains both even/even and odd/odd points? For rectangle interior, does it contain odd/odd points? For the interior of a rectangle, u ranges from L+1 to R-1. If L is multiple of 2a (even), then L+1 is odd, R-1 is odd if R is even? R = 2a(i+1) even, so R-1 odd. So u can be odd. Similarly v can be odd. So odd/odd points exist (e.g., (L+1, B+1) if B is even). But does that point satisfy u≡v mod2? Both odd, yes. So it's in the interior. Can we reach it from an even/even start point? Since both are in the same connected component, yes. For example, start at (2,2) even/even in rectangle (0,4)x(0,4). We can go (2,2)->(3,3) (odd/odd) safely. So we can always move to an odd/odd interior point before the first crossing. So no issue.
Thus, the plan works for all.
Now, we need to confirm that the answer is indeed max(|Δi|,|Δj|) and not something else like the Manhattan distance in terms of lines? Could there be a scenario where we can cross a vertical line without incurring a bad square by stepping over it in such a way that we never land exactly on the line? Since we move on integer grid, and the line is at integer u = multiple of 2a. When moving from u = k2a - 1 to u = k2a + 1, that's a change of +2, but not allowed because step size is 1. So you must land on the intermediate integer. Therefore, crossing forces a bad square.
But what about moving in a way that you cross the line but the point you land on is not considered bad because of absolute value? No, if u = 2a*k, then |u| is multiple, so it's bad. So yes.
Now, could there be a scenario where the start and end are in the same cell but the path must cross a line because of parity constraints? For example, start and end are both in the same rectangle, but maybe due to parity, to go from one point to another you might be forced to step outside the rectangle? However, as argued, the interior is connected, so you can stay inside. So no.
Thus, answer = max(|Δi|,|Δj|).
But wait: Let's double-check the transformation to cell indices. Are we sure that the number of lines that must be crossed equals the difference in these floor values? Consider u1 = 3, u2 = 5, a=2: i1=0,i2=1 diff=1, line at 4. Yes. Consider u1 = -3, u2 = 5: i1 = floor(-3/4) = -1, i2=1 diff=2, lines at 0 and 4. Yes. Consider u1 = -5, u2 = -1: i1 = -2, i2 = -1 diff=1, line at -4. Yes.
But what about u1 = 1, u2 = 7, a=3 (2a=6): i1=0, i2=1 (since 7/6=1.166 floor=1) diff=1. Lines: multiples of 6: 0,6,12,... Between 1 and 7, there is 6, so one line. Good. u1=5, u2=11: i1=0 (5/6=0.833 floor=0), i2=1 (11/6=1.833 floor=1) diff=1, line at 6. Good.
Now, if u1 and u2 are in intervals that are not adjacent but with i difference >1, there will be multiple lines. So diff counts them.
Now, is it possible that the minimal number of bad squares is actually less than the number of line crossings because sometimes you can cross a line at a point that is not counted as bad? No, any point on the line is bad.
Thus, answer stands.
Now, let's test a few more cases manually to be safe.
Case: a=2,b=3, start (x1,y1) = (0,0)? But start cannot be bad? Condition: |0+0|=0 mod 4? 0 mod4 -> bad. So not allowed. So we need start not bad. So pick start not on lines.
Say a=2,b=3, start (1,0): u=1, v=1. end (7,1): u=8, v=6. Check start: u=1 mod4 not 0, v=1 mod6 not 0, good. End: u=8 mod4=0 -> bad? Actually u=8 is multiple of 4, so end would be bad, but problem says final square isn't bad. So need end not bad. So choose end not on lines.
Let's pick start (1,0) u=1,v=1; end (6,2): u=8? 6+2=8 bad. So maybe end (5,2): u=7, v=3. u=7 mod4=3, v=3 mod6=3, ok. Compute i: u1=1/4=0, u2=7/4=1 (floor=1), Δi=1. j1 = v1=1/6=0, j2=3/6=0? floor(3/6)=0, Δj=0. So answer =1. Path: need to cross one vertical line at u=4. Can we do it with 1 bad square? Yes.
Now, consider case where a and b are not equal, and the rectangle dimensions differ. Should still work.
Now, what about a case where the start and end are in cells such that Δi and Δj have same sign but we could potentially cross all lines with fewer than max if we could cross multiple vertical lines at once? Not possible.
Thus, solution appears simple.
But wait, could there be a scenario where the minimal number of bad squares is not max(|Δi|,|Δj|) but rather something like the Manhattan distance in the "grid of cells"? For example, if we had to cross lines but could avoid some by taking a path that goes through a corner that lies on both a vertical and horizontal line, but we might be able to cross two vertical lines and one horizontal with two bad squares? That would be max=2? Actually if Δi=2, Δj=1, max=2. Can we achieve with 2? Yes: pair one vertical with the horizontal (cost 1) and the remaining vertical separately (cost 1). So total 2 = max. If we tried to do all separately: 3, worse. So max is achievable.
If Δi=2, Δj=2, max=2. Could we do with 2? Pair two vertical with two horizontal: we need to cross two vertical and two horizontal. Could we cross both pairs simultaneously? Possibly by two diagonal moves: first diagonal from (i,j) to (i+1,j+1) (cost 1), then another diagonal to (i+2,j+2) (cost 1). That yields total 2. So yes.
Thus, max is achievable.
Now, is there any scenario where we cannot pair a vertical and horizontal because the signs differ? But diagonal moves can be in any direction (NE, NW, SE, SW). So if Δi positive, Δj negative, we can do diagonal moves that increase i and decrease j. So still pair.
Now, confirm with a more complex example: Δi=3, Δj=-1, max=3. How to achieve with 3? Pair 1 vertical with the horizontal using a diagonal (cost 1). Then remaining 2 vertical separately (cost 2). Total 3. Alternatively, could we do 2? Not possible because we need to cross at least 3 vertical lines. So ok.
Thus, answer is max(|Δi|,|Δj|).
Now, we should also consider if the answer could be 0 when start and end are in same cell. That matches.
Now, note: The problem statement says "minimum number of bad cells one will have to visit on the way from (x1; y1) to (x2; y2)." Does visiting include the start and end? Usually "on the way" excludes start and end. But the problem guarantees start and end are not bad, so they are not counted. Our crossing costs count only intermediate bad squares. In our constructed path, we count only the crossing points (which are bad). We do not count start or end. So fine.
Now, potential edge case: If the start and end are in the same cell but the direct path might be forced to go through a bad square because of parity? Example: a=2,b=2, start (1,0) => u=1,v=1; end (0,1) => u=1,v=-1. Both in same cell? u=1 in (0,4), v=1 and v=-1? v=-1 is in cell (-4,0) because v=-1 ∈ (-4,0). So they are in different cells actually. So not same.
Consider same cell: start (1,0) u=1,v=1; end (2,1) u=3,v=1? Actually (2,1): u=3, v=1. Both u in (0,4), v in (0,4). So same cell. Can we go from (1,0) to (2,1) without bad squares? Path: (1,0)->(2,0)->(2,1). Check (2,0): u=2, v=2, not bad. So yes.
Thus, same cell works.
Now, we need to implement floor division carefully.
Implementation plan:
- Use long long for all variables.
- Read a,b,x1,y1,x2,y2.
- Compute u1 = x1+y1; v1 = x1-y1; u2 = x2+y2; v2 = x2-y2.
- Compute twoA = 2a; twoB = 2b.
- Define function floorDiv(long long val, long long d) {
long long r = val / d; // C++ division truncates toward zero
if (val >= 0) return r;
else {
// If val is exactly divisible? Not needed but safe.
if (val % d == 0) return r; // because -4/4 = -1, which is floor? floor(-1) = -1, correct.
else return r - 1; // because truncation toward zero yields ceil for negative? Let's derive.
}
}
But careful: In C++, for negative numbers, division truncates toward zero. For val negative and not divisible, e.g., val=-5, d=4, -5/4 = -1 (since truncation toward zero). But floor(-5/4) = -2. So we need to subtract 1. For val negative and divisible, e.g., val=-4, -4/4 = -1, floor(-1) = -1, so no adjustment. So the function can be:
if (val >= 0) return val / d;
else {
long long t = val / d; // trunc toward zero
if (val % d == 0) return t;
else return t - 1;
}
But using the formula (val - d + 1)/d might be simpler and avoid modulo check, as it works even for divisible? Let's test with val=-4,d=4: (-4 -4 +1)/4 = (-7)/4 = -1 (trunc toward zero) which is -1, correct. val=-8,d=4: (-8-4+1)/4 = (-11)/4 = -2 (since -11/4 = -2.75 trunc toward zero => -2) floor(-2) = -2, correct. So it works for all. So I'll use that.
But note: if val is very negative, val - d + 1 could overflow? But using 64-bit, it's fine.
Thus:
auto floorDiv = [](long long a, long long b) {
return a >= 0 ? a / b : (a - b + 1) / b;
};
Now compute:
i1 = floorDiv(u1, twoA);
i2 = floorDiv(u2, twoA);
j1 = floorDiv(v1, twoB);
j2 = floorDiv(v2, twoB);
ans = max(abs(i2-i1), abs(j2-j1));
Print ans.
Testing with examples.
Example1: a=2,b=2 => twoA=4,twoB=4. u1=1, floorDiv(1,4)=0; u2=1 =>0; v1=1=>0; v2=-1=> floorDiv(-1,4) = (-1-4+1)/4 = (-4)/4 = -1; so ans = max(0,1)=1. Good.
Example2: a=2,b=2: u1=21=>5; u2=1=>0; v1=-1=>-1; v2=-1=>-1; ans = max(5,0)=5. Good.
Example3: a=2,b=4 => twoA=4,twoB=8. u1=2