We need to output the minimum lucky number (digits only 4 and 7) whose sum of digits equals n. The lucky number must be positive integer, decimal representation only 4 and 7. We need the minimum such number. Since we care about numeric value, the minimum number with given digit sum and digits only 4 and 7 will be the one with the smallest length? Actually among numbers with the same digit sum, the one with fewer digits tends to be smaller? Not necessarily, because a 3-digit number like 447 is greater than a 2-digit number 77? But 447 sum = 4+4+7=15, 77 sum=14, so not same sum. For a given sum, we can have different combinations of 4s and 7s. To minimize the numeric value, we want the number to have as few digits as possible because fewer digits generally means smaller number (since leading digit is not zero). However, among numbers with same length, we want the smallest lexicographically when digits are considered with most significant digit first. Since digits are only 4 and 7, and 4 < 7, we prefer 4s at the leftmost positions and 7s at the rightmost positions. So the minimal number for given length L and sum S (S = 4a + 7b, where a+b = L, a number of 4s, b number of 7s) is to have as many 4s as possible at the left, then 7s. But we can also vary L.
Given n up to 1e6, we need to find nonnegative integers a (count of 4s) and b (count of 7s) such that 4a + 7b = n, and we want to minimize the numeric value. The number is formed by a 4s and b 7s arranged with all 4s first then 7s to be minimal (since 4 < 7). So the number will be: string of a 4s followed by b 7s. But is that always minimal? For a fixed a and b, yes, because any permutation that has a 7 before a 4 would be larger (since the first differing digit would be 7 vs 4). So the minimal arrangement is all 4s then all 7s.
Now we need to choose a and b (nonnegative integers) satisfying 4a+7b = n, and among all such, the numeric value of (a times '4', b times '7') is minimized. However, note that the length L = a+b. Since we want the smallest number, we should prefer smaller length? Let's compare two solutions: (a1,b1) and (a2,b2). Their corresponding numbers N1 = "4"*a1 + "7"*b1, N2 = "4"*a2 + "7"*b2. Which is smaller? It depends on length. If L1 < L2, then N1 has fewer digits, so it is definitely smaller because any L1-digit number is less than any L2-digit number (since no leading zeros). So the primary objective is to minimize total digits L = a+b. Among those with minimal L, we then want the lexicographically smallest string, which is to maximize a? Actually if we fix L, the sum constraint is 4a+7(L-a) = 7L - 3a = n => a = (7L - n)/3. But we need a integer between 0 and L. Since we want the number to be minimal for that L, we want as many leading 4s as possible (since 4<7) i.e., maximize a. However, we also need to satisfy the sum. For a given L, the possible a is determined by the equation: 4a + 7(L-a) = n => 4a + 7L -7a = 7L -3a = n => 3a = 7L - n => a = (7L - n)/3. For a to be integer, 7L - n must be divisible by 3, and 0 <= a <= L. Since we want minimal number, we want the smallest L such that there exists a valid a. Because if L1 < L2 and both have solutions, then the number with L1 digits is smaller than any with L2 digits. So we can just find the minimal possible total number of digits L = a+b. But careful: There might be multiple L with solutions; the smallest L yields the smallest number. However, could there be a case where the smallest L yields a number that is lexicographically larger than some number with larger L? No, because the number of digits is the primary determinant of magnitude: any number with d digits is between 10^{d-1} and 10^{d} -1, and any number with d-1 digits is at most 10^{d-1} -1, which is less than the minimum d-digit number (10^{d-1}). So indeed, smaller digit length always yields a smaller numeric value. So we should find the minimal total number of digits L such that n can be expressed as 4a+7b with a+b = L. Since a+b = L, b = L-a, so 4a+7(L-a) = n => 7L - 3a = n => 3a = 7L - n => a = (7L - n)/3. For a to be nonnegative integer <= L, we need 7L >= n and (7L - n) % 3 == 0, and a <= L, but 7L - n = 3a, and a <= L => 7L - n <= 3L => 4L <= n => L <= n/4? Actually, check: a <= L => (7L - n)/3 <= L => 7L - n <= 3L => 4L <= n => L <= n/4? That would be L <= n/4? But note n is the sum, and digits are at least 4 each if all digits are 4? Actually if all digits are 4, then L = n/4 (if divisible). So the maximum L such that we can have all 4s is n/4. But if we allow 7s, the sum per digit is higher, so for given n, L can be smaller than n/4? Wait, consider L being number of digits. The minimum possible sum for a given L is 4L (if all 4s). The maximum possible sum is 7L (if all 7s). So to represent n with L digits, we need 4L <= n <= 7L. So L must satisfy ceil(n/7) <= L <= floor(n/4). That's the range. So to find minimal L, we need the smallest L such that there exists a solution. That would be the smallest L satisfying 4L <= n <= 7L and also there exists a integer a between 0 and L with 4a+7(L-a)=n, which is equivalent to 7L - n being divisible by 3. Because given L, if 4L <= n <= 7L, then a = (7L - n)/3 is automatically between 0 and L? Let's verify: a = (7L - n)/3. Since n >= 4L, 7L - n <= 3L, so a <= L. Also since n <= 7L, 7L - n >= 0, so a >= 0. So as long as 4L <= n <= 7L and (7L - n) mod 3 == 0, we have a valid solution. So the problem reduces to: find minimal L (positive integer) such that 4L <= n <= 7L and (7L - n) % 3 == 0. Then a = (7L - n)/3, b = L - a = (n - 4L)/3? Let's derive: b = L - a = L - (7L - n)/3 = (3L - 7L + n)/3 = (n - 4L)/3. So that works too.
Thus we can iterate L from ceil(n/7) up to floor(n/4) in increasing order, and find first L such that (7L - n) % 3 == 0. But n up to 1e6, L range could be up to n/4 = 250k, that's fine to iterate linearly. Or we can compute directly by analyzing modulo constraints.
Alternatively, we can find a and b using number theory: we need 4a + 7b = n. Since 4 and 7 are small, we can try b from 0 to something, compute a = (n - 7b)/4 when divisible, and pick minimal digits length. Since we want minimal length L = a+b, we want to maximize the use of 7s because they give more sum per digit, leading to fewer digits. So we want as many 7s as possible to minimize total digits. However, we also need a integer and nonnegative. So we can try to maximize b such that n - 7b >= 0 and (n - 7b) divisible by 4, because then a = (n - 7b)/4, length = a+b. But is the maximal b solution always yields minimal digits? Since each 7 contributes 7 to sum, if we increase b by 1 and decrease a accordingly, the sum changes: 4(a-? Actually if we replace a 4 by a 7, the sum increases by 3 and digits count same. To reduce digit count, we want as many 7s as possible because then for a given n, more sum per digit => fewer digits. Indeed, the minimal digit length L is ceil(n/7) because if we could use only 7s, we would need ceil(n/7) digits. But we may not be able to achieve exactly n with only 7s, we need to adjust by using 4s. So the minimal L is the smallest L such that we can make sum n with L digits using 4s and 7s. That L is at least ceil(n/7). And as we increase L, we have more flexibility. So the minimal L is not necessarily achieved by maximum b? Actually maximum b for a given L? But we are free to choose L. To minimize L, we want to use as many 7s as possible because each 7 gives more sum. So we can try L = ceil(n/7) and check if there exists a solution with that L. If not, increment L until we find one. That's exactly what we described. And when we find the minimal L, we want to produce the number with a leading 4s then 7s, which corresponds to maximum a (since we want as many 4s as possible? Wait: At the minimal L, we want the smallest numeric value. For a given L and sum n, we need to assign digits. The smallest number (lexicographically) is with the smallest digits at the most significant positions. Since 4 < 7, we want as many 4s as possible at the left. That corresponds to maximizing a, the count of 4s. But note that a is determined uniquely by L from the equation a = (7L - n)/3. Since L is fixed, a is forced. So there's only one solution for a given L? Actually, given L, the sum 4a+7b = n with a+b = L yields a unique a and b, because it's two equations. So for a specific L, the counts are fixed. Thus the number is uniquely determined: a 4s, b 7s. And the minimal arrangement is all 4s first, then 7s. So there's no further choice.
Thus we can just find L minimal. So algorithm: For L from ceil(n/7) to floor(n/4) (or just start from ceil(n/7) and increment until condition satisfied), check if (7L - n) % 3 == 0. Since n up to 1e6, worst-case L could be n/4 = 250,000, which is fine.
Alternatively, we could solve using b: we want minimal total digits = a+b. Since b is number of 7s, we can try b from floor(n/7) down to 0, compute remaining = n - 7b, check if remaining divisible by 4, then total digits = b + remaining/4. Since we want minimal digits, we want as many 7s as possible, so starting from the largest b (which yields the smallest L) and going downwards until we find a valid solution? Actually larger b gives fewer digits if it works because total digits = b + a, with a = remaining/4. If remaining is divisible by 4, then total digits = b + (n-7b)/4 = (4b + n - 7b)/4? Wait compute: total digits = b + (n-7b)/4 = (4b + n - 7b)/4 = (n - 3b)/4. As b increases, n - 3b decreases, so total digits decreases. So the maximum possible b yields the smallest total digits. So we can iterate b from floor(n/7) down to 0, check if (n - 7b) >= 0 and divisible by 4. The first such b (largest) yields minimal total digits. That also works. Then we construct a = (n - 7b)/4, and output a 4s then b 7s. That might be simpler.
Check: For n=11, floor(11/7)=1, b=1: remaining=4, divisible by 4 => a=1, total digits=2. So output "4"*1 + "7"*1 = "47". That's correct.
For n=10, floor(10/7)=1, b=1: remaining=3, not divisible by 4. b=0: remaining=10, 10%4 !=0. So no solution -> -1.
For n=12, floor(12/7)=1, b=1: remaining=5, no. b=0: 12%4==0 => a=3 => "444". But is that minimal? L=3 digits. Could there be L=2? For L=2, possible sums: 8,11,14? Actually 4+4=8, 4+7=11, 7+7=14. 12 not possible. So L=3 is minimal, and "444" is the number with three digits? But maybe "447"? That sum=15, not 12. So only 3 fours sum to 12. So output "444"? But check if there is any other arrangement? With L=3, we need a+b=3, sum 4a+7b=12 => solving yields 4a+7(3-a)=12 => 4a+21-7a=12 => -3a=-9 => a=3, b=0. So indeed "444". That is minimal? But "444" is 3-digit. Could there be a 3-digit number smaller than "444"? The only 3-digit lucky numbers are combinations of 4 and 7; "444" is the smallest 3-digit lucky number (since "447" > "444", "474" > "444", etc). So correct.
For n=13, floor(13/7)=1, b=1: remaining=6, not divisible by 4 (6%4=2). b=0: remaining=13, not divisible by 4. So no solution? But is there a solution? L=2: sums: 8,11,14. Not 13. L=3: 4a+7b=13, a+b=3 => 4a+21-7a=13 => -3a=-8 => a=8/3 not integer. L=4: 4a+7(4-a)=28-3a=13 => 3a=15 => a=5, but a cannot exceed 4? Actually a+b=4, so a max 4. 5>4, so no. L=5: 4a+7(5-a)=35-3a=13 => 3a=22 => a not integer. L=6: 4a+7(6-a)=42-3a=13 => 3a=29 => no. L=7: 49-3a=13 => 3a=36 => a=12, too high. So no solution. So -1. Check if n=13 is representable? 13 = 4+4+4+? Actually 4+4+5? 5 not allowed. So indeed no.
For n=15, floor(15/7)=2, b=2: remaining=1, not divisible. b=1: remaining=8, divisible by 4 => a=2, total digits=3. Output "44"+"7" = "447"? Actually a=2, b=1 => "447". But is there L=2? Sums: 4+7=11, 7+7=14, no. So minimal digits is 3. Among 3-digit numbers with sum 15, possible combos: 4+4+7=15, 4+7+4=15, 7+4+4=15. Smallest lexicographically is "447". So correct.
Thus algorithm: try b from floor(n/7) down to 0: if (n - 7b) % 4 == 0, then a = (n - 7b) / 4, output string of a '4's + b '7's. If none, output -1.
But careful: we must also ensure that a is nonnegative integer. Since we start from b = floor(n/7) down to 0, the first valid gives the maximal b, thus minimal total digits. However, is it guaranteed that this yields the smallest numeric value? Let's verify: Two solutions with same total digits but different a and b? For a given total digits L, a and b are uniquely determined because sum and count are fixed. So if there are multiple b values that yield a solution, they would correspond to different L? Actually if we have two valid pairs (a1,b1) and (a2,b2) with b1 > b2, then L1 = a1+b1, L2 = a2+b2. Since b1 > b2, we need to compare L1 and L2. Typically L1 may be less than L2 because using more 7s reduces digit count. So L1 <= L2? Actually let's compute: b1 > b2, but a1 = (n-7b1)/4, a2 = (n-7b2)/4. Since b1 > b2, a1 < a2. So L1 = b1 + a1 = b1 + (n-7b1)/4 = (4b1 + n - 7b1)/4 = (n - 3b1)/4. Similarly L2 = (n - 3b2)/4. Since b1 > b2, L1 < L2. So indeed the larger b yields smaller L. So it's minimal L. So that approach yields the smallest number.
Now about numeric order: Suppose there are two different L that both yield a valid number, but maybe the one with slightly larger L could produce a number that is lexicographically smaller due to leading 4s? But as argued, any number with smaller L is automatically less because it has fewer digits. So the minimal L gives the globally minimal number. Therefore the first valid b (largest) gives minimal L and hence minimal number.
But wait, could there be a scenario where the minimal L yields a number that starts with 7? That would be larger than a number with larger L but starting with 4? For example, n=?? Let's try to see: Suppose n=7, minimal L = ceil(7/7)=1, b=1 yields "7". That's the only lucky number with sum 7? Actually 7 itself is a lucky number (single digit 7). That's the smallest? Could there be a 2-digit number with sum 7? Minimum sum for 2 digits is 8 (4+4). So no. So "7" is smallest. Suppose n=8: floor(8/7)=1, b=1 gives remaining 1 not divisible; b=0 gives a=2 => "44". L=2. Check L=1: possible sums: 4 or 7, not 8. So minimal L=2 yields "44". That's correct. Suppose n=18: floor(18/7)=2, b=2 => remaining 4 => a=1 => total digits=3 => number "4"+"77"? Actually a=1,b=2 => "477"? Wait string a 4s then b 7s => "4" then "77" => "477". L=3. Check if there is L=2? Sums: 4+7=11, 7+7=14, not 18. So minimal L=3. "477" is the smallest? Could there be a 3-digit number with sum 18 and digits only 4 and 7? Let's see all combos: 4+4+10 not allowed; 4+7+7=18; 7+4+7=18; 7+7+4=18. Among those, smallest lexicographically is "477". So yes.
Now consider n=21: floor(21/7)=3, b=3 => remaining 0 => a=0 => "777". L=3. Check L=2? 7+7=14, 4+7=11, 4+4=8, none 21. So "777" is minimal. That's fine.
Now n=22: floor(22/7)=3, b=3 => remaining 1, not divisible. b=2 => remaining 8 => a=2 => L=4 => "4477"? Actually a=2,b=2 => "4477"? Wait a=2,b=2 => "44"+"77" = "4477". L=4. Check if L=3 possible? 4a+7b=22, a+b=3 => 4a+7(3-a)=21-3a? Actually compute: 4a+21-7a =22 => -3a=1 => a=-1/3 no. So L=4 is minimal. But could there be L=4 with different arrangement that gives smaller number? For L=4, sum 22 yields a = (7L - n)/3 = (28-22)/3 = 6/3=2, b=2. So only arrangement "4477" is the minimal (since all 4s first). So okay.
Now consider n=25: floor(25/7)=3, b=3 => remaining 4 => a=1 => L=4 => "4777"? Actually a=1,b=3 => "4"+"777" = "4777". Check L=3? 4a+7(3-a)=21-3a=25 => -3a=4 => no. So "4777" is minimal. But could there be L=4 arrangement "4477"? That sum is 22, not 25. So fine.
Now consider n=28: floor(28/7)=4, b=4 => remaining 0 => a=0 => L=4 => "7777". L=3? 21 max, so no. So "7777".
Now consider n=29: floor(29/7)=4, b=4 => remaining 1 no; b=3 => remaining 8 => a=2 => L=5 => "44777"? Actually a=2,b=3 => "44"+"777" = "44777". Check L=4? 4a+7(4-a)=28-3a=29 => -3a=1 no. So L=5 minimal.
All seems consistent.
But wait: Could there be a case where the largest b solution yields a string that is not lexicographically minimal among solutions with the same L? But for a fixed L, a and b are unique, so no choice. So that's fine.
Thus algorithm: For b in range(n//7, -1, -1): if (n - 7b) % 4 == 0: a = (n - 7b)//4; output '4'*a + '7'*b; return. If loop ends, output -1.
Complexity: O(n/7) which is up to about 142857 for n=1e6, that's fine.
But we need to ensure that a can be zero (i.e., all 7s) or b can be zero (all 4s). That's fine.
Now consider n=1,000,000: floor(1e6/7)=142857 (since 7142857=999999). For b=142857, n-7b = 1 => not divisible by 4. Then b=142856 -> remaining = 1+7=8? Actually 1e6 - 7142856 = 1e6 - 999992 = 8, divisible by 4 => a=2. So total digits = 142856+2=142858. Output would be two 4s followed by 142856 7s: "44" + '7'*142856. That's huge but we can output string. That's okay.
But wait: The minimal number? Could there be a solution with b=142857? no. So our algorithm will find b=142856. That's correct.
Edge case: n=0? But n >=1 per constraints.
We should also consider that the number should be minimal numeric value. For the same total digits, as we argued, the smallest is all 4s then 7s. So our constructed string is correct.
But is it always true that the number formed by a 4s then b 7s is the minimum? Consider two numbers with same length and same counts of 4 and 7: any permutation will have same sum, but the smallest is with all 4s at the front. So yes.
Thus answer is straightforward.
But we must be careful about the order: The problem says "minimum lucky number". Usually among numbers with different lengths, the one with fewer digits is smaller. So our method yields that.
Implementation: read integer n. Then loop b from n//7 down to 0 inclusive. For each b, rem = n - 7*b; if rem >= 0 and rem % 4 == 0: a = rem // 4; print('4'*a + '7'*b); return. After loop, print(-1).
This works.
Alternatively, we could solve by iterating a from 0 to n//4, but that would be up to 250k, also fine, but we'd need to check 4a+7b=n, with b integer, and pick minimal L = a+b. To minimize L, we would want b as large as possible, so we could find maximal b. So the loop over b descending is more direct.
But note: Some n may be representable with both a,b but also with different L? But we already covered that.
Thus solution is simple.
But let's double-check some tricky cases: n=3? floor(3/7)=0, b=0, rem=3, not divisible by 4, output -1. Indeed, no lucky number sum 3. n=4: b=0, rem=4 divisible => a=1 => "4". That's minimal. n=5: b=0, rem=5 not divisible => -1. n=6: b=0 rem=6 not divisible => -1? But 6 can't be sum of 4 and 7? Actually 6= ? 4+? no 7? So -1. n=7: b=1 rem=0 => a=0 => "7". n=8: b=1 rem=1 no; b=0 rem=8 => a=2 => "44". n=9: b=1 rem=2 no; b=0 rem=9 no => -1. n=10: -1. n=11: b=1 rem=4 => a=1 => "47". n=12: b=1 rem=5 no; b=0 rem=12 => a=3 => "444". n=13: -1. n=14: b=2 rem=0 => "77". n=15: b=2 rem=1 no; b=1 rem=8 => a=2 => "447". n=16: b=2 rem=2 no; b=1 rem=9 no; b=0 rem=16 => a=4 => "4444". n=17: b=2 rem=3 no; b=1 rem=10 no; b=0 rem=17 no => -1. n=18: b=2 rem=4 => a=1 => "477"? Wait a=1, b=2 => "477". n=19: b=2 rem=5 no; b=1 rem=12 => a=3 => "4447"? Actually a=3,b=1 => "4447". Check L=4? Actually b=1 gives a=3 => total digits 4. But is there L=3? For L=3: 4a+7b=19, a+b=3 => 4a+21-7a=19 => -3a=-2 => no. So minimal L=4, number "4447". But is "4474" smaller? But we must output minimal number. "4447" is smaller than "4474" because compare digits: first three are 4, fourth: 7 vs 4? Actually "4447" vs "4474": at first two digits both 44; third digit: 4 vs 7, so 4<7 => "4447" is smaller. So our construction yields "4447". Good. n=20: b=2 rem=6 no; b=1 rem=13 no; b=0 rem=20 => a=5 => "44444". L=5. But maybe L=4? For L=4: 4a+7b=20, a+b=4 => 4a+28-7a=20 => -3a=-8 => no. So "44444" is correct.
Thus algorithm is verified.
Now, some might consider the possibility of leading zeros? Not allowed, digits are 4 and 7 only, so no zero.
Thus we can implement.
But we must also consider that the output may be huge if n is up to 1e6, the string length could be up to ~250k (if all 4s) or ~142858 as computed. That's okay as we can print a string of that length in Python. Memory is fine.
Implementation details: read input, convert to int, loop.
Edge: n=0? Not in constraints, but if it were, what would be minimal lucky number? Probably print nothing? But not needed.
We'll implement.
Now, could there be a situation where the minimal L yields a number that is not the minimal numeric because of leading digits? Let's double-check with a potential counterexample: Suppose we have two representations with different lengths, the shorter length is always smaller. So no.
But consider numbers with same length? Our solution gives unique counts for that length.
Thus answer is correct.
But wait, there is nuance: The minimal lucky number with sum n is not necessarily the one with minimal digits? For example, consider numbers of lengths 2 and 3: The smallest 2-digit lucky number is 44 (value 44). The smallest 3-digit lucky number is 444 (value 444). 44 < 444. So shorter length always yields smaller number. So yes.
Thus algorithm stands.
Now think about constraints: n up to 10^6. Our loop from floor(n/7) down to 0 worst-case about 142857 iterations, fine.
But we could also solve using modular arithmetic: Since we want n = 4a + 7b, we can find b mod 4? But loop is simple.
Thus final solution: Python code:
def solve():
import sys
n = int(sys.stdin.readline().strip())
for b in range(n // 7, -1, -1):
rem = n - 7 * b
if rem % 4 == 0:
a = rem // 4
sys.stdout.write('4' * a + '7' * b)
return
sys.stdout.write('-1')
But careful: We need to output as a string. Ensure that if a is zero we don't output the 4s part, and if b is zero similarly. That's fine.
Now we need to handle the case where n is 0? Not needed.
Thus answer ready.
But also, there's another approach: Since the number of 7s mod 4 determines. But not needed.
I'll also consider proof: Minimal digits: we maximize b because each 7 contributes more sum per digit, reducing total digits. For a given b, a = (n-7b)/4 must be integer >=0. So we look for largest b such that n-7b divisible by 4. That yields minimal a+b.
Now, confirm that the constructed number is indeed minimal among all solutions: For the same b and a, any permutation yields larger number because moving a 4 to the left makes number smaller. So our string with all 4s then 7s is minimal.
Thus solution correct.
Now test with some random values to ensure no off-by-one.
Also, consider n=1000000. Our loop will start from b=142857 (since 142857*7=999999), then b=142856 yields rem=8 divisible by 4 => a=2. So output '44' + '7'*142856. That is 2+142856 = 142858 digits. That's okay.
But is there any solution with b=142857? Not because 1e6 - 7142857 = 1, not divisible. Could there be solution with b=142855? That would give rem=1e6 - 7142855 = 1e6 - 999985 = 15, not divisible by 4. Next b=142854 => rem=22? Actually 1e6 - 7*142854 = 1e6 - 999978 = 22, not divisible by 4? 22%4=2. b=142853 => rem=29, %4=1. b=142852 => rem=36, 36%4=0 => a=9? Actually rem=36 => a=9, total digits = 142852+9=142861 which is larger than 142858, so not minimal. So indeed the solution we found is minimal.
Thus code works.
But consider n=21: b=3 works, rem=0, a=0 => "777". Output is "777". That's fine.
Now, check n=22: b=3 rem=1, b=2 rem=8 => a=2 => "4477". That's correct.
Now, think about n=6? Not representable, output -1.
Now, potential pitfalls: if a and b are both zero? That would be sum 0, but n>=1 so ignore.
Now, output format: just print the number or -1.
Thus final answer.
I'll produce a clear explanation in final.
But wait, there's a subtlety: For the minimal number, we said we want the lexicographically smallest among those with minimal digits. But is it always true that the smallest numeric corresponds to smallest lexicographic? For numbers with same length, yes, lexicographic order matches numeric order. So we want lexicographically smallest string of digits (with digits 4 and 7) that sum to n and have minimal length. Among all strings of length L with given sum, the lexicographically smallest is the one with as many 4s as possible at the beginning. But our construction for the found L gives a specific (a,b). However, could there be other strings of same length L that also sum to n but have different arrangement? For given L and sum n, the counts a and b are determined uniquely because we have two equations: a+b = L, 4a+7b = n. So there is only one solution for (a,b). Thus the only strings of that length with that sum are permutations of a 4s and b 7s. Among those, lexicographically smallest is indeed all 4s then 7s. So our constructed string is the unique minimal number for that L.
But is it possible that there are multiple L that yield the same minimal length? No, minimal length is unique. So we are done.
Thus answer is straightforward.
Now I'll write the solution.
However, I've seen some solutions using DP or BFS, but this is simpler.
I'll include explanation.
Now write final answer. ```python
import sys
def main():
n = int(sys.stdin.readline().strip())
# Try the largest possible number of 7's to minimise total digits.
for b in range(n // 7, -1, -1):
remainder = n - 7 * b
if remainder >= 0 and remainder % 4 == 0:
a = remainder // 4
sys.stdout.write('4' * a + '7' * b)
return
sys.stdout.write('-1')
if name == "main":
main()