We need to solve: given integer a, find smallest positive integer b such that a + b is lucky (contains digit '8' in its decimal representation). Lucky numbers can be negative, but the definition: "an integer is lucky, if and only if its decimal notation contains at least one digit '8'." For negative numbers, presumably we consider the decimal notation including the negative sign? Usually digits of negative numbers: the absolute value's digits; e.g., -180 contains digit '8'? The example: "- 180, 808 are all lucky". So they consider that -180 contains digit '8'. So we can treat it as: let n = a + b; then the absolute value of n's decimal representation contains at least one '8'. But careful: number 0? Does 0 contain digit 8? No. So lucky numbers are non-zero? Actually 0 doesn't have digit 8. So floor 0 is not lucky.
Constraints: a is between -10^9 and 10^9, but b could be larger? The problem statement says: "find the minimum positive integer b, such that, if he walks b floors higher, he will arrive at a floor with a lucky number." Since floor numbers go from -8,888,888,888 to 8,888,888,888, there is a limit. But in the problem it's not said that b must be such that a+b stays within that range? Probably yes, but we can assume we only care about valid floors, but the range is huge (17.7e9 floors). But a is up to 1e9, so b could be up to maybe 8e9? Actually need to ensure a+b within that range? Probably we can assume there is always a lucky floor above, since the tower has many floors. But given constraints a up to 1e9, and the upper bound is 8,888,888,888, we can always go up to that. But b might need to be large. But we can solve without brute forcing.
We need minimal b>0 such that (a+b) contains digit '8'.
We could simulate b from 1 upward until lucky. But a can be up to 1e9, but worst-case the next lucky number might be far? Let's consider the maximum gap between consecutive numbers containing digit 8. In decimal, numbers without digit 8 (called '8'-free) have some density. Among numbers up to 10^k, fraction without 8 is (9/10)^k? Actually digits 0-9 except 8? But leading digit can't be 0? For positive numbers, careful: numbers that do not contain digit 8. Among all numbers from 0 to 10^k -1, all combinations of k digits (including leading zeros) yields 10^k numbers; those without digit 8: 9^k (each digit from 0-9 except 8). So density ~ (9/10)^k. For k=9 (since up to 1e9, 10 digits?), Actually 1e9 is 10^9, so 10-digit numbers? But numbers up to 1e9 have at most 10 digits? Actually 1,000,000,000 is 10 digits. But density ~ (0.9)^10 ≈ 0.348, so gaps can be moderate. But if a is negative, we have to consider negative numbers? For negative numbers, we consider absolute value? The rule: lucky if decimal representation contains '8'. For negative numbers, the representation includes a minus sign then digits. So it's about the digits after the sign. So essentially we care about the absolute value's decimal representation containing an 8. So we can define f(n) = true if the decimal representation of |n| contains the digit '8'. For n=0, it contains no 8. So same.
Thus we need smallest b>0 such that |a+b| contains '8'. However note that if a+b is negative, its absolute value is -(a+b) (positive). So essentially, we can treat the condition on absolute value, but careful: For a+b = -8, it's lucky because it contains 8? Yes, as per example, -180 is lucky, which has absolute value 180 contains 8. So indeed condition is: the decimal digits of the absolute value of the number (ignoring sign) contain '8'. So we need the absolute value to contain digit 8.
But what about a+b = -0? Not relevant.
So problem reduces to: Given integer a, find smallest positive integer b such that the integer N = a + b has the property that its absolute value's decimal representation contains the digit '8'. Or equivalently, N is not in the set of numbers that do not contain digit 8 (including zero). For negative numbers, we check absolute value.
We can think of scanning from a+1 upwards, but we need to consider also potentially negative numbers? Since b positive, we only go higher, i.e., increasing the floor number. So if a is negative, going higher means moving towards zero and then positive. For example, a = -1, b = 9 gives 8 (positive). That's lucky. Could there be a smaller b? Let's check: -1+1=0 (not lucky), -1+2=1 (no 8), ... -1+8=7 (no 8), -1+9=8 (lucky). So answer 9. So we only consider numbers greater than a.
Thus we need to find the next integer > a that is lucky (contains digit 8). But careful: The "next integer greater than a" that is lucky. However, need to ensure that we also consider negative numbers if a is negative? Yes, because we are going upward. So we simply find the smallest integer n > a such that containsDigit8(|n|). That's it.
But is there any nuance with negative numbers? For negative numbers, the floor numbers are like ... -3, -2, -1, 0, 1, 2,... So increasing from negative to positive passes through zero. So indeed we need to check numbers like -1, 0, 1,... So absolute value condition holds.
Thus problem is to compute the smallest n > a with digit 8 in its absolute value.
Given a up to 1e9, n could be as high as maybe a+some gap. The gap can be up to maybe 20? Actually worst-case gaps: numbers without digit 8 can be quite large? Let's think about the maximum gap between consecutive numbers that contain digit 8. But we are looking for numbers that contain digit 8; the complementary set (non-lucky) are numbers without digit 8. So we need the distance from a to the next number that contains digit 8. Since the density of lucky numbers is 1 - (9/10)^d? Actually the density for numbers containing at least one 8 among all numbers of up to d digits? But across the whole number line, the set of numbers without digit 8 is infinite? Actually there are infinitely many numbers without digit 8, like powers of 2? But they become rarer as digits increase. But there will always be numbers without digit 8, for any number of digits, e.g., all digits 1,2,...9 except 8? But digit 8 is only missing, you can have numbers like 1, 10, 100, etc. So there are arbitrarily long runs of numbers without digit 8? Let's examine: Among numbers of a given length, the numbers without 8 are plentiful. But consecutive numbers may cross digit boundaries. The largest gap between numbers containing an 8 can be quite large? Consider numbers just before a power of 10 that is all 9's? For instance, 79, 80: 79 no 8? Actually 79 no 8, 80 contains 8? 80 has digit 8? Yes, contains 8? 8 is the tens digit, so 80 is lucky. So gap from 79 to 80 is 1. But what about 199999? That contains 8? No. Next number 200000? No. But somewhere soon? Actually need to find a long run without any digit 8. This is similar to numbers that avoid digit 8. The longest run of consecutive numbers without 8 might be when you have a block of numbers where each number avoids 8. For example, consider numbers from 0 to 9? Many of them avoid 8 except 8 itself. So max gap from 7 to 8? That's 1. But consider numbers from 80 to 89: all contain 8? Actually 80 to 89: tens digit is 8, so all contain 8. So there is a block of 10 consecutive lucky numbers. The gaps can be longer: For example, from 79 to 80: gap 1 (79 not lucky, 80 lucky). But what about from 77 to 78: 78 contains 8? No, 78 doesn't contain 8. 79 no. 80 lucky. So the run of non-lucky numbers from something to something may be longer. Actually we need to examine the pattern: The set of numbers without digit 8 is like numbers expressed in base 9 (with digits 0-7,9? Wait, digits allowed: 0,1,2,3,4,5,6,7,9. That's 9 digits. So there is a mapping to base-9 numbers. However, the distribution is not uniform because the digit set includes 9 but not 8. And the numbers are in base-10 but using these digits. The numbers that avoid 8 are dense? The density decays as (9/10)^k for k-digit numbers. So there will be arbitrarily large gaps? Actually consider all numbers up to 10^k. The proportion of numbers without 8 is (9/10)^k. As k increases, this goes to 0, but there are still infinitely many numbers without 8. However, the gaps between consecutive numbers that contain an 8 might become larger on average? Let's think: As numbers get larger, the numbers without 8 become sparser. But consecutive numbers without 8 may be separated by numbers that contain 8. In fact, the maximum possible gap between numbers that contain an 8 likely grows as you go higher. Because you can have a block of numbers with many digits where none contain 8, like the range from 0 to 10^k -1: actually many numbers within that range avoid 8, but also there are numbers containing 8 within that range. But can we have a run of, say, 100 consecutive numbers without 8? Possibly yes, if we consider numbers like 1,000,000,000? Actually think: The numbers without 8 are those whose decimal representation uses only digits from the set D = {0,1,2,3,4,5,6,7,9}. This set is of size 9. Numbers that avoid 8 are not closed under consecutive increments because carrying can introduce an 8. For example, consider numbers like 199999. Add 1 gives 200000. Neither contains 8? 199999 digits: 1,9,9,9,9,9 - no 8. 200000 digits: 2,0,0,0,0,0 - no 8. So both avoid 8. So we can have consecutive numbers avoiding 8. The question is: what is the longest possible run of consecutive numbers that avoid digit 8? This is similar to the concept of "digit 8-free runs". Since we have 9 allowed digits, and the carry process can skip over digit 8. The longest run would be when we have a number like ...? Let's think: When we increment a number, if we cross a digit that becomes 8, we get an unlucky number? Actually we want to avoid 8. So the run ends when we hit a number that contains 8. So we need to find the longest consecutive sequence of numbers with no 8.
Consider numbers that are all comprised of digits 0-7 and 9, but note that digit 9 is allowed, and when you increment a number ending with 9, you get a carry that may produce a digit 8? For example, ...9 +1 => ...0 with carry. The carry may cause some digit to increase. If that digit was 7, it becomes 8 - which is not allowed. So if we have a number ending with a sequence of 9's and the next digit is 7, adding 1 will turn that 7 into 8, producing an 8. So that would break the run. So to avoid creating an 8, when we have a carry, the digit that is incremented must not become 8. That means that any digit that receives a carry must be not 7 (since 7+1=8) and also if it becomes 9? Actually digits allowed: 0-7,9. So if digit is 9, adding 1 with carry? Wait, if a digit is 9 and we add 1 (maybe from carry), then 9+1=10, causing a carry to next digit, and this digit becomes 0. But 0 is allowed. However, the carry itself may propagate. The critical point is when a digit is 7 and we add 1, it becomes 8, which is forbidden. So runs of consecutive numbers without 8 can only contain numbers where during increments we never hit a digit 7 that gets incremented. So the run ends when we encounter a number that either already contains an 8 or when a carry produces an 8.
Thus the longest run might be limited. Actually, is it possible to have an arbitrarily long run of numbers without digit 8? Suppose we consider numbers that are all '9's except certain digits? Let's try to construct a long run: We want a sequence of consecutive integers all of which have no digit 8. Consider the numbers from 0 up to ... But obviously, 8 itself contains 8, so run from 0 to 7 is length 8. From 9 to 17? 9-17: check 9,10,11,12,13,14,15,16,17: all no 8? 10 has digits 1,0 no 8; 11; ... 17 no 8. 18 contains 8, so run length 9? Actually from 9 to 17 inclusive is 9 numbers. But also 0-7 gives 8 numbers. The next run maybe from 19 to 27? 19-27: 19,20,21,22,23,24,25,26,27 all no 8? 28 contains 8. So length 9. So runs of length about 9 are common. But can we have longer runs? Consider numbers like 79-80: 79 no 8, 80 has 8, so run break. But what about 80-89 are all lucky, so the run of non-lucky on each side is short.
What about numbers around 100? 97-99: 97,98,99? 98 contains 8? Yes, 98 contains 8. So run broken. 99 contains 9,9 no 8, 100 has no 8. But 101,102,... up to 107? 108 contains 8. So from 100 to 107 is 8 numbers (100-107 inclusive =8). 108 lucky. So length 8.
But can we get longer than 9? Let's check around 199: 190-197? 190 has 9 and 0, no 8; 191; ... 197; 198 contains 8. So 190-197 length 8? Actually 190 to 197 inclusive is 8 numbers. But 199? 199 no 8, but 198 has 8, so there is a gap. So still length around 9.
But what about numbers with more digits? For example, consider numbers from 1000 to 1007? 1000-1007 (8 numbers) then 1008 contains 8. So length 8. But maybe there are runs of length 9 or maybe 18? Let's think about the pattern: The numbers that avoid digit 8 are those whose digits are from set D. When incrementing, the moment we hit a digit that becomes 8, the run ends. So the worst-case run length would be when we have a number like ...? To maximize the run, we want to avoid hitting any digit 7 that gets incremented to 8, and avoid any existing 8. The run can be as long as the distance to the next number that has a digit 8. This is like: given a starting number without 8, after how many increments do we inevitably create an 8? That depends on the position of the rightmost digit that is not 9? Actually incrementing works by adding 1 to the least significant digit, and if it becomes 10, carry, etc. The digits that can become 8 are: if a digit is 7 and we need to increment it (due to carry from lower digits all becoming 9?). So consider a number whose decimal representation ends with a sequence of digits that are all 9? Let's examine: Suppose we have a number like ...? If the number ends with a digit less than 8, then adding 1 yields a number with last digit increased by 1. If the last digit is 7, then 7+1=8, so that number contains 8, so the run would include numbers up to ...? Actually, if we start at a number that avoids 8, and we add 1, if the result contains 8, then the run stops at the previous number. So to have a long run, we need that for many increments, the result does not contain 8. That means for each increment, the resulting number must avoid 8. That requires that whenever we have a carry that increments a digit, that digit is not 7 (and not 8 obviously). Also, the resulting number must not have a digit 8 in any other place that might appear as a result of the increment? Only digits that change are those involved in the carry chain. So the dangerous digit is 7. So to avoid generating an 8, we must avoid the situation where a digit 7 receives a carry. That implies that the digits to the right of the digit that would be incremented must all be 9, causing a carry to propagate to that digit. So the run will end when we encounter a number that has a digit 7 and all less significant digits are 9, because adding 1 will turn that 7 into 8. So the longest run may be determined by the largest gap between numbers of the form ...? Actually, the numbers that contain an 8 are exactly those that either have an 8 already, or are produced by an increment that creates an 8. But the latter are already numbers containing an 8. So the set of numbers that contain an 8 is just numbers with digit 8. So the complementary set (no digit 8) includes numbers that have no 8. So we are asking: what is the maximum length of a consecutive block of numbers that avoid digit 8? This is a known problem: the maximum run of numbers not containing a certain digit is limited? Actually, consider numbers written in base 10, avoiding digit 9? Not sure. But we can reason: Since numbers without digit 8 can be mapped to base 9 numbers (digits 0-7,9 corresponds to digits 0-8 if we map 9->8?). But note that the digit 9 is allowed, so the mapping is not straightforward to base 9? However, we can map digits {0,1,2,3,4,5,6,7,9} to digits 0-8 by mapping 9 to 8. But then the order is not preserved as numerical order? Actually, if we take a number without 8 and replace all 9's with 8's, we get a number in base 9? But the transformed number would be interpreted in base 10? Not exactly. The set of numbers without digit 8 is isomorphic to all numbers expressed in base 9 using digits 0-8, but then interpreting that base-9 number as a decimal number yields a number whose decimal representation (in base 10) actually uses digits 0-9? Wait, confusion.
Better: Count of numbers without digit 8 up to a certain bound: they are like base-9 representation but with digits 0-7,9? Actually if we consider the set S = {numbers whose decimal digits are in D = {0,1,2,3,4,5,6,7,9}}. This is not simply base-9 because digit 9 is allowed, but we have 9 digits total, so it's essentially a base-9 system with digit set D, but D is not the standard digits 0-8. However, there's a bijection between numbers in base-9 (digits 0-8) and numbers in S, by mapping each digit d in 0-8 to the d-th smallest element of D. Since D sorted: 0,1,2,3,4,5,6,7,9. So map 0->0,1->1,...,7->7,8->9. So any number in S corresponds uniquely to a base-9 number by applying the inverse map. For example, the base-9 number 18 (which is 1*9+8=17 in decimal) maps to digits: 1 ->1, 8->9, so number 19? Actually 1 and 9 -> 19. So 19 in decimal is in S? 19 has digits 1 and 9, no 8, yes. And 19 is the decimal value. So the mapping is not order-preserving; 17 in decimal corresponds to base-9 number? Not directly.
But the runs of consecutive numbers without 8 may not be arbitrarily long. In fact, I recall that the longest run of decimal numbers without a particular digit (say 8) is 9? I'm not entirely sure. Let's test by searching manually for longer runs. We can write a small mental program: we want to find a block of length L>9 of consecutive integers all lacking digit 8. Suppose we have a number n such that n, n+1, ..., n+L-1 all avoid 8. Consider the last digit cycles every 10. Within a block of 10 consecutive numbers, one of them ends with digit 8 (like ...,8). So if the block length is >=10, then it must contain a number ending with 8, unless the block crosses a boundary where the tens digit changes? Actually, numbers ending with digit 8 always have last digit 8. So any block of 10 consecutive numbers (like from x0 to x9) includes the number with last digit 8. So if we have a block of length 10, it will include at least one number ending in 8. Unless the block does not cover all residues mod 10? For a block of 10 consecutive numbers, the set of last digits is exactly all digits 0-9. So it will include 8. Therefore, any block of 10 or more consecutive integers must contain a number whose last digit is 8. But wait, does a number whose last digit is 8 automatically contain digit 8? Yes, because the last digit is 8. So that number would be lucky, contradicting the block being all non-lucky. So the maximum possible length of a block of consecutive non-lucky numbers is at most 9. Is that always true? Let's check: If we have a block of 10 numbers, indeed they cover all residues mod 10, so one ends with 8. However, could there be a scenario where the block of length 10 but the number ending with 8 might have a carry that changes the tens digit, making the last digit not 8? Actually, last digit is determined solely by the number mod 10. For any integer n, its last digit is n mod 10. In a consecutive range of 10 numbers, the residues mod 10 will be all 0-9 exactly once. So there will be exactly one number whose last digit is 8. That number contains digit 8 regardless of other digits? Yes, because the last digit is 8. So it is lucky. So indeed any block of 10 consecutive numbers must contain a lucky number. Therefore, the maximum length of consecutive non-lucky numbers is 9. Good! That's a simple and important observation. This holds for positive numbers? What about negative numbers? For negative numbers, the concept of "last digit" is a bit tricky because the decimal representation of negative numbers includes a minus sign and then digits; but the last digit of -18 is 8? Actually, -18 has digits "1","8"? The last character is '8'? Usually the decimal notation for negative numbers is "-18", which ends with '8'. So it also contains digit 8. So the same logic: For any consecutive integers, the residues mod 10 cycle. If we consider negative numbers, the set of numbers from -10 to -1, they have last digits from 0 to 9? Actually, -1 ends with '1'? The representation "-1" ends with '1'; the absolute value's last digit is 1. But we care about the absolute value containing 8. However, the property of containing digit 8 is not determined solely by the last digit of the absolute value. But we can consider the absolute value's last digit. For any integer n, its absolute value |n| has a last digit that is the same as |n| mod 10, which is the same as n mod 10 up to sign? Actually, if n is negative, say n = -18, |n| = 18, last digit is 8. For n = -17, |n| = 17, last digit 7. So the mapping from n to its absolute value's last digit is: if n >=0, last digit = n mod 10; if n <0, then |n| = -n, and -n mod 10 = (-n) mod 10 = (10 - (n mod 10)) mod 10? Actually, careful: For negative numbers, e.g., n = -1, -n = 1, last digit 1. For n = -2, last digit 2. For n = -10, -n=10, last digit 0. So the absolute value's last digit is essentially the last digit of -n, which is not equal to n mod 10 generally. However, the set of absolute values of a block of consecutive integers might not have all residues 0-9. For example, consider the block from -5 to 4: numbers -5,-4,-3,-2,-1,0,1,2,3,4. Their absolute values: 5,4,3,2,1,0,1,2,3,4. The set of last digits: 5,4,3,2,1,0,1,2,3,4. So residues 0-5 appear, but 6,7,8,9 are missing. So it's possible that a block of 10 consecutive integers could have none whose absolute value ends with 8. But wait, does that guarantee that no number contains digit 8? Not necessarily; could contain digit 8 in higher places. But at least the simple argument that a block of length 10 must contain a number ending with 8 fails for negative numbers because the last digit of the absolute value does not cycle regularly across negative numbers. However, note that the problem only requires b positive, so we only consider numbers greater than a. If a is negative, we consider numbers from a+1 upward, which may include negative numbers and zero and positive numbers. So we need to consider the possibility that the next lucky number might be more than 9 steps away if a is negative? Let's test with a = -1, answer 9. That's less than 10. Could there be a case where answer > 9? Let's try a = -9? a = -9. Then a+1 = -8 (floor -8). Is -8 lucky? -8 contains digit 8? The absolute value is 8, contains 8, so yes. So b=1. So not >9. a = -10? a = -10. Then a+1 = -9 (abs 9, no 8), a+2 = -8 (abs 8, lucky). So b=2. a = -11? Then -10 (abs 10 no 8), -9 (abs 9 no 8), -8 (abs 8 lucky) => b=3. So up to a = -18? Actually, -18: a+1 = -17 (abs 17 no 8), ..., up to -8? Let's compute: from -18+1 = -17 to -8 inclusive? That's 10 numbers? -17, -16, -15, -14, -13, -12, -11, -10, -9, -8. Check each absolute value: 17,16,15,14,13,12,11,10,9,8. Among these, 8 appears only at -8. So b = 10? Because -18+10 = -8? -18+10 = -8, yes. So b=10? But check: -18+9 = -9, not lucky; -18+10 = -8 lucky. So answer would be 10. But is that allowed? Let's test the sample: For 18 (positive) answer was 10. For -18? Not given but let's compute. If a = -18, then the next lucky number greater than -18 is? Numbers greater than -18: -17, -16, ... -8, -7, ... The first lucky one is -8? But -8 is lucky, yes. So b = 10? But wait, is -8 greater than -18? Yes, -8 > -18. So b = (-8) - (-18) = 10. So answer would be 10. That's >9. But earlier we argued that any block of 10 consecutive numbers must contain a number ending with 8. However, in this block from -17 to -8 inclusive, the numbers are -17, -16, -15, -14, -13, -12, -11, -10, -9, -8. Their absolute values: 17,16,15,14,13,12,11,10,9,8. The ones with last digit 8: 8 has last digit 8, 18? Not present. So indeed there is exactly one number whose absolute value ends with 8: the number with absolute value 8, which is -8. But that number does have digit 8 (in its absolute value). So that block of 10 numbers includes a lucky number, but it's the last one. The distance from -18 to that lucky number is 10. But the block length of non-lucky numbers before hitting lucky is 9? Actually from -17 to -9 all non-lucky, then -8 lucky. So the maximum gap (number of steps to reach lucky) could be up to? Starting at a = -18, we need 10 steps to reach -8. So b=10. But could it be larger? Let's check a = -19. Then a+1 = -18 (abs 18, contains 8? 18 contains 8? Yes, digit 8 in 18. So -18 is lucky? 18 has digit 8, so -18 is lucky. So b=1. So not larger. So far max b=10 observed. Could it be larger than 10? Consider a = -80? a = -80. Then a+1 = -79 (abs 79 no 8), ... up to -71? Let's see: from -79 to -71: -79 (abs 79 no 8), -78 (abs 78 no 8? 78 does not contain 8? Actually 78 digits: 7 and 8? Contains 8? Yes, 78 has digit 8. So -78 is lucky. So b =? -80+2 = -78? Wait: -80+1=-79, -80+2=-78 lucky => b=2. So no.
What about a = -88? -88 is lucky itself? -88 contains 8, yes. But we need b positive, so a itself might be lucky, but we need b>0 such that a+b lucky. If a is lucky, we cannot stay; need to go higher, so we must find next lucky > a. So if a is lucky, the distance to the next lucky could be >9? Possibly. For example, a = 88. Next lucky? 88 is lucky, but we need a+b > a, so the next lucky after 88. The next number after 88 is 89, which contains 8? 89 has digit 8? Yes, tens digit 8. So b=1. But maybe after a lucky number, there could be a gap? Let's test a = 79 (non-lucky). Next lucky is 80, b=1. a = 87 (lucky? 87 contains 8? Yes, 8 is present). Next lucky >87 is 88 (contains 8), b=1. a = 188? Contains 8, next likely 189? 189 contains 8? No. Actually 189 digits: 1,8,9 -> contains 8, so b=1. So maybe after a lucky, always 1? Not necessarily; consider a = 199? 199 no 8. Next is 200? No 8. 201? ... up to 207? 208? Actually 208 contains 8? Yes, 208 contains 8. So distance from 199 to 208 is 9? 199+1=200, +... 207=207 no 8, +8=208. So b=9. That's 9 steps. But 199 is not lucky. So for lucky a, we need to go to the next lucky, skipping possibly some numbers. Could the gap between consecutive lucky numbers be more than 10? Suppose we have two lucky numbers L1 and L2, with L2 > L1, such that there are at least 10 non-lucky numbers between them? That would mean a block of at least 10 consecutive non-lucky numbers exists. But earlier we argued that any block of 10 consecutive numbers must contain a number ending with 8, which is lucky. However, that argument holds for positive numbers where we consider the number itself (not absolute value). For positive numbers, the last digit being 8 implies the number is lucky. So for positive numbers, any block of 10 consecutive positive integers must contain a lucky number. Therefore, the maximum gap between lucky numbers among positive integers is at most 9. But careful: Could there be a block of 10 consecutive positive integers where the one ending with 8 does contain an 8 but maybe it's not considered lucky because we need at least one digit 8; it does. So it is lucky. So indeed among positive integers, the maximum gap is 9. This is true for all integers >=0. For negative numbers, it's trickier because the condition depends on absolute value. But for numbers greater than a, as we increase from a, eventually we may hit positive numbers. So if a is negative and far enough, the next lucky number might be after crossing zero into positive numbers, and we might encounter a gap larger than 9? Let's test with a very negative number, say a = -1000000. What's the next lucky > a? We need to check numbers > -1000000. Some negative numbers might be lucky if their absolute value contains 8. For large negative numbers, the absolute values are large positive numbers, which will contain an 8 somewhere with high probability. But the distance to the next lucky might still be bounded? Let's examine negative numbers. Consider negative numbers near -1000. The absolute values are around 1000. The pattern of lucky numbers among negative numbers is the same as among positive numbers but mirrored and offset? Actually, a negative number -n (n>0) is lucky iff n contains digit 8. So the set of lucky negative numbers is { -n | n ∈ ℕ, n contains digit 8 }. So as we move from negative to positive, we encounter numbers in increasing order: ... -n-1, -n, ... . For negative numbers, the sequence of absolute values decreases as we increase? Let's list numbers around -20: -20, -19, -18, -17, ... -1, 0, 1, ... The absolute values: 20,19,18,17,...1,0,1,... So as we increase from -20 to -19, absolute value goes from 20 to 19, etc. So the condition "contains digit 8" for a negative number -n is based on n. So the distribution of lucky numbers among negative numbers is like the distribution of lucky numbers among positive numbers but reversed order. Specifically, consider positive numbers: lucky if contains 8. For negative numbers, lucky if the positive counterpart contains 8. So the set of lucky numbers in ℤ is symmetric? Not exactly: For a positive lucky number n, -n is also lucky? Since if n contains 8, then -n's absolute value also contains 8, so -n is lucky. So lucky numbers come in pairs (except 0). So the set is symmetric about 0. Therefore, the gaps between consecutive numbers (in the total order on ℤ) of lucky numbers could be analyzed by considering both sides.
Now, could there be a gap larger than 10? Consider the interval from -9 to 9. List numbers: -9,-8,...,-1,0,1,...,9. Which are lucky? Those with absolute value containing 8: -8,8. So lucky numbers are -8 and 8. So between -8 and 8, there are numbers: -7,...,7,0? Actually, -8 then next integer is -7, up to 7, then 8. That's 15 numbers? Let's count: -8 (lucky), then -7,-6,-5,-4,-3,-2,-1,0,1,2,3,4,5,6,7, then 8 (lucky). That's 16 numbers between -8 and 8 inclusive of endpoints? Actually difference: from -7 to 7 inclusive is 15 numbers, plus maybe 0 is included. But the gap between consecutive lucky numbers: from -8 to 8, the next lucky after -8 is 8? Are there any lucky numbers in between? -7? No. -6? No. ... 7? No. 0? No. So the gap is 16? Let's compute: if we take a = -8, then next lucky > -8 is 8. So the distance b would be 8 - (-8) = 16. But is 8 the next lucky > -8? Check numbers: -7, -6, ... -1, 0, 1, ..., 7. Do any contain digit 8? -7 has abs 7, no; -6 abs 6, no; ... -1 abs 1, no; 0 no; 1 no; ... 7 no. So indeed the next lucky after -8 is 8. That's a gap of 16! But wait, we must check -0? No. Also, is -8 considered lucky? Yes, because -8 contains digit 8? The example says -180 is lucky. So -8 is lucky. So if a = -8, we need b > 0 such that a+b lucky. a is lucky, but we need next lucky greater than a. So the answer for a = -8 would be b = 16? Let's test: -8+1 = -7 (not), ... -8+15 = 7 (not), -8+16 = 8 (lucky). So b = 16. That is larger than 9. The problem examples didn't include such a case, but it's possible. Let's verify: Does the tower have floor 8? Yes, since it goes up to 8,888,888,888. So that's valid. So b = 16 is valid. But is there any lucky number between -8 and 8 other than -8 and 8? What about -80? -80 is less than -8, not in that range. So indeed gap of 16. Could there be larger gaps? Consider a = -88? But -88 is lucky. The next lucky > -88 might be -80? Wait, -80 is > -88? -80 > -88 (since -80 is larger). Check if -80 is lucky: abs 80 contains 8? Yes, digit 8. So from -88, next greater is -87? -87? -87 abs 87 no 8; -86 ... up to -80? Let's see: -87, -86, -85, -84, -83, -82, -81, -80. -80 is lucky. So distance = (-80) - (-88) = 8. So b=8. That's less than 16.
What about a = -888? Then the next lucky > -888 might be -880? Actually -880 is > -888? -880 > -888. -880 is lucky? 880 contains 8, yes. So distance 8. But maybe there is a gap around 0 that yields large gap because between -8 and 8 there is no lucky numbers. That's 16 steps. Could there be a gap from -9 to 9? That's from -9 to 9, but -9 is not lucky, so the gap between lucky numbers: the previous lucky before -9? Actually the lucky numbers near zero are -8 and 8. The gap between them is 16. So if a is less than or equal to -8, the next lucky might be as far as 8 if there are no other lucky numbers in the negative range just above a. But if a is very negative, there could be many lucky numbers in the negative region (since many negative numbers have absolute value containing 8). For example, consider a = -100. The negative lucky numbers near -100: -98? 98 contains 8, so -98 is lucky. -99? 99 no. -100? 100 no. -101? 101 no. Actually -98 is > -100? -98 > -100. So if a = -100, the next lucky > -100 is -98? Check: -99? -99 not lucky. -98 lucky. So b = (-98) - (-100) = 2. So small gap.
But the worst-case gap might occur when a is just below a lucky number that is the last lucky before a long stretch without lucky numbers, and the next lucky is far ahead. For positive numbers, the max gap is 9. For negative numbers, the max gap could be larger due to the region around zero where lucky numbers are sparse because numbers with small absolute values (1-7) don't contain 8, and zero doesn't. However, there is also negative lucky numbers with small absolute values: only -8. So the gap between -8 and 8 is 16. Also what about between -88 and -80? That's only 8. Between -888 and -880 is 8. So the largest gap appears to be around zero. What about gap between -8 and 8? That's 16. Could there be an even larger gap? Consider the gap between the largest negative lucky number that is less than some negative number and the next positive lucky number maybe if there is a stretch without lucky numbers crossing zero. But are there any lucky numbers with absolute value between 9 and 17? 10-17: none contain 8 except 18? Actually 18 contains 8, so 18 is lucky. So between 8 and 18, there are numbers 9-17, none contain 8. So from 8 to 18, gap is 10? But 8 is lucky, 9-17 are not, 18 is lucky. So gap between consecutive lucky numbers in the positive side: from 8 to 18 is 10? Let's compute: the next lucky after 8 is 18? Check numbers after 8: 9,10,11,12,13,14,15,16,17,18. 9-17 are not lucky, 18 is lucky. So distance from 8 to 18 is 10. But wait earlier we argued that any block of 10 consecutive numbers must contain an 8-digit? But 9-17 are 9 numbers? Actually 9 to 17 inclusive is 9 numbers: 9,10,11,12,13,14,15,16,17 = 9 numbers. And 18 is the 10th number after 8? Starting at 8 (lucky), the next numbers: 9 (1),10(2),11(3),12(4),13(5),14(6),15(7),16(8),17(9),18(10). So the gap between lucky numbers 8 and 18 is 10. But is there any lucky number between them? 10? no. So indeed the gap is 10. However, earlier I argued that any block of 10 consecutive numbers must contain a number ending with 8. But note that 9-18 includes numbers 9,10,11,12,13,14,15,16,17,18. That's 10 numbers from 9 to 18 inclusive. Among these, 18 ends with 8, and 18 is lucky. So the block of 10 numbers contains a lucky number (18). The gap from 8 to 18 is 10, meaning that between them there is a block of 9 consecutive non-lucky numbers (9 to 17). So the maximum gap between lucky numbers in positive numbers is 9? Actually, distance between 8 and 18 is 10, but that means there are 9 numbers in between (9-17) that are non-lucky. So the number of steps from 8 to the next lucky is 10. That's b if a=8 (lucky). But note a=8 is lucky; we need b>0 such that a+b lucky. For a=8, the next lucky after 8 is 18, so b=10. That's allowed? Check: 8+10=18 lucky. So b=10. So indeed for a positive lucky number, the gap can be 10. But is there any gap larger than 10 among positive numbers? Let's consider 88 to 98? 88 lucky, 89 lucky? 89 has 8, so actually 89 is lucky. So consecutive lucky numbers can be adjacent. The largest gap among positive numbers likely occurs around powers of 10 or numbers where the digit 8 is scarce. Known: The largest gap between numbers containing digit 8 is 10? Let's test: After 79 comes 80, gap 1. After 87, 88 gap 1. After 88, 89 gap 1. After 89, 90? 90 no 8, 91,... 97? 98? 98 has 8, so from 89 to 98, 89 lucky, 90-97 non-lucky (8 numbers), 98 lucky, gap 9. So max gap maybe 10? Check after 799? 799 no 8, 800 lucky? Actually 800 contains 8? 800 has digit 8? Yes, 8 in hundreds. So 799->800 gap 1. After 889? 889 lucky, 890? 890 contains 8? 8 in hundreds? Actually 890: digits 8,9,0 -> contains 8, so lucky. So adjacent. After 899? 899 contains 8, 900 no, ... 907? 908? 908 contains 8, so from 899 to 908 gap 9? Let's compute: 899 lucky, 900 not, 901-907 not (8 numbers?), 908 lucky. So numbers between: 900-907 inclusive is 8 numbers, so gap steps = 9? Actually distance from 899 to 908 is 9. So max gap likely 10 as seen for 8 to 18. Could there be a gap of 11? Consider from 98 to 108? 98 lucky, 99 not, 100 not, 101-107 not? 108 lucky. Numbers between: 99-107 inclusive? Count: 99,100,101,102,103,104,105,106,107 = 9 numbers. So gap = 10? 98+10=108, yes. So 10. From 998 to 1008? 998 contains 8? 998 has 8? Digits: 9,9,8 -> contains 8, lucky. 999 no, 1000 no, 1001-1007 no, 1008 lucky. So between: 999-1007 inclusive = 9 numbers? Let's list: 999,1000,1001,1002,1003,1004,1005,1006,1007 = 9 numbers. So gap = 10? 998+10=1008, yes. So pattern: When the lucky number ends with 8 and all higher digits are 9? Actually after a lucky number that is the largest number with a certain prefix without an 8? For instance, 8 is lucky, and the next lucky is 18, gap 10. Similarly, 18 is lucky, next lucky is 28? Actually 18+? 19? 20? 28 is lucky. 18 to 28: numbers 19-27 are 9 numbers, gap 10. So indeed from a lucky number that is like ?8 (ends with 8) but the digits before are not containing 8? Actually 18 has digits 1,8, so it's lucky. The next lucky after 18 is 20? Wait, 19? 19 no 8; 20 no; 21-27 no; 28 lucky. So gap = 10. So many gaps of 10. Could we have gap larger than 10? Consider a lucky number that is like 88? But 88 lucky, 89 lucky, so gap 1. 89 to 98 gap 9. So maximum appears to be 10. But is there a scenario where we have a block of 10 consecutive numbers all without digit 8? That would require that none of the 10 have digit 8. But as argued, a block of 10 consecutive positive numbers must contain a number ending with 8. That number would be lucky. Therefore, any block of 10 consecutive positive integers contains at least one lucky number. Thus the maximum gap between consecutive lucky numbers in the positive integers is at most 9 numbers in between, i.e., distance of 10. So the maximum b when a is non-lucky and >0 is at most 9? Wait: if a is non-lucky, the next lucky could be as far as a+9? Actually if a is non-lucky, the block starting at a+1 could be up to 9 numbers without lucky, and the 10th must be lucky. So maximum b is 10? Let's test: a = 199 (non-lucky). The next lucky is 208? Actually 199+1=200 non, ... 207 non, 208 lucky. So b = 9? Wait, 199 to 208 is 9 steps? 199+1=200 (1), +2=201 (2), +3=202 (3), +4=203 (4), +5=204 (5), +6=205 (6), +7=206 (7), +8=207 (8), +9=208 (9). So b=9. For a=9 (non-lucky), next lucky is 18, b=9? 9+9=18? 9+1=10, +2=11, +3=12, +4=13, +5=14, +6=15, +7=16, +8=17, +9=18 => b=9. For a=0 (non-lucky), next lucky is 8? Actually 0+8=8, b=8. So for non-lucky positive a, b ≤ 9. For a lucky positive a, b could be 1 (if the next number is lucky) or up to 10? Example a=8 (lucky), b=10. a=18 (lucky), next lucky? 18 is lucky, 19? no; ... 28 lucky, distance 10. So b=10. a=28 lucky, next? 29? no; 30? ... 38 lucky, distance 10. So for lucky positive numbers that are exactly those ending with 8 and not having another 8 in higher places? Actually 88: but 88 lucky, next lucky is 89, b=1. So not all lucky numbers yield gap 10. The maximum gap for a lucky a seems to be 10 as well, because after a lucky number, the next lucky might be up to 10 steps away. But could it be more than 10? Consider a = 888... something? Let's check a = 7999? Actually 7999 is not lucky? 7999 contains 8? No, 7,9,9,9 no 8. So that's non-lucky. But for lucky a, the worst-case might be when a is the largest lucky number before a long block without 8. That block can be at most 9 numbers (since the 10th must be lucky). So the distance from a (which is lucky) to the next lucky can be at most 10 if the block of non-lucky numbers starts right after a. That is, if a is lucky, the next numbers a+1, a+2,... up to a+9 could all be non-lucky, but a+10 must be lucky. So b ≤ 10. But is it possible that a+10 is not lucky? Suppose a = 8, a+10 = 18 which is lucky. Could there be a case where a+10 is not lucky? That would require that among the 10 numbers a+1...a+10, none contain digit 8. But as argued, any set of 10 consecutive positive integers must include a number ending with 8. However, that number might be a+10? Actually the number ending with 8 could be any of them. But if a+10 is not lucky, maybe some other in the set is lucky. But we need the next lucky after a. If some earlier number (like a+3) is lucky, then b would be smaller. So the worst-case for a lucky a is when the first lucky after a is as far as possible. That would be when the block of numbers starting at a+1 of length 9 are all non-lucky, and the 10th (a+10) is lucky. That is possible if a is such that a+1 through a+9 avoid 8, and a+10 contains 8. Is that possible? Yes, for a=8, a+1..a+9 = 9..17 avoid 8, a+10=18 contains 8. So b=10. For a=18, a+1..a+9=19..27 avoid 8, a+10=28 contains 8. So b=10. For a=28, similar. So maximum b for a positive a (lucky or not) seems to be 10. But careful: For a non-lucky, the maximum b is 9? Let's check a=199 (non-lucky) gave b=9. Could there be a non-lucky a where b=10? Suppose a=9 (non-lucky) gave b=9. a=99? 99+? 100-107 non, 108 lucky => 99+9=108? Actually 99+9=108, b=9. a=999? 999+9=1008? 1008 lucky? 1008 contains 8? Yes. So b=9. To get b=10 for non-lucky, we would need a such that a+1..a+9 all non-lucky, and a+10 is lucky. That would mean the block of 10 numbers a+1..a+10 includes a lucky (a+10), but a+1..a+9 are non-lucky. That's exactly the condition for a lucky a? Actually if a is non-lucky, then the block of 10 numbers a+1..a+10 must contain a lucky, but it could be that the lucky is a+10. Then b=10. But is it possible that a is non-lucky and a+1..a+9 are all non-lucky? That would be a block of 9 numbers that are non-lucky, which is allowed. But we need to check if there exists a non-lucky a such that a+1 through a+9 avoid 8. For example, a=9: a+1=10 through a+9=18? Wait a+9=18, which is lucky, so that block includes a lucky at the end. So a+1..a+9 includes a lucky (18). So b would be less than 10 (since a+9=18 lucky, b=9). So to have b=10, we need a+10 to be the first lucky, meaning a+1..a+9 are all non-lucky. That would require that a+9 is not lucky, i.e., does not contain 8. But also a+1..a+9 must be non-lucky. So the block of 9 numbers must avoid 8. That is possible. But also a itself is non-lucky. Example: suppose a=0? a+1..a+9 = 1..9. Does 9 contain 8? No. So 1..9 all non-lucky. a+10=10? Actually a+10=10, 10 does not contain 8. So that fails because a+10 is not lucky. So need a+10 lucky. So we need a block of 9 numbers without 8, and the 10th number after a (i.e., a+10) contains 8, while a is non-lucky and a+1..a+9 are non-lucky. Let's search for such a.
We want a such that:
- a does not contain 8.
- a+1 does not contain 8.
- ...
- a+9 does not contain 8.
- a+10 contains 8.
But note: a+10 is the 10th number after a. So the set {a+1,...,a+10} is a block of 10 consecutive numbers. As argued, any block of 10 consecutive numbers must contain at least one number ending with 8. That number could be a+10 if it ends with 8. So we need a+10 to end with 8? Not necessarily; it could contain 8 in another digit, e.g., 80 contains 8 in tens. But could a block of 10 numbers have only the last number containing 8, and that 8 not being the last digit? Possibly. For example, consider numbers 79 to 88: 79,80,81,82,83,84,85,86,87,88. That's 10 numbers. Among these, 80-87 have tens digit 8? Actually 80: tens digit 8, so contains 8. So many contain 8. Not a good example. We need a block where only the last number contains 8, and that 8 might be in a higher digit. For instance, consider numbers 179 to 188: 179,180,181,...,188. Here 180 contains 8? 180 has an 8, so not only last. 179 has no 8, but 180 has 8. So again early.
Consider numbers 199 to 208: 199,200,201,202,203,204,205,206,207,208. In this block, only 208 contains 8? Check: 200-207 have no 8? 200: digits 2,0,0 no 8. 201: 2,0,1 no. ... 207: 2,0,7 no. So only 208 contains 8. And the block is 10 numbers (199 to 208 inclusive). So here a = 199? Actually a = 199 (non-lucky), a+1=200, ... a+9=208? Wait a+9 = 199+9=208, which is lucky. So b=9. So the lucky number is a+9, not a+10. That's because the block of 10 numbers from 199 to 208 includes 208 as the 10th? Let's count: 199 (a), then 200 (a+1) =1, 201=2, 202=3, 203=4, 204=5, 205=6, 206=7, 207=8, 208=9. So a+9=208. So the block of 10 numbers starting from a (inclusive) is 199..208, and the lucky is at offset 9. So a+10 would be 209, which is not lucky. So for a=199, b=9. So we need a such that a+10 is lucky, and a+1..a+9 are all non-lucky. That would require that the block of 10 numbers from a+1 to a+10 (i.e., skipping a) has its only lucky at the end. Let's check if such a exists. Take a = 198? a=198 (lucky? 198 contains 8, so a is lucky). Not non-lucky.
Take a = 189? 189 contains 8? Yes, 189 has 8. So lucky.
Take a = 188? lucky.
Take a = 187? 187 no 8. Let's test a=187. a+1=188 (lucky) -> b=1. So not.
Take a = 179? a=179 (no 8). a+1=180 (lucky) -> b=1.
Take a = 169? a=169 no 8, a+1=170 no, ... up to a+? We need 10 steps. Actually a=169, a+1=170, a+2=171, ... a+10=179. 179 no 8. So not.
We need a+10 contains 8, but a+1..a+9 no 8. Is it possible that a+10 contains 8 while a+9 does not, and also all numbers in between avoid 8? Suppose a+10 ends with 8? Then a+10 ends with 8. Then what about a+9? It ends with 7. a+9 ends with 7, which is fine, but does a+9 contain 8 elsewhere? Possibly not. So the condition reduces to: a+9 = ...7 (last digit 7), and none of digits contain 8. a+10 = ...8 (last digit 8), so a+10 is lucky. But a+9 is non-lucky. However, we also need that all numbers from a+1 to a+8 have no 8. Could we have such a run? For a positive, a block of 9 numbers where the last digits run from something to something, but we need that none of them have digit 8 anywhere. In particular, for the numbers a+1,...,a+8, they must avoid 8. Also a+9 avoids 8 and ends with 7. So essentially we need a block of 9 consecutive numbers, all without digit 8, whose last digits are from some range that doesn't include 8, and the next number (the 10th) ends with 8 and contains no other 8? But if it ends with 8, it contains 8, so it's lucky. But we also need to ensure that a+10 does not have 8 in other digits? It could, but it's fine as long as it's lucky. So the question: does there exist a positive integer N such that N, N+1, ..., N+8 are all free of digit 8, and N+9 ends with 8 (so it's lucky) and also free of other 8? But if N+9 ends with 8, it's automatically lucky; we don't care about other digits. However, could N+9 have an 8 elsewhere? Possibly, but that's okay. But we also need N+9 to be lucky, which it is. So we need a block of 9 consecutive 8-free numbers. Is that possible? Yes, as seen: 199..207 are 9 numbers? 199 to 207 inclusive is 9 numbers? 199,200,201,202,203,204,205,206,207 => that's 9 numbers, all without 8. Then the next number 208 is lucky. So that's a block of 9 non-lucky numbers followed by a lucky. In that case, if a = 199? Actually a=199 is the first of the 9 non-lucky? No, 199 is non-lucky, a+1=200,... a+8=207 are non-lucky, a+9=208 lucky. So b=9. That's a case where the lucky appears at offset 9, not 10. To have b=10, we would need a such that a+1..a+9 are all non-lucky (9 numbers) and a+10 lucky. That means the block of 9 non-lucky numbers starts at a+1, not including a. So a itself can be either lucky or non-lucky? For a non-lucky, we need a itself also non-lucky, but then the block of non-lucky numbers could be from a to a+9? That would be 10 numbers including a? But a is non-lucky, and if a+10 is lucky, then a..a+9 are all non-lucky (10 numbers) which is impossible because any 10 consecutive positive integers contain a lucky. Wait, check: If a is non-lucky, and a+1..a+9 are non-lucky, then together a..a+9 are 10 numbers? Actually a plus a+1..a+9 gives 10 numbers: a, a+1, ..., a+9. That's 10 consecutive numbers. If all of them are non-lucky, that would violate the fact that any 10 consecutive positive integers must contain at least one number ending with 8, which is lucky. So it's impossible for a and a+1..a+9 all to be non-lucky. Therefore, if a is non-lucky, it's impossible that a+1..a+9 are all non-lucky. Because then the set {a, a+1, ..., a+9} would be 10 consecutive numbers with no lucky, contradiction. Thus for non-lucky a, the maximum b is at most 9. Indeed examples: a=199 gives b=9; a=9 gives b=9; a=0 gives b=8 (but 0 is non-lucky? 0 not lucky, and 0+8=8, b=8, less than 9). So max b for non-lucky positive a is 9.
Now for lucky a, it's possible that a+1..a+9 are all non-lucky? That would give b=10. Because then the set {a+1,...,a+9} has 9 numbers, and we need also check a+10 lucky. But also need to ensure that a itself is lucky, so the set {a, a+1, ..., a+9} would be 10 numbers, but a is lucky, so it's fine that there is a lucky in that block (a itself). So the block of 10 numbers contains a lucky (a). So no contradiction. So it's possible to have a lucky a such that the next lucky is at a+10. Example a=8 gave b=10. Also a=18,28,... up to maybe a= (if a ends with 8 and the digits before are such that a+1..a+9 avoid 8). For a=8, that works. For a=18, that works. For a=28, works. For a=38? Let's test: a=38 lucky. a+1=39 no 8, a+2=40 no, ... a+9=47 no 8, a+10=48 lucky. So b=10. For a=48? a=48 lucky. a+1=49 no, a+2=50, ... a+9=57 no, a+10=58 lucky. So b=10. For a=58: a+1=59, a+2=60,... a+9=67, a+10=68 lucky. So b=10. For a=68: a+1=69, a+2=70,... a+9=77, a+10=78 lucky. b=10. For a=78: a+1=79 (no 8), a+2=80 (80 contains 8) -> b=2, not 10. So a=78 is lucky, but a+1=79 no 8, a+2=80 lucky, so b=2. So not all lucky numbers give b=10; only those that are of the form d8 where d is not 7? Actually 78 is d=7? 78: digits 7 and 8. a+2=80 contains 8, but also a+1=79 no 8. So the next lucky is at 80, which is only 2 steps. So why does the pattern break? Because after 78, we have 79 (no 8), then 80 (lucky). So the block of non-lucky numbers after 78 is only 1 number (79). So b=2. So the maximal b for lucky a is 10, but not all lucky have that.
Thus for positive a, b ≤ 10. For negative a, we need to consider both negative and positive numbers. Could b exceed 10? As we saw, for a = -8 (lucky), b = 16. That's >10. For a = -9 (non-lucky), what is b? -9+1=-8 lucky? -8 is lucky, so b=1. So -9 gives 1. For a = -10: -10+1=-9 not, +2=-8 lucky, b=2. So not large. For a = -11: b=3, ... up to a = -17: b=9? Let's compute: a=-17, a+1=-16,... a+9=-8? Actually -17+9 = -8? -17+9 = -8, yes, and -8 is lucky. So b=9. a=-18: b=10 as computed earlier? -18+10 = -8, yes b=10. a=-19: b=1 (since -19+1=-18 lucky). So maximum for negative non-lucky a around -18 gave 10. But for lucky a = -8, b=16. So that's larger. What about a = -88? That's lucky. Next lucky > -88? Might be -80? That's 8 steps. So b=8. a = -888? b=8. So the largest b seems to be for a = -8? But also maybe for a = -88? No, that's 8. What about a = -8888? Could be similar small. So the maximum b for negative a might be 16? But could there be a case where a is a lucky negative number that is the last lucky before zero, and the next lucky is the first positive lucky? That gap could be even larger. For example, a = -8 gave next lucky = 8, gap 16. What about a = -88? The next lucky after -88 is -80? Actually -80 is greater than -88 and is lucky. So gap 8. So the largest gap between a lucky negative number and the next lucky number could be from -8 to 8 (16). Are there any other lucky negative numbers with a larger gap to the next lucky? Consider a = -0? Not relevant. What about a = -7? Not lucky. So only lucky numbers that are close to zero might have large gaps. But what about a = -9? Not lucky, but if a is non-lucky, the gap could be up to maybe 10? For a = -18, we got 10. For a = -28? Let's test a = -28 (lucky). Next lucky > -28? -27? -27 abs 27 no 8; -26; ... -20? -20 abs 20 no; -19? 19 no; -18? 18 contains 8 -> -18 is lucky. So distance = (-18) - (-28) = 10. So b=10. So a = -28 gives b=10. a = -38 gives next lucky? -30? Actually -38 lucky, next greater: -37? no, -36? no,... -30? -30 abs 30 no, -29? 29 no, -28? 28 contains 8 -> -28 lucky. So distance = (-28)-(-38)=10. So b=10. a = -48 gives next lucky -40? Actually -40 abs 40 no 8; -39 no; -38? 38 contains 8 -> lucky. So distance = (-38)-(-48)=10. So b=10. a = -58 -> next -50? Actually -50 no; ... -48? -48 abs 48 contains 8 -> lucky. So distance = (-48)-(-58)=10. a = -68 -> next -60? ... -58? -58 contains 8? 58 has 8, so -58 lucky. distance = (-58)-(-68)=10. a = -78 -> next? -78 lucky, next greater: -77? no; -76? ... -70? -70 no; -69? no; -68? 68 contains 8, so -68 lucky. distance = (-68)-(-78)=10. a = -88 -> next -80 (lucky) distance 8. So largest from lucky negative numbers appears to be 16 for -8. Could there be a lucky negative number that is less than -8, but the next lucky is a positive number, giving even larger gap? For that to happen, there would have to be no lucky numbers between that negative lucky and positive numbers. For example, if a = -9 (non-lucky), the next lucky is -8, not positive. So for a lucky negative number, the next lucky could be either a larger negative lucky or a positive lucky. Is it possible that there is no negative lucky greater than a, so the next lucky is the first positive lucky? That would require that all numbers between a+1 and -1 (inclusive) are non-lucky, and also 0 is non-lucky, and then the first positive lucky is 8 (or maybe 18? Actually 8 is the smallest positive lucky? Check: 1-7 no 8, 8 lucky. So first positive lucky is 8). So if a is a negative lucky such that there is no other negative lucky greater than it, then the next lucky would be 8, and the gap would be 8 - a. The largest such gap would be when a is the largest negative lucky with no other negative lucky above it, i.e., the negative lucky closest to zero from below that has no other negative lucky between it and zero. Which negative lucky is closest to zero? The negative lucky numbers are -8, -18, -28, -38, -48, -58, -68, -78, -80? Wait -80 is lucky, but -80 < -78? Actually -80 is less than -78, but -78 is greater than -80. So the negative lucky numbers greater than -8? There is none; -8 is the greatest negative lucky? Let's list negative lucky numbers: They are -n where n contains 8. So n can be 8, 18, 28, 38, 48, 58, 68, 78, 80, 81, 82, ... Actually 80,81,82,... contain 8. But negative numbers: -8, -18, -28, -38, -48, -58, -68, -78, -80, -81, -82, ... So there are many negative lucky numbers greater than -8? Wait, "greater" meaning larger in value. On the number line, -8 is greater than -18, because -8 > -18. So among negative numbers, the ones closer to zero are larger. So -8 is the largest negative lucky number. The next negative lucky number greater than -8? Actually numbers greater than -8 are -7, -6, ... -1, 0, 1,... So the next negative lucky number would have to be a negative number greater than -8, but there is none because the next negative numbers are -7, -6,..., and none of those have absolute value containing 8 (since abs values 7,6,...,1). So indeed, -8 is the maximum negative lucky number. So for a = -8, the next lucky number greater than -8 is the smallest positive lucky number, which is 8. That yields b = 8 - (-8) = 16. So that's the maximum possible gap from a lucky negative number? What about a = -9? But -9 is not lucky. So the maximum b might be 16. However, could there be a scenario where a is non-lucky but the gap is larger than 16? For example, a = -10 gave b=2; a = -18 gave b=10; a = -100? Let's compute b for a = -1000? We can reason: For negative a, the next lucky could be either a negative lucky or a positive lucky. The worst-case b would be the maximum distance from some a to the next lucky > a. Since we can vary a, we might get larger than 16 if a is chosen appropriately between negative lucky numbers and positive lucky numbers? But from earlier, the maximum gap between consecutive lucky numbers in the integer line (ordered) could be larger than 16. Let's examine the gaps between lucky numbers across zero. The lucky numbers are symmetric: ... -18, -8, 8, 18, ... Actually we have -18, -8, then 8, 18. Wait, order: ... -18 (less), -8 (greater), then maybe -? Actually the order on number line: ... -18, -17, ..., -8, -7,..., 0,1,...,8,9,...,18,... So the consecutive lucky numbers in increasing order are: ..., -18, -8, 8, 18, 28, ... But is there a lucky number between -8 and 8? No. So the gap from -8 to 8 is 16. Is there a gap larger than that? What about from -88 to -80? That's 8. -80 to -78? Actually -80 and -78: -80 < -78, gap 2? Wait -80 to -78: There is -79 between? -79 not lucky. So consecutive lucky numbers: after -80, the next lucky might be -78? Let's list negative lucky numbers in increasing order: They are negative numbers that are increasing as they approach zero. So we need to list all negative numbers whose absolute value contains 8, sorted ascending. The smallest (most negative) are arbitrarily large in magnitude, but as we go up, we encounter many. The density among negative numbers is same as among positive numbers, but reversed. Near zero, the first few negative lucky numbers are: -8, -18, -28, -38, -48, -58, -68, -78, -80, -81, -82, -83, -84, -85, -86, -87, -88, -89, -90? Actually -90 does not contain 8? 90 has no 8, so not lucky. -98 is lucky. So there are many. But we need to find the largest gap between consecutive lucky numbers over all integers. Since among positive numbers, the max gap is 10 (e.g., between 8 and 18, 18 and 28, etc.). Among negative numbers, the ordering is ascending: e.g., ... -18, then -8? Wait, careful: On the number line, numbers increase from left to right. So negative numbers: -100 < -99 < ... < -10 < -9 < -8 < -7 < ... 0. So the lucky numbers: -1000 if contains 8, then ... Let's list from -20 upward: -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7,... . The lucky numbers in this range: -18 (contains 8), -8 (contains 8). Also -? -? -? Actually -28 is less than -20, so not in this range. So from -20 up, the lucky numbers are -18 and -8. So between -18 and -8, are there any other lucky numbers? Check -17? no; -16? no; -15? no; -14? no; -13? no; -12? no; -11? no; -10? no; -9? no. So gap from -18 to -8 is 10? Let's compute: -18 to -8: The difference is 10? -8 - (-18) = 10. So gap of 10. Then from -8 to 8: gap 16. Then from 8 to 18: gap 10. So overall, the largest gap is 16. Could there be a gap larger than 16 elsewhere? Consider between -88 and -80? That's 8. Between -80 and -78? -80 to -78 = 2. Between -78 and -68? -78 to -68 = 10? Actually -78 to -68: -68 - (-78) = 10. So many gaps of 10. Between -98 and -88? -98 to -88 = 10. So the maximum gap seems to be 16, occurring only between -8 and 8. Also maybe between -0? Not relevant.
But is there a gap from a lucky number to the next lucky number that is more than 16? For example, between -8 and 8 is 16. Could there be a gap from some lucky number to the next one that is more than 16 if there is a region where lucky numbers are sparse? Let's think about numbers around -8,888,888,888? But the tower range is limited, but a is limited to 1e9, and the tower goes up to 8.888e9, so we might not need to consider extremely large numbers. But still, in theory, the gap could be larger if we consider numbers where the digit 8 appears infrequently? But we already argued that among positive numbers, any 10 consecutive numbers contain a lucky number. So the maximum gap between consecutive positive lucky numbers is 10. For negative numbers, consider the ordering ascending: For negative numbers, the condition for being lucky is that its absolute value contains 8. So the set of lucky numbers in the negative reals is just the negative of the positive lucky numbers. So the gaps between consecutive lucky numbers in the negative region (when sorted ascending) correspond to gaps between consecutive positive lucky numbers when sorted descending? Let's examine: The negative lucky numbers are -n where n is positive and contains 8. Sorting them ascending (i.e., from more negative to less negative) gives: ... -100, -99? Actually -100 is not lucky unless 100 contains 8 (no). So we need to list all -n where n contains 8. As n increases, -n decreases. So the ascending order of negative lucky numbers corresponds to decreasing order of n. For example, n=8 gives -8 (largest negative lucky). n=18 gives -18 (next, less than -8). n=28 gives -28, etc. So the gaps between consecutive negative lucky numbers when moving upward (i.e., from more negative to less negative) are: from -28 to -18: difference 10? Actually -18 - (-28) = 10. From -18 to -8: 10. So gaps are 10. So the maximum gap in the negative region is also 10. The only place where the gap could be larger is when we transition from negative to positive, because 0 is not lucky and the smallest positive lucky is 8, while the largest negative lucky is -8. So the gap between -8 and 8 is 16. However, is there any lucky number between -8 and 8? Could -0 be considered? No. Could there be a lucky number like -? Actually -? No. So indeed the only gap >10 is that one. But what about the gap between -8 and the next lucky is 8? Actually next lucky after -8 is 8, gap 16. But if a is not -8, but maybe a is -9? Then the next lucky is -8, gap 1. So the maximum b we can get is when a is exactly -8 (lucky) or maybe when a is less than -8 but close, but the worst-case b for a non-lucky negative might be when a is just below -8? Let's compute a = -9 -> b=1. a = -10 -> b=2. a = -11 -> b=3, ... a = -17 -> b=9, a = -18 -> b=10. So the maximum for non-lucky negative is 10 (when a = -18, b=10). For lucky negative, maximum is 16 (when a = -8). Could there be a lucky negative number with b > 16? Consider a = -? If a is a lucky negative number that is not the largest, then the next lucky is another negative number, and the gap is at most 10. So only -8 yields gap 16. Also consider a = -88? That gives gap 8 to -80. So indeed 16 is the max.
But wait, what about a = 0? a = 0 (non-lucky). Next lucky is 8, b=8. That's <10. So not max.
Thus overall, b can be up to 16? But is there any other scenario where b could be larger? What if a is very negative, like a = -888888888? The next lucky could be closer. So no.
But check a = -8 is lucky, b=16. That is within input range? a can be -10^9 to 10^9, so -8 is allowed. So answer could be 16. Also a = -8 is a valid input. So we need to handle that.
But wait, we must also consider the possibility of a+b exceeding the tower's top? The problem statement says there are floors from -8,888,888,888 to 8,888,888,888. So if a is very large, say a = 10^9, we need to ensure a+b is within 8,888,888,888. But since a ≤ 10^9 and the maximum b we might need is at most maybe 16? Actually for positive a, b ≤ 10. So a+b ≤ 10^9+10 ≤ 1,000,000,010, which is far less than 8,888,888,888. So it's safe. For negative a, a can be as low as -10^9, and b might be up to 16? Actually from negative a, the next lucky could be a negative lucky that is not too far, but could it be far? If a is very negative, say a = -1,000,000,000. The next lucky > a will be some number with absolute value containing 8. Since numbers containing 8 are dense enough, the gap should be relatively small. The worst-case gap for a negative a (non-lucky) might be when a is just below a "desert" but the desert size among negative numbers is at most 10? Because among negative numbers, the set of lucky numbers are just negatives of positive lucky numbers. The gaps between consecutive lucky numbers in the negative number line (when moving upward) are at most 10 (since between -n and -m where n>m, the difference is n-m, which for positive lucky numbers m,n are consecutive in increasing order? Actually consider positive lucky numbers sorted ascending: 8,18,28,38,48,58,68,78,80,81,... Wait 80 is after 78, gap from 78 to 80 is 2. So the gaps vary. But the maximum gap between consecutive positive lucky numbers is 10, as argued. Therefore, the maximum gap between consecutive negative lucky numbers when ordered ascending (i.e., from more negative to less negative) is also 10, because the negative lucky numbers are -L where L are positive lucky numbers sorted descending? Let's be precise:
Let L_pos = sorted set of positive lucky numbers: l1=8, l2=18, l3=28, l4=38, l5=48, l6=58, l7=68, l8=78, l9=80, l10=81, l11=82, ... The gaps between consecutive l_i can be up to 10 (e.g., 8->18=10, 18->28=10, ... up to 78->80=2, 80->81=1, etc.). So max gap = 10.
Now, negative lucky numbers are -l_i for all l_i. Sorting them ascending (from most negative to least negative) yields: -l_max, ..., -l2, -l1, where l_max is the largest positive lucky number within the tower? But we consider all integers, but within our range, there are many. The difference between two consecutive negative lucky numbers in ascending order is: suppose we have two positive lucky numbers p < q. Then -q and -p are negative lucky numbers, with -q < -p. In ascending order, after -q comes -p? Actually ascending: -q, then ... maybe other numbers? Are there any negative lucky numbers between -q and -p? Since negative lucky numbers are exactly -r for all r lucky. If p and q are consecutive positive lucky numbers (i.e., no other lucky number between them), then there is no r such that p<r<q and r lucky. Then between -q and -p, the negative lucky numbers are exactly -q and -p, with no other. So the gap (difference) between -q and -p is q - p. So the gap between consecutive negative lucky numbers (in ascending order) equals the gap between corresponding consecutive positive lucky numbers (in ascending order). Therefore, max gap = 10. So for any negative a that is not lucky, the distance to the next lucky (which could be a negative lucky if one exists greater than a) is at most 10? Not exactly: If a lies between two negative lucky numbers, the distance to the next lucky (the greater one) is at most the gap between those two negative lucky numbers. Since max gap is 10, the maximum b for a non-lucky a in the negative region (when the next lucky is still negative) is at most 10. However, if a is less than the smallest negative lucky? But there is no smallest negative lucky; there are negative lucky numbers arbitrarily negative (since for any large n containing 8, -n is lucky). So for any negative a, there exists a negative lucky number greater than a? Possibly the next lucky could be negative if a is not too close to zero. But if a is between -8 and 0, the next lucky is 8 (positive), because there is no negative lucky greater than -8 (since -8 is the maximum negative lucky). For a in (-8, 0), the next lucky is 8, with distance less than 16? For a = -7, b =? -7+? next lucky? -6? no; ... -1? no; 0? no; 1? no; ... 8? so from -7 to 8 = 15 steps? Actually -7+15=8? -7+7=0, +8=1? Let's compute: -7 to 8: difference = 8 - (-7) = 15. So b=15. For a = -6, b=14? -6 to 8 = 14. For a = -5 ->13, -4->12, -3->11, -2->10, -1->9, 0->8. So for a in (-8,0) (non-lucky), b ranges from 9 to 15. That's larger than 10. So we need to consider that. For a = -7, b=15. That's even larger than 16? 15 < 16. For a = -8 (lucky) b=16. So the maximum b might be 16. But what about a = -9? b=1. So the region just below -8 gives small b. So the maximum b overall appears to be 16, occurring at a = -8. Also a = -8 is lucky, so b=16. Could there be a = -? Let's test a = -80? Wait, if a = -80 (lucky), next lucky is -78? Actually -80 is lucky, but is there a negative lucky greater than -80? Yes, -78 is lucky (since 78 contains 8). So b = (-78) - (-80) = 2. So small. So indeed the largest gap is from -8 to 8 = 16. So answer b can be as high as 16.
But we must also consider a could be large positive and lucky, leading to b=10. So overall, b is at most 16.
Given that a is up to 1e9, we could simply simulate b from 1 to maybe 20 and check each a+b for luckiness. Since the answer is bounded by a small constant (16) for all a? But is it always ≤ 16? Let's verify for a extremely negative, say a = -10^9. What is the next lucky? Could it be that the distance is more than 16? Let's test the logic: The maximum gap between consecutive lucky numbers (in the integer ordering) is 16, as argued? Wait, we argued that among positive lucky numbers, max gap is 10; among negative lucky numbers (sorted ascending), max gap is 10; the only gap crossing zero is between -8 and 8, which is 16. However, is it possible that there exists a gap between two lucky numbers that is larger than 16, not crossing zero? For example, consider two positive lucky numbers that are far apart. But we argued that any 10 consecutive positive numbers contain a lucky, so the maximum gap between consecutive positive lucky numbers is at most 10. That seems solid. Because if there were a gap of 11 or more, then there would be at least 10 consecutive numbers without a lucky, but any block of 10 consecutive numbers has a number ending with 8, which is lucky. So max gap is 10. So among positive lucky numbers, max gap is 10. Similarly, among negative lucky numbers, the condition for a negative number to be lucky is that its absolute value contains 8. For a block of 10 consecutive negative numbers, do we necessarily have one whose absolute value ends with 8? Not necessarily, because negative numbers are not symmetric in digit distribution? Let's examine a block of 10 consecutive negative numbers, say from -20 to -11 inclusive. Their absolute values are 20 down to 11. Among these absolute values, do we have one ending with 8? 20 ends with 0, 19 ends with 9, 18 ends with 8? 18 is in that range? Actually absolute values: 20,19,18,17,16,15,14,13,12,11. Yes, 18 ends with 8. So that block contains a lucky? -18 is lucky. So any block of 10 consecutive negative numbers that are all greater than -∞? In general, consider any 10 consecutive integers. If they are all negative, say from -n-9 to -n (inclusive) where n is positive. Their absolute values are n+9, n+8, ..., n. Among these 10 consecutive positive integers, there is always one ending with 8? Actually among any 10 consecutive positive integers, there is always one ending with 8. Because residues mod 10 cycle. So yes, among the absolute values, there will be a number ending with 8. Therefore, among any 10 consecutive negative integers, there is at least one whose absolute value ends with 8, making it lucky. So the same argument holds: any block of 10 consecutive negative integers contains a lucky number. Therefore, the maximum gap between consecutive lucky numbers in the negative region (when considering the order on the number line) is also at most 10. Because if there were a gap of 11 between two negative lucky numbers, then there would be a block of at least 10 consecutive negative numbers without a lucky, contradiction. However, careful: The gap is measured as the difference in the numbers, not the count of integers between them. If two negative lucky numbers L1 < L2 (both negative) have no other lucky numbers between them, then the integers between them are all non-lucky. If L2 - L1 > 10, then the set of integers from L1+1 to L2-1 contains at least 10 numbers? Actually if L2 - L1 = d, then there are d-1 integers between them. If d > 10, then there are at least 10 integers between them. Those integers are consecutive (since L1+1, L1+2, ..., L2-1) and they are all negative (assuming L1 and L2 are negative and L2 > L1, so the interval lies in negative numbers). So we would have a block of at least 10 consecutive negative numbers with no lucky number, contradiction. Therefore, d ≤ 10. So indeed, among negative lucky numbers, the gap is ≤ 10. The only place where this argument fails is when the block spans across zero, because the numbers are not all of the same sign. A block of 10 consecutive integers that cross zero could have all non-lucky? Let's check: from -4 to 5: numbers -4,-3,-2,-1,0,1,2,3,4,5. Do any contain 8? No. So that's a block of 10 consecutive integers with no lucky number! Indeed, -4 to 5 inclusive is 10 numbers, none contain 8. That contradicts the earlier claim that any 10 consecutive integers must contain a lucky. Because we previously argued for positive numbers, but crossing zero can break it because the numbers include negative ones and zero. But our earlier argument for positive numbers relied on the fact that among any 10 consecutive positive integers, one ends with 8. For negative numbers, we used the absolute value argument, but that works if the numbers are all negative. For a mixed block that includes both negative and positive numbers, the absolute values are not a set of 10 consecutive positive integers; they are something like: for numbers from -4 to 5, absolute values: 4,3,2,1,0,1,2,3,4,5. That set is not 10 consecutive positive integers; it's symmetric and includes duplicates. So there can be a block of 10 consecutive integers with no 8. Indeed, -4 to 5 has no 8. So the maximum gap between lucky numbers can be larger than 10 because of the region around zero where both negative and positive numbers lack digit 8. Let's examine the block from -8 to 8 we already saw is 17 numbers? Actually -8 to 8 inclusive includes 17 numbers: -8,-7,...,0,...,7,8. Among these, the lucky are -8 and 8. So the block of non-lucky numbers between them is 15 numbers: -7 to 7. That's a block of 15 consecutive non-lucky numbers. So the gap (difference between lucky numbers) is 16, and there are 15 non-lucky between. Could there be a block of even more consecutive non-lucky numbers? Let's search. We want to find the longest sequence of consecutive integers that all avoid digit 8. The maximum length of such a sequence may be determined by the distance between the largest negative lucky number (-8) and the smallest positive lucky number (8). That's 16 steps from -8 to 8, but the block of consecutive non-lucky numbers is from -7 to 7, which is length 15. Could there be a longer block elsewhere? Consider around -88 and 88? But there are negative lucky numbers like -80, -78, etc., so the gap between -88 and -80 is 8, not larger. Between -80 and -78 is 2. So no. The only region where lucky numbers are sparse is near zero because the smallest positive lucky is 8, and the largest negative lucky is -8. So that yields the largest gap. However, what about between -8 and 8 we already have. But could there be a gap between a negative lucky number and a positive lucky number that is larger than 16 if the smallest positive lucky is 8, but maybe there is no positive lucky less than 8? The positive lucky numbers start at 8. So that's fixed. The largest negative lucky is -8. So the gap is 16. So that's the maximum possible gap between any two lucky numbers in the entire integer line. However, is it possible that the next lucky after a non-lucky number is even farther than 16? For a non-lucky a that lies within the big gap, the distance to the next lucky could be up to the distance from a to the nearest lucky to the right. For a in the middle of the gap, say a = 0, distance to next lucky (8) is 8. For a = -7, distance to 8 is 15. For a = -8 (lucky), distance to next lucky (8) is 16. For a = -9, next lucky is -8, distance 1. So the maximum b is indeed 16, achieved at a = -8.
But wait, consider a = -80? That's lucky, but next lucky is -78, distance 2. So no.
Thus the answer b is at most 16 for all a in the range? But is it possible that a is very negative such that the next lucky is a positive number and the distance is larger than 16? For that to happen, there would need to be no negative lucky numbers greater than a, meaning a is greater than the maximum negative lucky? But the maximum negative lucky is -8. So if a > -8, then it's in the range (-8, ∞). For a > -8, the next lucky could be 8 if a < 8 and there is no other lucky between. So for a in (-8,8), the distance to 8 is at most 8 - a. The maximum of 8 - a for a in (-8,8) occurs at the smallest a, i.e., a just greater than -8? Actually a must be > -8. The supremum of a approaching -8 from above gives distance approaching 16. But a cannot be -8 exactly if we want distance 16? Actually at a = -8, distance to 8 is 16. At a = -7.999? Not integer. So the maximum for integer a in (-8,8) is a = -7 gives distance 15. So less than 16. So the maximum integer a that gives b=16 is exactly a = -8. So b=16 is maximum.
But wait, could there be a scenario where the smallest positive lucky is not 8? Because 0 is not lucky, 1-7 not lucky, 8 is lucky. So yes. So b max = 16.
But what about the negative side? Could there be a negative lucky number less than -8 for which the next lucky is also negative, but gap larger than 10? We argued no, because among negative numbers, any 10 consecutive negatives contain a lucky. However, consider the block from -9 to 0? That's mixed. But if both L1 and L2 are negative, the interval between them is entirely negative. So by the argument, gap ≤ 10. So safe.
Thus overall, the answer b is bounded by a small constant (≤ 16). This is crucial: we can simply iterate b from 1 upward until we find a lucky number, and we will need at most 16 iterations. Because the next lucky is at most 16 steps away.
But is that always true? Let's double-check with a = 10^9, which is 1000000000. Does it contain 8? No. The next lucky? Could it be more than 16 away? According to the claim, any integer a, the next lucky is at most 16 steps away. Let's test a = 10^9 = 1,000,000,000. Check numbers: 1,000,000,001? contains 8? no. ... up to 1,000,000,008? That ends with 8, so it's lucky. So b = 8. So fine. a = 1,000,000,008 is lucky? Actually 1,000,000,008 contains 8? Yes, because last digit 8. So a = 1,000,000,008 is lucky, next lucky? Could be 1,000,000,018? That's 10 steps? Let's see: after 1,000,000,008, we have 1,000,000,009 (no), 1,000,000,010 (no), ... up to 1,000,000,017? Actually we need to see the next number ending with 8 is 1,000,000,018, which is 10 steps away. But is there any lucky number earlier, maybe with 8 in a higher digit? For numbers around 1,000,000,008, the next numbers: 1,000,000,009, 1,000,000,010, ... 1,000,000,017 none have 8? 1,000,000,010 has digits: 1,0,0,0,0,0,0,1,0 - no 8. So indeed the next lucky is 1,000,000,018 (ends with 8) which is 10 steps away. So b=10. That's ≤16. So okay.
But is there any a for which the next lucky is more than 16 away? Consider a = -8, we got 16. Consider a = -9, we got 1. So max is 16. Let's try a = -888888888? That's a lucky? It contains many 8's. The next lucky after that? Probably close. For instance, -888888888 + 1 = -888888887, does that contain 8? Yes, many 8's except last digit 7, but still contains 8. Actually -888888887 contains eight 8's? It has digits: 8,8,8,8,8,8,8,8,7? So contains 8, so it's lucky. So b=1. So indeed the maximum gap is small.
Therefore, we can solve by simply checking a+1, a+2, ... up to a+16, and returning the first b where a+b is lucky. Since 16 is small, it's efficient.
But wait, need to be absolutely sure that the next lucky is always within 16 steps. Let's prove: For any integer a, consider the next integer > a that contains digit 8. We need to show that the distance is at most 16. Approach: If a ≥ 0, then among the numbers a+1 to a+10, there is a number ending with 8 (since residues mod 10 cycle). That number contains digit 8, so the next lucky is at most 10 steps away. Actually careful: if a >=0, then the set {a+1, a+2, ..., a+10} are 10 consecutive positive integers. Among them, there is at least one number whose last digit is 8. So that number is lucky. So b ≤ 10. So for a >= 0, b ≤ 10. For a < 0, we have two cases:
- If a ≤ -9, then a+1 ≤ -8. The interval from a+1 to a+10 includes at most negative numbers? Actually if a ≤ -9, then a+1 ≤ -8, and a+10 ≤ 1? For a = -9, a+10 = 1. So the interval may cross zero. But we can find an upper bound. However, we can more simply: For a ≤ -9, consider the number a+9? Let's think: The worst-case is when a is just below a negative lucky such that the next lucky is far. But as argued, among negative numbers, any 10 consecutive negative numbers contain a lucky. So if a+1 to a+10 are all negative, then there is a lucky among them, so b ≤ 10. The only way that a+1 to a+10 are not all negative is when a >= -9? Actually if a = -9, a+1 = -8 (negative), a+2 = -7, ... a+9 = 0, a+10 = 1. So the set includes non-negatives. In that case, we cannot guarantee a lucky among them because the block crosses zero. For a = -9, we found the next lucky is -8 (at b=1). So still small. For a = -10, a+1 = -9, a+2=-8 lucky at b=2, so okay. For a = -11, etc., up to a = -18, b=10. For a = -18, a+1=-17,..., a+9=-9, a+10=-8? Actually -18+10 = -8, which is lucky. So b=10. For a = -19, a+1=-18 lucky, b=1. So for a ≤ -9, the maximum b is 10? Actually a = -18 gave b=10. Could a = -? Let's test a = -28: a+1=-27,... a+9=-19, a+10=-18 lucky => b=10. So yes, for a ≤ -9, the next lucky is at most 10 away? Wait, consider a = -8? That's not ≤ -9, it's > -9. So for a < -8? Actually a = -8 is > -9. So the problematic region is a ∈ [-8, 7] maybe? Because for a between -8 and 7 (inclusive), the next lucky is 8, so b = 8 - a. That could be up to 16 when a = -8? Actually for a = -8, b = 16. For a = -7, b=15, up to a=7, b=1? Actually 7 -> b=1? 7+1=8, yes. So for a in [-8,7], b ranges from 1 to 16. But note that a = -8 is lucky itself; for a = -8, b=16. For a = -7 (non-lucky), b=15. So for a in [-8,7], b can be as high as 16. For a < -8, b ≤ 10. For a ≥ 8, b ≤ 10.
Thus overall, b is at most 16.
But wait, is there a case where a is in [-8,7] but the next lucky might be a negative lucky rather than 8? For a in (-8,0), the next lucky could be -8 if a < -8? No, because if a > -8, then -8 is less than a, so not greater. So the next lucky must be ≥ a+1. The only lucky numbers greater than a in that range are? For a between -8 and -1, the lucky numbers greater than a could be? Negative lucky numbers: the only negative lucky numbers greater than -8 are none, because -8 is the maximum. So the next lucky is 8. For a between 0 and 7, the next lucky is 8. So indeed, for a ∈ (-8,7], the next lucky is 8, giving b = 8 - a. That's as computed.
Therefore, the maximum b is 16 for a = -8.
But hold on: a can be as low as -10^9, but we argued for a ≤ -9, b ≤ 10. However, is it possible that for some a ≤ -9, the next lucky is not within 10? Let's test a = -1000000. According to the argument, among the next 10 numbers, maybe some are negative, some positive? Actually if a is very negative, say a = -1000000, then a+1 = -999999, ..., a+10 = -999990. All these are negative. By the property: any 10 consecutive negative numbers contain a lucky. So there must be a lucky among them. So b ≤ 10. Good.
But we need to verify that the property holds for any 10 consecutive negative numbers. As argued, for a block of 10 consecutive negative numbers, their absolute values form a set of 10 consecutive positive integers (since if the numbers are -x-9, -x-8, ..., -x, where x is some integer, the absolute values are x+9, x+8, ..., x). That's a set of 10 consecutive positive integers. Among any 10 consecutive positive integers, there is one ending with 8. That number's negative counterpart will be in the original block and will be lucky (since its absolute value ends with 8). So indeed, any 10 consecutive negative numbers contain a lucky. So b ≤ 10 for a ≤ -9? Actually for a ≤ -9, a+1 might be negative, but we need the block of 10 numbers starting from a+1 to be all negative? For a ≤ -10, a+10 ≤ 0? For a = -10, a+10 = 0, which is not negative. So the block a+1 to a+10 for a = -10 includes 0, which is not negative, and the absolute value set is not 10 consecutive positive integers. So the property of 10 consecutive negative numbers only holds if a+10 is still negative, i.e., a ≤ -11? Let's check: For a ≤ -11, a+10 ≤ -1, so all numbers from a+1 to a+10 are negative. So for a ≤ -11, we have a block of 10 consecutive negative numbers, thus contains a lucky, so b ≤ 10. For a = -10, a+1 = -9, a+2 = -8 (lucky), so b ≤ 2. For a = -9, b=1. So indeed for a ≤ -11, b ≤ 10. For a = -10 and -9, b small. So the only region where b could be >10 is when a is between -8 and 7 inclusive. In that region, b = 8 - a if a < 8, but need to check if there is any other lucky between a and 8? For a between -8 and -1, the numbers between a and 8 include negative numbers up to -1, 0, and positive up to 7. Are any of those lucky? Negative lucky numbers greater than a? The only negative lucky is -8, but if a > -8, -8 is less than a, so not in the interval. So none. Positive lucky numbers less than 8? The smallest positive lucky is 8. So indeed none. So b = 8 - a. For a from -8 to 7, that yields b from 16 down to 1. So maximum 16 at a = -8. For a = 7, b=1; for a=0, b=8; etc.
Thus, the answer b is at most 16, and we can compute by simply checking b from 1 to 16, or we can derive a formula. But given the problem constraints, it's easy to loop.
However, we must be careful about negative numbers: when checking if a+b is lucky, we need to check the absolute value's decimal representation contains '8'. So we can define a function isLucky(n): return '8' in str(abs(n)). That's straightforward.
But also note the input range: a can be -10^9 to 10^9, a+b could be up to 10^9+16, which is safe for integer operations and string conversion.
So algorithm: read integer a. For b from 1 to infinity (but we know within 16), compute x = a + b. If '8' in str(abs(x)), output b and break.
But is it guaranteed that we will find within 16? Let's test some edge: a = 8,888,888,887? That's close to upper bound of tower? But a is ≤ 10^9, and the tower's top is 8,888,888,888, so still within. But if a is 8,888,888,888? Actually a ≤ 10^9, so it's less than that. But even if a were 8,888,888,887, it's not lucky? It contains many 8's, actually it contains 8's: digits: 8,8,8,8,8,8,8,8,8,7? That's nine 8's? Actually 8,888,888,887 has digit 8 in many places, so it's lucky. So next lucky? If a is that, then a+1 = 8,888,888,888 which contains 8, so b=1. So fine.
What about a = 8,888,888,888? Not in input range because a ≤ 10^9, and 8,888,888,888 > 10^9? Actually 8.888e9 > 1e9, so not allowed. So all a are within 1e9, which is much smaller than 8.888e9.
Thus simple loop works.
But wait, we should also consider the possibility that a+b might exceed the tower's maximum? The problem statement says the tower has floors from -8,888,888,888 to 8,888,888,888. Since a is at most 10^9 and we add at most 16, a+b ≤ 1,000,000,016 which is less than 8,888,888,888. For negative a, a can be as low as -10^9, a+b could be as high as -10^9+16, still above -8.888e9? -10^9 > -8.888e9 (since -1e9 > -8.888e9). So still within tower. So safe.
Thus solution: iterate b from 1 to maybe 20 (to be safe) and check.
But we can also derive a direct formula. However, given the simplicity, loop is fine.
But I want to be absolutely certain that the maximum b is indeed 16. Could there be a scenario where a is, say, -88? No. But what about a = -88? That is less than -8, so in the region a ≤ -11? Actually -88 ≤ -11, so b ≤ 10. Indeed we saw b=2? Actually a = -88 (lucky), next lucky is -80? Wait -88 + ? = -80? That's 8 steps? Actually -88 to -80 is 8, but check if there is any lucky between -88 and -80? -87? no; -86? no; -85? no; -84? no; -83? no; -82? no; -81? no; -80 lucky. So b=8. So ≤10.
Thus, the simple loop works.
But wait, there is a nuance: The definition of lucky number includes numbers like -0? Not relevant. And also note that 0 does not contain 8. So correct.
Thus algorithm is straightforward.
However, I recall that in some Codeforces problem (this is likely Codeforces Round #278, problem A? Actually the description matches CF 488A - Giga Tower). That problem indeed asks for minimal positive b such that a+b is lucky. The typical solution is to iterate b from 1 to something like 10 or 20, because the answer is small. I've seen it before. So it's fine.
Thus we can implement: read a; for b in 1..20: if '8' in str(abs(a+b)): print(b); break.
But ensure that we handle negative numbers correctly. For example, a = -1, a+b = 8 for b=9, abs(8)=8 contains 8, so prints 9.
Thus solution is simple.
But we might also need to consider if a+b could be negative and its string representation of abs is used, fine.
Complexity: O(1).
Edge Cases: a = -8 -> output 16? Let's test: a=-8, b=1 => -7 abs 7 no; b=2 => -6 abs 6 no; ... b=15 => 7 abs 7 no; b=16 => 8 abs 8 contains 8 -> output 16. So correct.
Check a = 8: b=1 => 9 no; ... b=9 => 17 no; b=10 => 18 contains 8 -> output 10. matches pattern.
Check a = 18: b=1=>19 no; ... b=9=>27 no; b=10=>28 contains 8 -> output 10. matches sample? Sample says for input 18, output 10. Yes.
Check a = 179: b=1 =>180 contains 8 -> output 1. matches sample.
Check a = -1: b=1=>0 no; ... b=8=>7 no; b=9=>8 contains 8 -> output 9. matches sample.
So all good.
Thus final answer: loop b=1 to 20.
But we should be cautious: In the sample input 18, output is 10, which is 10. So loop up to at least 10. So 20 is safe.
Now, confirm the maximum b found in tests: for a = -8, b=16. So loop must go at least to 16. 20 is fine.
Thus implement.
Now, I'll produce the final solution in Python (or any language). The problem expects a single integer output.
We'll implement:
def is_lucky(n):
return '8' in str(abs(n))
def solve():
import sys
a = int(sys.stdin.readline().strip())
b = 1
while True:
if '8' in str(abs(a + b)):
print(b)
break
b += 1
But we can also do for b in range(1, 20): if... print(b); break.
Because we know it will always find.
That's it.
Now, we should consider if a is extremely large, abs(a+b) conversion to string might be heavy? No, it's fine.
Now, there might be a nuance: The tower floors go from -8,888,888,888 to 8,888,888,888. So if a is near the top, we must ensure that a+b does not exceed 8,888,888,888. But a is at most 1e9, and b is at most 16, so a+b <= 1,000,000,016, which is far less than 8,888,888,888. So fine.
Thus solution is correct.
But wait, what if a is -10^9? Then a+b might be negative but still within lower bound? -1e9+16 = -999,999,984, which is greater than -8,888,888,888, so within.
Thus no issue.
Therefore, answer: loop.
Now, I'll produce final answer.
But the problem might be from Codeforces where Python is allowed, so fine.
Now, I'll provide the final solution.
But the instruction says: "You are an AI. Provide the final answer." So I need to output the code? Actually typical format: Provide solution in Python or any language. I'll write the solution in Python.
Thus final answer. ```python
import sys
def is_lucky(x: int) -> bool:
"""Return True if decimal representation of abs(x) contains digit '8'."""
return '8' in str(abs(x))
def solve() -> None:
a = int(sys.stdin.readline().strip())
b = 1
while True:
if is_lucky(a + b):
print(b)
break
b += 1
if name == "main":
solve()