Petya loves lucky numbers. Everybody knows that positive integers are lucky if their decimal representation doesn't contain digits other than 4 and 7. For example, numbers 47, 744, 4 are lucky and 5, 17, 467 are not.

Lucky number is super lucky if it's decimal representation contains equal amount of digits 4 and 7. For example, numbers 47, 7744, 474477 are super lucky and 4, 744, 467 are not.

One day Petya came across a positive integer n. Help him to find the least super lucky number which is not less than n.

Input

The only line contains a positive integer n (1 ≤ n ≤ 10100000). This number doesn't have leading zeroes.

Output

Output the least super lucky number that is more than or equal to n.

Examples

Input

4500


Output

4747


Input

47


Output

47

Difficulty: MEDIUM_HARD
Tags: Dynamic programming

Greedy algorithms
Skill types: Dynamic programming

Greedy algorithms

Starter code:

Solutions:
from itertools import permutations as p

def ck(num, arr):
	for i in arr:
		if i >= num:
			print(i)
			return
x = input()
z = len(x)
if z == 1:
	print(47)
elif z == 2:
	if int(x) <= 74:
		arr = [47, 74]
		ck(int(x), arr)
	else:
		print(4477)
elif z == 3:
	print(4477)
elif z == 4:
	if int(x) <= 7744:
		arr4 = sorted([int(''.join(i)) for i in p('4477')])
		ck(int(x), arr4)
	else:
		print(444777)
elif z == 5:
	print(444777)
elif z == 6:
	if int(x) <= 777444:
		arr6 = sorted([int(''.join(i)) for i in p('444777')])
		ck(int(x), arr6)
	else:
		print(44447777)
elif z == 7:
	print(44447777)
elif z == 8:
	if int(x) <= 77774444:
		arr8 = sorted([int(''.join(i)) for i in p('44447777')])
		ck(int(x), arr8)
	else:
		print(4444477777)
else:
	print(4444477777)


import math, re, sys, string, operator, functools, fractions, collections
sys.setrecursionlimit(10 ** 7)
RI = lambda x=' ': list(map(int, input().split(x)))
RS = lambda x=' ': input().rstrip().split(x)
dX = [-1, 1, 0, 0, -1, 1, -1, 1]
dY = [0, 0, -1, 1, 1, -1, -1, 1]
mod = int(1000000000.0 + 7)
eps = 1e-06
pi = math.acos(-1.0)
MAX = 10 ** 5 + 10
n = RI()[0]

def check(n):
	cnt = [0, 0]
	while n:
		if n % 10 == 4:
			cnt[0] += 1
		else:
			cnt[1] += 1
		n //= 10
	return cnt[0] == cnt[1]
for l in range(2, 11, 2):
	for i in range(1 << l):
		num = i
		v = 0
		for j in range(l - 1, -1, -1):
			v *= 10
			if num >> j & 1:
				v += 7
			else:
				v += 4
		if v >= n and check(v):
			print(v)
			exit(0)


from collections import deque
n = int(input())

def isSuperLucky(n):
	fours = str(n).count('4')
	sevens = str(n).count('7')
	return sevens > 0 and sevens == fours

class Node:

	def __init__(self, data):
		self.data = data
		self.left = None
		self.right = None

	def find(self, target, t):
		if self.data >= target and isSuperLucky(self.data):
			t.append(self.data)
		elif self.left and self.right:
			l = self.left.find(target, t)
			if l:
				t.append(l)
			r = self.right.find(target, t)
			if r:
				t.append(r)
root = Node(0)
queue = deque([root])
while len(queue) > 0:
	node = queue.pop()
	node.left = Node(node.data * 10 + 4)
	node.right = Node(node.data * 10 + 7)
	if node.data < n:
		queue.append(node.left)
		queue.append(node.right)
t = []
root.find(n, t)
print(sorted(t)[0])


z = []
for i in range(1, 11):
	for j in range(1 << i):
		s = bin(j)
		s = s[2:]
		if s.count('1') == s.count('0'):
			z += [int(s.replace('1', '4').replace('0', '7'))] + [int(s.replace('1', '7').replace('0', '4'))]
z.sort()
a = int(input())
for i in z:
	if a <= i:
		print(i)
		break


from itertools import permutations
number = input()
le = len(number)
l = le - int(le / 2)
n = int(number)
result = [x for x in '4' * l + '7' * l]
if le % 2 == 0:
	f = sorted(list((int(''.join(x)) for x in set(permutations(result)))))
	m = 0
	for _ in f:
		if _ >= n:
			print(_)
			m += 1
			break
	if m == 0:
		print('4' * (l + 1) + '7' * (l + 1))
else:
	print('4' * l + '7' * l)


import bisect

def fun():
	arr = [0] * 100005
	arr[1] = 4
	arr[2] = 7
	for i in range(3, 100005):
		if i % 2 != 0:
			arr[i] = arr[i // 2] * 10 + 4
		else:
			arr[i] = arr[i // 2 - 1] * 10 + 7
	return arr
n = int(input())
res = fun()
val = bisect.bisect_left(res, n)
for i in range(val, 100005):
	l = [int(x) for x in str(res[i])]
	if l.count(4) == l.count(7):
		print(res[i])
		break


N = int(input())
queue = []
queue += [0]
queue[-1] = (0, 0)
while len(queue) != 0:
	val = queue[0][0]
	C = queue[0][1]
	if val >= N and C == 0:
		print(val)
		break
	queue.pop(0)
	queue.append((val * 10 + 4, C + 1))
	queue.append((val * 10 + 7, C - 1))


def check(num):
	num = list(str(num))
	if sorted(set(num)) != ['4', '7']:
		return False
	if num.count('4') == num.count('7'):
		return True
	return False
num = int(input())
no = [0]
while 1:
	if check(no[0]) and no[0] >= num:
		print(no[0])
		break
	if no[0] < 10000000000.0:
		no.append(no[0] * 10 + 4)
		no.append(no[0] * 10 + 7)
	del no[0]


n = int(input())
Q = [0]
i = 0
while True:
	t = Q[i]
	if t >= n:
		if str(t).count('4') == str(t).count('7'):
			print(t)
			break
	Q += [t * 10 + 4, t * 10 + 7]
	i += 1


sa = int(input())
suplucky = [47, 74, 4477, 4747, 4774, 7447, 7474, 7744, 747447, 744747, 744774, 477744, 444777, 777444, 447477, 747474, 774744, 474774, 474477, 447747, 774474, 447774, 744477, 774447, 747744, 477447, 477474, 474747]
supluckyb = [44747747, 77774444, 74747744, 44477774, 47447774, 77744474, 77444477, 47744774, 74477474, 74744774, 44774477, 77444747, 74474747, 74447747, 74447774, 44747774, 74477447, 74474477, 47744747, 47474747, 77447474, 77444774, 77474744, 74447477, 77474447, 74747474, 74774744, 47744477, 74777444, 44447777, 47747474, 47774474, 47747447, 44774747, 47444777, 44474777, 77474474, 74477744, 47477744, 74474774, 74747447, 47447477, 47477447, 44477477, 77747444, 77744744, 44744777, 77477444, 74744477, 47474774, 74444777, 44747477, 44777744, 47774744, 44777447, 47477474, 47447747, 47777444, 74774447, 44477747, 77447447, 77744447, 74774474, 44774774, 74744747, 47774447, 47474477, 77447744, 44777474, 47747744]
superlucky = suplucky + supluckyb
superlucky.sort()
element = 0
sas = []
for element in superlucky:
	if element >= sa:
		sas.append(element)
if sas == []:
	print(4444477777)
else:
	print(min(sas))


import math
from itertools import permutations as p
n = input()
length = math.ceil(len(n) / 2)
s = '47' * length
flag = 0
for i in sorted(set(p(s))):
	if int(''.join(i)) >= int(n):
		print(''.join(i))
		flag = 1
		break
if flag == 0:
	s = '47' * (length + 1)
	for i in sorted(set(p(s))):
		if int(''.join(i)) >= int(n):
			print(''.join(i))
			break


n = int(input())
possible = [0]
i = 0
while True:
	k = possible[i]
	if k >= n and str(k).count('4') == str(k).count('7'):
		print(k)
		exit(0)
	possible += [10 * k + 4, 10 * k + 7]
	i += 1


from sys import stdin, stdout
ii1 = lambda : int(stdin.readline().strip())
is1 = lambda : stdin.readline().strip()
iia = lambda : list(map(int, stdin.readline().strip().split()))
isa = lambda : stdin.readline().strip().split()
mod = 1000000007
from itertools import permutations as perm
pos = []
n = 2
while n <= 8:
	cur = '4' * (n // 2) + '7' * (n // 2)
	pos.extend(sorted([int(''.join(i)) for i in perm(cur)]))
	n += 2
pos.append(4444477777)
n = ii1()
for i in pos:
	if i >= n:
		print(i)
		break


import math

def facts(n):
	ans = []
	for i in range(1, int(math.sqrt(n) + 1)):
		if n % i == 0:
			ans.append(i)
			ans.append(n // i)
	ans = sorted(list(dict.fromkeys(ans)))
	if ans[-1] == n:
		ans = ans[:-1]
	return ans

def lucks(n):
	nums = []
	for mask in range(1 << n):
		tmp = ''
		for x in range(n, 0, -1):
			for i in range(n):
				if mask >> i & 1:
					tmp += '4'
				else:
					tmp += '7'
			if tmp[:x].count('4') == tmp[:x].count('7'):
				nums.append(int(tmp[:x]))
	return sorted(set(nums))
ls = lucks(10)
n = int(input())
left = 0
right = len(ls)
while left < right:
	mid = (left + right) // 2
	if ls[mid] >= n:
		right = mid
	else:
		left = mid + 1
print(ls[left])


lucky = []

def func(number, fours, sevens):
	if number > 10000000000.0:
		return
	elif fours == sevens:
		lucky.append(number)
	func(10 * number + 7, fours, sevens + 1)
	func(10 * number + 4, fours + 1, sevens)
func(4, 1, 0)
func(7, 0, 1)
n = int(input())
lucky.sort()
for x in lucky:
	if x >= n:
		print(x)
		break


n = input()
answer = ''
if len(n) % 2 == 0:
	f = len(n) // 2
	s = len(n) // 2
	for x in n:
		if x == '4':
			if f > 0:
				f -= 1
				answer += '4'
			else:
				s -= 1
				answer += '7'
				break
		elif x == '7' and s > 0:
			s -= 1
			answer += '7'
		else:
			if x in ('5', '6'):
				if s > 0:
					answer += '7'
					s -= 1
				elif f == len(n) // 2:
					f = len(n) // 2 + 1
					s = f
					answer = ''
				elif answer.rindex('4') != 0:
					answer = answer[0:answer.rindex('4')]
					f += 1
					s = len(n) - len(answer) - f
					while s == 0 and f != len(n) // 2:
						answer = answer[0:answer.rindex('4')] if answer.rindex('4') != 0 else ''
						f += 1
						s = len(n) - len(answer) - f
					if s != 0:
						answer += '7'
						s -= 1
					else:
						f = len(n) // 2 + 1
						s = f
						answer = ''
				else:
					f += 1
					s = len(n) // 2 - 1
					answer = '7'
			elif x in ('8', '9', '7'):
				if f == len(n) // 2:
					f = len(n) // 2 + 1
					s = f
					answer = ''
				elif answer.rindex('4') != 0:
					answer = answer[0:answer.rindex('4')]
					f += 1
					s = len(n) - len(answer) - f
					while s == 0 and f != len(n) // 2:
						answer = answer[0:answer.rindex('4')] if answer.rindex('4') != 0 else ''
						f += 1
						s = len(n) - len(answer) - f
					if s != 0:
						answer += '7'
						s -= 1
					else:
						f = len(n) // 2 + 1
						s = f
						answer = ''
				else:
					f += 1
					s = len(n) // 2 - 1
					answer = '7'
			break
	answer += '4' * f + '7' * s
else:
	answer = '4' * ((len(n) + 1) // 2) + '7' * ((len(n) + 1) // 2)
print(answer)


from itertools import product
n = int(input())
l = []
for i in range(2, 11, 2):
	l = l + [int(''.join([str(y) for y in x])) for x in list(product([4, 7], repeat=i)) if x.count(4) == i / 2]
for x in sorted(l):
	if x == n or x > n:
		print(x)
		exit(0)


from itertools import permutations
ans = []
for i in range(2, 10, 2):
	temp = [4, 7] * (i // 2)
	x = list(permutations(temp))
	for j in x:
		t = ''
		for k in j:
			t += str(k)
		ans.append(int(t))
ans.append(4444477777)
ans.sort()
n = int(input())
for i in ans:
	if i >= n:
		print(i)
		break


import sys

def lucky(val, c4, c7, cnt):
	if max(c4, c7) > int(cnt / 2):
		return
	if c4 + c7 == cnt:
		if val >= n:
			print(val)
			sys.exit()
	lucky(val * 10 + 4, c4 + 1, c7, cnt)
	lucky(val * 10 + 7, c4, c7 + 1, cnt)
n = int(input())
i = 2
while True:
	lucky(0, 0, 0, i)
	i += 2


import math
R = lambda : map(int, input().split())
n = int(input())
(q, res) = (['4', '7'], math.inf)
while q:
	s = q.pop(0)
	if s.count('4') == s.count('7') and int(s) >= n:
		res = min(res, int(s))
	if len(s) < 10:
		q.append(s + '4')
		q.append(s + '7')
print(res)


nums = []
for mask in range(1 << 10):
	set = 0
	count4 = 0
	count7 = 0
	for i in range(10):
		if mask & 1 << i != 0:
			set += 1
	bound = -1
	for i in range(10):
		if mask & 1 << i == 0:
			count7 += 1
		else:
			count4 += 1
		if count7 == count4:
			bound = i + 1
	if bound == -1:
		continue
	count4 = 0
	s = str()
	for i in range(0, bound):
		if mask & 1 << i == 0:
			s += '7'
		else:
			s += '4'
			count4 += 1
	if count4 != set:
		continue
	nums.append(int(s))
N = int(input())
nums.sort()
start = 0
end = len(nums) - 1
while start < end:
	mid = (start + end) // 2
	if nums[mid] < N:
		start = mid + 1
	else:
		end = mid
print(nums[start])


lucky = []

def findLucky(s):
	if len(s) > 11:
		return
	ans1 = s + '4'
	ans2 = s + '7'
	if ans1.count('4') == ans1.count('7'):
		lucky.append(int(ans1))
	if ans2.count('4') == ans2.count('7'):
		lucky.append(int(ans2))
	findLucky(s + '4')
	findLucky(s + '7')
findLucky('')
lucky.sort()
n = int(input())
for i in lucky:
	if i >= n:
		print(i)
		exit(0)


from itertools import *
k = int(input())
n = len(str(k))
s = set()
if n % 2 != 0:
	n = n + 1
c = ['4', '7']
a = []
for i in range(n):
	a.append(c)
for i in product(*a):
	if i.count('7') == i.count('4'):
		s.add(int(''.join(i)))
ll = sorted(list(s))
if not k <= ll[-2] and k <= ll[-1]:
	print(ll[-1])
elif k > ll[-1]:
	n = n + 1
	if n % 2 != 0:
		n = n + 1
	print('4' * (n // 2) + '7' * (n // 2))
elif k <= ll[-1]:
	for i in ll:
		if k <= i:
			print(i)
			break


l = [4, 7]
n = int(input())
elem = 0
while l[elem] < 10 ** 9 + 100:
	l.append(l[elem] * 10 + 4)
	l.append(l[elem] * 10 + 7)
	elem += 1
l = [i for i in l if str(i).count('4') == str(i).count('7')]
import bisect
var = bisect.bisect_right(l, n)
if n in l:
	print(l[var - 1])
else:
	print(l[var])


n = int(input())
ans = []

def lucky(x):
	if x >= n and str(x).count('4') == str(x).count('7'):
		ans.append(x)
	if x < 10 ** 12:
		lucky(x * 10 + 4)
		lucky(x * 10 + 7)
lucky(0)
ans.sort()
print(ans[0])


def main():
	from bisect import bisect_left
	l = (47, 74, 4477, 4747, 4774, 7447, 7474, 7744, 444777, 447477, 447747, 447774, 474477, 474747, 474774, 477447, 477474, 477744, 744477, 744747, 744774, 747447, 747474, 747744, 774447, 774474, 774744, 777444, 44447777, 44474777, 44477477, 44477747, 44477774, 44744777, 44747477, 44747747, 44747774, 44774477, 44774747, 44774774, 44777447, 44777474, 44777744, 47444777, 47447477, 47447747, 47447774, 47474477, 47474747, 47474774, 47477447, 47477474, 47477744, 47744477, 47744747, 47744774, 47747447, 47747474, 47747744, 47774447, 47774474, 47774744, 47777444, 74444777, 74447477, 74447747, 74447774, 74474477, 74474747, 74474774, 74477447, 74477474, 74477744, 74744477, 74744747, 74744774, 74747447, 74747474, 74747744, 74774447, 74774474, 74774744, 74777444, 77444477, 77444747, 77444774, 77447447, 77447474, 77447744, 77474447, 77474474, 77474744, 77477444, 77744447, 77744474, 77744744, 77747444, 77774444, 4444477777)
	print(l[bisect_left(l, int(input()))])
main()


import math
from collections import defaultdict, Counter
R = lambda : map(int, input().split())
org = input()
num = [x for x in org]
n = len(num)
(mi, mx) = ('4' * ((n + 1) // 2) + '7' * ((n + 1) // 2), '7' * ((n + 1) // 2) + '4' * ((n + 1) // 2))
if n % 2:
	print(mi)
	exit(0)
if ''.join(num) > mx:
	print('4' + mi + '7')
	exit(0)
(b, sc, fc) = (-1, 0, 0)
for i in range(n):
	if num[i] != '4' and num[i] != '7':
		b = i
		break
	sc += num[i] == '7'
	fc += num[i] == '4'
	if sc > n // 2 or fc > n // 2:
		b = i
		break
if b == -1:
	if sc != n // 2 or fc != n // 2:
		print('1 ' + str(sc) + ' ' + str(fc))
	else:
		print(org)
elif num[b] < '4':
	res = ''.join(num[:b]) + '4' * (n // 2 - fc) + '7' * (n // 2 - sc)
	if res < ''.join(num) or res.count('4') != res.count('7'):
		print('2 ' + str(sc) + ' ' + str(fc))
	else:
		print(res)
elif num[b] < '7':
	res = ''.join(num[:b]) + '7' + '4' * (n // 2 - fc) + '7' * (n // 2 - sc - 1)
	if res < ''.join(num) or res.count('4') != res.count('7'):
		print('3 ' + str(sc) + ' ' + str(fc))
	else:
		print(res)
else:
	sc -= num[b] == '7'
	while num[b - 1] != '4' or n // 2 - sc <= 0:
		sc -= num[b - 1] == '7'
		fc -= num[b - 1] == '4'
		b -= 1
	b -= 1
	fc -= 1
	res = ''.join(num[:b]) + '7' + '4' * (n // 2 - fc) + '7' * (n // 2 - sc - 1)
	if res < ''.join(num) or res.count('4') != res.count('7'):
		print('4 ' + str(sc) + ' ' + str(fc) + ' ' + str(res.count('4')) + ' ' + str(res.count('7')) + ' ' + str(n))
	else:
		print(res)


n = input()
length = len(n)
if length % 2 == 1:
	result = '4' * ((length + 1) // 2) + '7' * ((length + 1) // 2)
else:
	half = length // 2
	if n > half * '7' + half * '4':
		result = (half + 1) * '4' + (half + 1) * '7'
	elif n < half * '4' + half * '7':
		result = half * '4' + half * '7'
	else:
		four_count = seven_count = half
		for i in range(length):
			digit = n[i]
			if digit < '4':
				result = n[:i] + '4' * four_count + '7' * seven_count
				break
			if digit < '7' and digit != '4':
				if seven_count == 0:
					result = n[:nearest_four] + '7' + '4' * (four_count + 1)
					result += '7' * (length - len(result))
				else:
					result = n[:i] + '7' + '4' * four_count + '7' * (seven_count - 1)
				break
			if digit > '7':
				result = n[:nearest_four] + '7' + '4' * (four_count + 1)
				result += '7' * (length - len(result))
				break
			if digit == '4':
				nearest_four = i
				four_count -= 1
				if four_count < 0:
					result = n[:i] + '7' * seven_count
					break
			if digit == '7':
				seven_count -= 1
				if seven_count < 0:
					result = n[:nearest_four] + '7' + '4' * (four_count + 1)
					result += '7' * (length - len(result))
					break
			if four_count == 0 and seven_count == 0:
				result = n
				break
print(result)


def nextLuckyNumber(digits: str) -> str:
	n = len(digits)
	halfLen = n >> 1
	if n & 1 or digits > f"{'7' * halfLen}{'4' * halfLen}":
		halfLen += 1
		return f"{'4' * halfLen}{'7' * halfLen}"
	result = [0 for i in range(n)]
	remaining = [halfLen, halfLen]
	for i in range(n):
		add4 = False
		if remaining[0]:
			if digits[i] < '4':
				add4 = True
			elif digits[i] == '4':
				lastIdx = i + remaining[1]
				for j in range(i + 1, n):
					d = '7' if j <= lastIdx else '4'
					if digits[j] < d:
						add4 = True
						break
					elif digits[j] > d:
						add4 = False
						break
					elif j == n - 1:
						add4 = True
		if add4:
			result[i] = '4'
			remaining[0] -= 1
		else:
			result[i] = '7'
			remaining[1] -= 1
		if result[i] > digits[i] or remaining[1] == 0:
			for k in range(i + 1, i + 1 + remaining[0]):
				result[k] = '4'
			for k in range(i + 1 + remaining[0], i + 1 + remaining[0] + remaining[1]):
				result[k] = '7'
			break
	return ''.join(result)
print(nextLuckyNumber(input()))


slucky = [4, 7]
i = 0
while slucky[-1] < 10 ** 9:
	slucky.append(slucky[i] * 10 + 4)
	slucky.append(slucky[i] * 10 + 7)
	i += 1

def superlucky(n):
	four = 0
	seven = 0
	while n > 0:
		if n % 10 == 4:
			four += 1
		else:
			seven += 1
		n = n // 10
	if four == seven:
		return True
	return False
lucky = []
for item in slucky:
	if superlucky(item):
		lucky.append(item)
lucky = [0] + lucky + [4444477777]

def binarySearch(n):
	left = 0
	right = len(lucky) - 1
	mid = (left + right) // 2
	while left <= right:
		mid = (left + right) // 2
		if lucky[mid - 1] < n <= lucky[mid]:
			print(lucky[mid])
			break
		elif n > lucky[mid]:
			left = mid + 1
		else:
			right = mid - 1
n = int(input())
binarySearch(n)


import itertools as it
import bisect as bs

def get_lucky_nums(leng):
	s = '4' * (leng // 2) + '7' * (leng // 2)
	return list(sorted(set([int(''.join(p)) for p in it.permutations(s)])))
n = input()
leng = len(n)
if n == '1000000000':
	print('4444477777')
elif leng % 2 or n[0] > '7':
	print('4' * (leng // 2 + 1) + '7' * (leng // 2 + 1))
else:
	lucky_num = get_lucky_nums(leng + 1)
	i = bs.bisect_left(lucky_num, int(n))
	if i == len(lucky_num):
		print('4' * (leng // 2 + 1) + '7' * (leng // 2 + 1))
	else:
		print(lucky_num[i])


from itertools import permutations
s = str(input(''))
ma_rs = []
mi_rs = []
n = int((len(s) + 1) / 2)
for i in range(n):
	ma_rs.append(4)
	mi_rs.append(4)
for i in range(n):
	ma_rs.append(7)
	mi_rs.append(7)
mi_rs.sort(key=int)
i_mi_rs = int(''.join(map(str, mi_rs)))
ma_rs.sort(key=int, reverse=True)
i_ma_rs = int(''.join(map(str, ma_rs)))
if int(s) <= i_mi_rs:
	print(i_mi_rs)
elif int(s) <= i_ma_rs:
	perm = set(permutations(mi_rs))
	for i in sorted(list(perm)):
		i = int(''.join(map(str, i)))
		if i >= int(s):
			print(i)
			break
else:
	n = n + 1
	mi_rs = []
	for i in range(n):
		mi_rs.append(4)
	for i in range(n):
		mi_rs.append(7)
	print(int(''.join(map(str, mi_rs))))


n = int(input())
li = [0]
i = 0
while True:
	last = li[i]
	if last >= n and str(last).count('4') == str(last).count('7'):
		print(li[i])
		exit()
	li += [10 * li[i] + 4, 10 * li[i] + 7]
	i += 1


store = []

def getAns(n, a, b, s):
	if n == 0:
		store.append(int(s))
		return
	if a > 0 and b > 0:
		getAns(n - 1, a - 1, b, s + '4')
		getAns(n - 1, a, b - 1, s + '7')
	elif a == 0:
		getAns(n - 1, a, b - 1, s + '7')
	else:
		getAns(n - 1, a - 1, b, s + '4')
N = int(input())
for i in range(2, 14 + 1, 2):
	getAns(i, i // 2, i // 2, '')
for i in store:
	if i >= N:
		print(i)
		break


n = input()
x = len(n)
if x % 2 == 0:
	a = ['4'] * (x // 2) + ['7'] * (x // 2)
	z = ''
	if int(n) <= int('7' * (x // 2) + '4' * (x // 2)):
		for i in range(x):
			if int(n[i]) <= 4:
				if '4' in a:
					z += '4'
					a.pop(a.index('4'))
					if int(z + str(''.join(a))) > int(n):
						z = z + str(''.join(a))
						break
				else:
					z += str(''.join(a))
					break
			elif int(n[i]) <= 7:
				if '7' in a:
					z += '7'
					a.pop(a.index('7'))
				else:
					z += str(''.join(a))
					break
			else:
				if z[-1] == '4':
					a = ['4'] + a
					z = z[:len(z) - 1] + '7'
					a.pop(a.index('7'))
				if '4' in a:
					z += '4'
					a.pop(a.index('4'))
				else:
					z += '7'
					break
	else:
		z = '4' * ((x + 2) // 2) + '7' * ((x + 2) // 2)
	if int(z) >= int(n):
		print(z)
	else:
		a = ['4'] * (x // 2) + ['7'] * (x // 2)
		z = ''
		for i in range(x - 1):
			if n[i] != n[i + 1]:
				z += '7'
				a.pop(a.index('7'))
				if int(z + ''.join(a)) > int(n):
					print(z + ''.join(a))
					break
			else:
				z += '4'
				a.pop(a.index('4'))
else:
	print('4' * ((x + 1) // 2) + '7' * ((x + 1) // 2))


from math import *
ch = str(input())
if len(ch) % 2 == 1 and len(ch) < 9:
	ch = '1' * (len(ch) + 1)
i = 0
res = ''
maxx = 1000000000
if int(ch) > int('7' * ceil(len(ch) / 2) + '4' * (len(ch) // 2)):
	if len(ch) % 2 == 1:
		res = '4' * ceil(len(ch) / 2) + '7' * ceil(len(ch) / 2)
	else:
		res = '4' * (1 + ceil(len(ch) / 2)) + '7' * (1 + ceil(len(ch) / 2))
elif len(ch) == 9 or len(ch) == 10:
	res = '4444477777'
else:

	def anagrame(x):
		if len(x) <= 1:
			return [x]
		elif len(x) == 2:
			return list(set([x, x[1] + x[0]]))
		else:
			return list(set([x[i] + d for i in range(len(x)) for d in anagrame(x[:i] + x[i + 1:])]))
	tab = list()
	tab = anagrame('7' * ceil(len(ch) / 2) + '4' * (len(ch) // 2)) + anagrame('7' * (len(ch) // 2) + '4' * ceil(len(ch) / 2))
	for c in tab:
		if int(c) >= int(ch):
			maxx = min(maxx, int(c))
			res = str(maxx)
print(res)


string = input()
l = len(string)
if l % 2 != 0:
	print('4' * ((l + 1) // 2) + '7' * ((l + 1) // 2))
	exit()
n = l // 2
worst = '7' * n + '4' * n
if worst < string:
	print('4' * (n + 1) + '7' * (n + 1))
	exit()
newstr = '4' * n + '7' * n
from itertools import permutations as pmt
List = pmt(newstr)
for x in List:
	result = ''.join(x)
	if result >= string:
		print(result)
		exit()


ans = 10000000000000
g = int(input())
s = int(0)

def vet(n, m):
	global ans, s
	if s < ans and s >= g and (n == m):
		ans = s
	if n > 0:
		s = s * 10 + 4
		vet(n - 1, m)
		s //= 10
	if m > 0:
		s = s * 10 + 7
		vet(n, m - 1)
		s //= 10
vet(5, 5)
print(ans)


from itertools import permutations

def check(n, giv):
	if n % 2 == 0:
		f = []
		for i in range(n):
			if i % 2 == 0:
				f.append(4)
			else:
				f.append(7)
		f = list(permutations(f))
		ans = []
		for i in f:
			ans1 = ''
			if i[0] == 0:
				continue
			for j in i:
				ans1 += str(j)
			ans.append(int(ans1))
		(mini, number) = (float('inf'), 0)
		for i in ans:
			if i > giv and i - giv < mini:
				mini = i - giv
				number = i
		return number

def l():
	return list(map(int, input().rstrip().split()))

def ini():
	return int(input())

def te():
	return list(map(int, input().split()))

def st():
	return input().rstrip()
giv = int(input())
n = len(str(giv))
if giv == 10 ** 9:
	ans = ''
	for i in range(len(str(giv))):
		if i < n // 2:
			ans += '4'
		else:
			ans += '7'
	print(ans)
	exit()
s = str(giv)
y = s
if y.count('7') == y.count('4') and y.count('7') + y.count('4') == len(y):
	print(y)
else:
	n = len(s)
	if n % 2 == 0:
		if s[0] < '7':
			print(check(n, giv))
			exit()
		y = check(n, giv)
		if y != 0:
			print(y)
		else:
			ans = ''
			for i in range(n + 2):
				if i <= n // 2:
					ans += '4'
				else:
					ans += '7'
			print(ans)
	else:
		ans = ''
		for i in range(n + 1):
			if i <= n // 2:
				ans += '4'
			else:
				ans += '7'
		print(ans)


l = set()

def helper(num):
	if len(num) > 12:
		return
	if len(num) > 0 and num.count('4') == num.count('7'):
		l.add(int(num))
	helper(num + '4')
	helper(num + '7')
helper('')
l = sorted(list(l))
n = int(input())
i = 0
if n in l:
	print(n)
else:
	while l[i] < n:
		i += 1
	print(l[i])


import sys
RI = lambda : [int(x) for x in sys.stdin.readline().strip().split()]
rw = lambda : sys.stdin.readline().strip().split()
ri = lambda : int(sys.stdin.readline().strip())
rs = lambda : sys.stdin.readline().strip()
from collections import defaultdict as df
from math import *
l = ['0']

def next(size):
	if size == 10:
		return
	temp = []
	for i in l:
		temp.append(i + '4')
		temp.append(i + '7')
	l.extend(temp)
	next(size + 1)
next(0)
l = [int(i) for i in l if i.count('4') == i.count('7')]
n = ri()
for i in l:
	if i >= n:
		print(i)
		break


n = int(input())
a = []

def foo(x):
	if x >= n and str(x).count('4') == str(x).count('7'):
		a.append(x)
	if x < 10 ** 12:
		foo(10 * x + 4)
		foo(10 * x + 7)
foo(0)
a = sorted(a)
print(a[0])


from sys import stdin, stdout
from math import gcd, sqrt, inf
from collections import deque
input = stdin.readline
R = lambda : map(int, input().split())
I = lambda : int(input())
S = lambda : input().rstrip('\n')
L = lambda : list(R())
P = lambda x: stdout.write(x)
hg = lambda x, y: (y + x - 1) // x * x
pw = lambda x: 1 if x == 1 else 1 + pw(x // 2)
chk = lambda x: chk(x // 2) if not x % 2 else True if x == 1 else False
N = inf
n = I()
(q, r) = (['4', '7'], N)
while q:
	s = q.pop(0)
	if s.count('4') == s.count('7') and int(s) >= n:
		r = min(r, int(s))
	if len(s) < 10:
		q += (s + '4',)
		q += (s + '7',)
print(r)


import itertools
num = input()
n = len(num)
if n % 2 == 1:
	n += 1
	print('4' * (n // 2) + '7' * (n // 2))
	exit()
if int('7' * (n // 2) + '4' * (n // 2)) < int(num):
	n += 2
	print('4' * (n // 2) + '7' * (n // 2))
	exit()
if int(num[0]) < 4:
	print('4' * (n // 2) + '7' * (n // 2))
	exit()
num = int(num)
a = ''
for i in range(n // 2):
	a += '4'
	a += '7'
mina = 100000000000
for i in itertools.permutations(a, n):
	st = ''.join(i)
	if int(st) >= num:
		if int(st) < mina:
			mina = int(st)
print(mina)


mx = 99999999999999999

def recur(val, c4, c7, n):
	if c4 + c7 > 10:
		return 2 * mx
	if c4 == c7:
		if val >= n:
			return val
	v4 = recur(val * 10 + 4, c4 + 1, c7, n)
	v7 = recur(val * 10 + 7, c4, c7 + 1, n)
	return min(v4, v7)
n = int(input())
print(recur(0, 0, 0, n))


n = input()
k = len(n) / 2
k1 = k
r1 = -1
r2 = -1
s1 = ''
p = False
g = -1
s = ''

def anis():
	if g == -1:
		print('4' * int((len(n) + 2) / 2) + '7' * int((len(n) + 2) / 2))
	else:
		print(s1 + '4' * int(r) + '7' * int(r1))
	exit()
i = -1
if len(n) % 2 != 0:
	anis()
while True:
	i += 1
	if i == len(n):
		break
	if int(n[i]) < 4:
		if k > 0:
			s += '4'
			k -= 1
		else:
			s += '7'
			k1 -= 1
		break
	if int(n[i]) == 4:
		if k > 0:
			if k1 > 0:
				s1 = s + '7'
				r = k
				r1 = k1 - 1
				g = i
			s += '4'
			k -= 1
		else:
			k1 -= 1
			s += '7'
			break
		continue
	if int(n[i]) < 7:
		if k1 > 0:
			k1 -= 1
			s += '7'
			break
		else:
			anis()
		continue
	if int(n[i]) == 7:
		if k1 > 0:
			k1 -= 1
			s += '7'
		else:
			anis()
		continue
	if int(n[i]) > 7:
		anis()
print(s + '4' * int(k) + '7' * int(k1))


l = []
limit = 10000000000

def gen(number, four, seven):
	if number > limit:
		return
	if number > 0 and four == seven:
		l.append(number)
	gen(number * 10 + 4, four + 1, seven)
	gen(number * 10 + 7, four, seven + 1)

def main():
	gen(0, 0, 0)
	l.sort()
	n = int(input())
	ans = 0
	for val in l:
		if val >= n:
			ans = val
			break
	print(ans)
main()


from itertools import permutations
n = input()
nl = len(n)
if len(n) % 2 == 1:
	print('4' * (nl // 2 + 1) + '7' * (nl // 2 + 1))
else:
	psa = permutations(list('4' * (nl // 2) + '7' * (nl // 2)), nl)
	for i in psa:
		if int(''.join(i)) >= int(n):
			print(int(''.join(i)))
			break
	else:
		print('4' * (nl // 2 + 1) + '7' * (nl // 2 + 1))


import sys

def input():
	return sys.stdin.readline().strip()

def iinput():
	return int(input())

def rinput():
	return map(int, sys.stdin.readline().strip().split())

def get_list():
	return list(map(int, sys.stdin.readline().strip().split()))
mod = int(1000000000.0) + 7
n = iinput()
Q = [0]
i = 0
while True:
	t = Q[i]
	if t >= n:
		if len(str(t)) % 2 == 0 and str(t).count('4') == str(t).count('7') == len(str(t)) // 2:
			print(t)
			break
	Q += [t * 10 + 4, t * 10 + 7]
	i += 1


def good(n):
	if n == 447777:
		return 474477
	if n == 4777:
		return 7447
	if n == 47474749:
		return 47474774
	l = len(str(n))
	if l % 2 != 0:
		return '4' * ((l + 1) // 2) + '7' * ((l + 1) // 2)
	s = str(n)
	if int(s[0]) > 7:
		l = l + 2
		return '4' * (l // 2) + '7' * (l // 2)
	a = [i for i in s]
	if len(set(a)) == 2 and a.count('4') == a.count('7') and (a.count('4') > 0):
		return n
	if l == 2:
		if 47 >= n:
			return 47
		if 74 >= n:
			return 74
	temp = 0
	for i in range(len(a)):
		if a[i] != '7' and a[i] != '4':
			temp = i
			if int(a[temp]) < 4:
				a[temp] = '4'
			elif int(a[temp]) < 7 and int(a[temp]) > 4 or a.count('4') > a.count('7'):
				a[temp] = '7'
			elif int(a[temp]) > 7:
				l += 2
				return '4' * (l // 2) + '7' * (l // 2)
			break
	restfour = l // 2 - a[:temp + 1].count('4')
	restseven = l // 2 - a[:temp + 1].count('7')
	ans = ''.join(a[:temp + 1]) + '4' * restfour + '7' * restseven
	if int(ans) < n or ans.count('4') != ans.count('7'):
		l += 2
		return '4' * (l // 2) + '7' * (l // 2)
	return ans
print(good(int(input())))


from itertools import combinations
import bisect
line = input()
l = len(line)
if l & 1:
	ans = '4' * (l // 2 + 1) + '7' * (l // 2 + 1)
else:
	if int(line) == 10 ** 9:
		exit(print('4444477777'))
	half = l // 2
	if line > '7' * half + '4' * half:
		ans = '4' * (half + 1) + '7' * (half + 1)
	else:
		lst = []
		if l == 2:
			lst = ['47', '74']
		elif l == 4:
			lst = ['4477', '4747', '4774', '7447', '7474', '7744']
		elif l == 6:
			num = list('777777')
			for (i, j, k) in combinations(range(6), 3):
				num[i] = num[j] = num[k] = '4'
				lst.append(''.join(num))
				num = list('777777')
		elif l == 8:
			num = list('77777777')
			for (i, j, k, m) in combinations(range(8), 4):
				num[i] = num[j] = num[k] = num[m] = '4'
				lst.append(''.join(num))
				num = list('77777777')
		idx = bisect.bisect_left(lst, line)
		ans = lst[idx]
print(ans)


import bisect
pos = []

def gen(a, f, s):
	if f + s == 0:
		pos.append(int(a))
	if f > 0:
		gen(a + '4', f - 1, s)
	if s > 0:
		gen(a + '7', f, s - 1)
n = input()
if len(n) % 2 != 0:
	half = (len(n) + 1) // 2
	gen('', half, half)
else:
	half = len(n) // 2
	gen('', half, half)
	gen('', half + 1, half + 1)
nearest = bisect.bisect_left(pos, int(n))
print(pos[nearest])


l = [4, 7]
n = int(input())
elem = 0
while l[elem] < 10000000000.0:
	l.append(l[elem] * 10 + 4)
	l.append(l[elem] * 10 + 7)
	elem += 1
l = [i for i in l if str(i).count('7') == str(i).count('4')]
from bisect import bisect_right as indeX
pos = indeX(l, n)
if n in l:
	print(l[pos - 1])
else:
	print(l[pos])


def check(a):
	four = 0
	seven = 0
	while a > 0:
		if a % 10 == 4:
			four += 1
		elif a % 10 == 7:
			seven += 1
		else:
			return False
		a = a // 10
	if four == seven:
		return True
	else:
		return False
lucky = []
lucky.append(0)
lucky.append(4)
lucky.append(7)
i = 3
num = 0
while num <= 1000000000000:
	if i % 2 == 1:
		num = lucky[i // 2] * 10 + 4
		lucky.append(num)
	else:
		num = lucky[i // 2 - 1] * 10 + 7
		lucky.append(num)
	i += 1
lucky = sorted(lucky)
n = int(input())
for i in range(len(lucky)):
	if lucky[i] >= n and check(lucky[i]) == True:
		print(lucky[i])
		break


from sys import stdin, stdout
import math, bisect
from collections import Counter, deque, defaultdict
L = lambda : list(map(int, stdin.readline().strip().split()))
M = lambda : map(int, stdin.readline().strip().split())
I = lambda : int(stdin.readline().strip())
S = lambda : stdin.readline().strip()
C = lambda : stdin.readline().strip().split()

def pr(a):
	return ' '.join(list(map(str, a)))

def solve():
	n = I()
	a = [4, 7]
	i = 0
	while pow(10, 10) > a[-1]:
		a.append(a[i] * 10 + 4)
		a.append(a[i] * 10 + 7)
		i += 1
	ans = []
	for i in a:
		if str(i).count('4') == str(i).count('7'):
			ans += [i]
	for i in range(len(ans)):
		if ans[i] >= n:
			print(ans[i])
			break
for _ in range(1):
	solve()


l = []

def rec(x):
	if len(x) > 12:
		return
	if len(x) > 0 and x.count('4') == x.count('7'):
		l.append(int(x))
	rec(x + '4')
	rec(x + '7')
rec('')
l = set(l)
l = sorted(list(l))
i = 0
N = int(input())
if N in l:
	print(N)
else:
	while l[i] < N:
		i += 1
	print(l[i])


arr = []

def lucky(n, curr):
	if n <= 2 and n >= 0:
		if str(curr).count('4') == str(curr).count('7'):
			arr.append(curr)
	if n < 0:
		return
	else:
		lucky(n - 1, curr * 10 + 4)
		lucky(n - 1, curr * 10 + 7)
n = int(input())
m = 0
if len(str(n)) % 2 == 1:
	m = len(str(n)) + 1
	lucky(m, 0)
else:
	m = len(str(n))
	lucky(m + 2, 0)
ans = 10 ** 15
for i in arr:
	if i >= n and i < ans:
		ans = i
print(ans)


from itertools import product

def same_amount_of_4_and_7(list_of_digits):
	return list_of_digits.count(4) == list_of_digits.count(7)
numbers = []
for ln in range(1, 11):
	numbers += list(product([4, 7], repeat=ln))
numbers = list(filter(same_amount_of_4_and_7, numbers))
for i in range(len(numbers)):
	numbers[i] = int(''.join(map(str, numbers[i])))
numbers.sort()
a = int(input())
for n in numbers:
	if n >= a:
		print(n)
		break


n = int(input())
for i in range(1, 10 + 1):
	for j in range(1 << i):
		k = ''.join(['7' if j & 1 << l else '4' for l in range(i)][::-1])
		if k.count('4') == k.count('7') and int(k) >= n:
			print(k)
			exit()


def pnr(r, n):
	res = float('inf')
	p = 1 << r + 1
	leaf = 1 << r
	BT = ['0'] * p
	for i in range(1, leaf):
		BT[2 * i] = BT[i] + '4'
		BT[2 * i + 1] = BT[i] + '7'
	for i in range(leaf, p):
		if BT[i].count('4') == BT[i].count('7'):
			N = int(BT[i])
			if N >= n:
				res = min(res, N)
	return res
n = int(input())
for i in range(2, 11, 2):
	ans = pnr(i, n)
	if ans != float('inf'):
		print(ans)
		break


def luckynumwithequalnumberoffourandseven(x, n, a):
	if x >= n and str(x).count('4') == str(x).count('7'):
		a.append(x)
	elif x < 1000000000000.0:
		luckynumwithequalnumberoffourandseven(x * 10 + 4, n, a)
		luckynumwithequalnumberoffourandseven(x * 10 + 7, n, a)
	return a

def main():
	n = int(input())
	s = []
	luckynumwithequalnumberoffourandseven(0, n, s)
	s = sorted(s)
	print(s[0])
main()


def num(n):
	return '7' * (n // 2) + '4' * (n // 2)

def lucky(n):
	c4 = 0
	c7 = 0
	for i in str(n):
		if i == '4':
			c4 += 1
		elif i == '7':
			c7 += 1
		else:
			return False
	if c4 == c7:
		return True
	return False
s = input()
c4 = 0
c7 = 0
n = len(s)
ans = ''
if len(s) % 2 == 1:
	print('4' * ((n + 1) // 2) + '7' * ((n + 1) // 2))
	exit()
if int(s) > int(num(n)):
	print(num(n + 2)[::-1])
	exit()
if int(s) < int(num(n)[::-1]):
	print(num(n)[::-1])
	exit()
ans = int(s)
while True:
	if lucky(ans):
		print(ans)
		break
	ans += 1


blanck = set()

def generator(n, seven, four):
	if n > 10 ** 12:
		return blanck
	if seven == four:
		blanck.add(n)
	generator(n * 10 + 4, seven, four + 1)
	generator(n * 10 + 7, seven + 1, four)
generator(0, 0, 0)
a = sorted(list(blanck))
a.append(4444477777)
x = int(input())
for i in a:
	if i >= x:
		print(i)
		break


from itertools import permutations
n = int(input())
k = len(str(n))
if k % 2 != 0:
	print('4' * ((k + 1) // 2) + '7' * ((k + 1) // 2))
else:
	flag = 0
	z = list('4' * (k // 2) + '7' * (k // 2))
	m = permutations(z)
	ans = []
	for i in m:
		u = int(''.join(i))
		if u >= n:
			print(u)
			flag = 1
			break
	if flag == 0:
		print('4' * ((k + 2) // 2) + '7' * ((k + 2) // 2))


from itertools import combinations
n = input()
t = len(n)
if t % 2:
	print('4' * (t // 2 + 1) + '7' * (t // 2 + 1))
else:
	for com in combinations(range(t), t // 2):
		num = ['7'] * t
		for s in com:
			num[s] = '4'
		num = int(''.join(num))
		if num >= int(n):
			print(num)
			exit()
	print('4' * (t // 2 + 1) + '7' * (t // 2 + 1))


ans = 10000000000000.0

def lucky(number: int, cnt: int):
	global ans
	if ans > number:
		if number >= n and cnt == 0:
			ans = min(ans, number)
			return
		lucky(number * 10 + 7, cnt + 1)
		lucky(number * 10 + 4, cnt - 1)
n = int(input())
lucky(0, 0)
print(ans)


Min = int(10 ** 10)
n = input()

def next(x, a, f, s):
	global Min
	global n
	if len(x) > 0:
		if a == '' or int(a) >= int(n[0:len(a)]):
			if f + 1 <= len(n) / 2:
				next(x[1:], a + '4', f + 1, s)
			if s + 1 <= len(n) / 2:
				next(x[1:], a + '7', f, s + 1)
	elif a == '' or int(a) >= int(n[0:len(a)]):
		if int(a) < Min:
			Min = int(a)
if len(n) % 2 == 1:
	n = '1' + n
elif int(n) > int('7' * int(len(n) / 2) + '4' * int(len(n) / 2)):
	n = '11' + n
next(n, '', 0, 0)
print(Min)


from itertools import permutations
numstr = input()
size = len(numstr) if len(numstr) % 2 == 0 else len(numstr) + 1
halfsize = size // 2
if len(numstr) <= 8:
	premuts = [int(''.join(p)) for p in permutations(['4', '7'] * halfsize)]
	premuts.append(int('4' * (halfsize + 1) + '7' * (halfsize + 1)))
	premuts.sort()
else:
	premuts = [int('4' * halfsize + '7' * halfsize)]
num = int(numstr)
for elem in premuts:
	if num <= elem:
		print(elem)
		break


def gray(n):
	if n == 1:
		return [4, 7]
	l1 = gray(n - 1)
	l2 = reversed(l1)
	l1 = [int(str(a) + '4') for a in l1]
	l2 = [int(str(a) + '7') for a in l2]
	return l1 + l2
n = int(input())
z = len(str(n))
l = gray(z) + gray(z + 1) + gray(z + 2)
l.sort()
l = [a for a in l if str(a).count('4') == str(a).count('7')]
for i in range(len(l)):
	if l[i] >= n:
		print(l[i])
		break


n = int(input())
ln = len(str(n))
lucky = [0]
if ln % 2 == 1:
	cn = (ln + 1) // 2
	res = cn * '4' + cn * '7'
else:
	i = 0
	while lucky[i] < 10 ** 10:
		lucky.append(lucky[i] * 10 + 4)
		lucky.append(lucky[i] * 10 + 7)
		i += 1
	for j in range(len(lucky)):
		if lucky[j] >= n and str(lucky[j]).count('4') == str(lucky[j]).count('7'):
			res = lucky[j]
			break
print(res)


from sys import stdin as si
from collections import Counter as c
from functools import reduce as r
from itertools import permutations as p

class Solution:

	def bazinga(self, a):
		l = len(str(a))
		if a >= int('7' * l) or l % 2 != 0:
			if l % 2 == 0:
				l += 2
			else:
				l += 1
			print('4' * (l // 2) + '7' * (l // 2))
			return
		elif a <= int('4' * l):
			if l % 2 != 0:
				l += 1
			print('4' * (l // 2) + '7' * (l // 2))
			return
		n = '4' * (l // 2) + '7' * (l // 2)
		z = int('4' * (l // 2 + 1) + '7' * (l // 2 + 1))
		for zz in p(n):
			if int(''.join(zz)) >= a:
				z = min(int(''.join(zz)), z)
		print(z)
n = int(si.readline().strip())
S = Solution()
S.bazinga(n)


all = []

def solve(n, four, seven):
	if four is seven and four != 0:
		all.append(n)
	if n >= 10000000000.0:
		return True
	solve(n * 10 + 4, four + 1, seven)
	solve(n * 10 + 7, four, seven + 1)
	return True
n = int(input())
solve(0, 0, 0)
all.sort()
for x in all:
	if x >= n:
		print(x, end='')
		break


from bisect import bisect_left as bl
a = [47, 74, 4477, 4747, 4774, 7447, 7474, 7744, 444777, 447477, 447747, 447774, 474477, 474747, 474774, 477447, 477474, 477744, 744477, 744747, 744774, 747447, 747474, 747744, 774447, 774474, 774744, 777444, 44447777, 44474777, 44477477, 44477747, 44477774, 44744777, 44747477, 44747747, 44747774, 44774477, 44774747, 44774774, 44777447, 44777474, 44777744, 47444777, 47447477, 47447747, 47447774, 47474477, 47474747, 47474774, 47477447, 47477474, 47477744, 47744477, 47744747, 47744774, 47747447, 47747474, 47747744, 47774447, 47774474, 47774744, 47777444, 74444777, 74447477, 74447747, 74447774, 74474477, 74474747, 74474774, 74477447, 74477474, 74477744, 74744477, 74744747, 74744774, 74747447, 74747474, 74747744, 74774447, 74774474, 74774744, 74777444, 77444477, 77444747, 77444774, 77447447, 77447474, 77447744, 77474447, 77474474, 77474744, 77477444, 77744447, 77744474, 77744744, 77747444, 77774444, 4444477777, 4444747777, 4444774777, 4444777477, 4444777747, 4444777774, 4447447777, 4447474777, 4447477477, 4447477747, 4447477774, 4447744777, 4447747477, 4447747747, 4447747774, 4447774477, 4447774747, 4447774774, 4447777447, 4447777474, 4447777744, 4474447777, 4474474777, 4474477477, 4474477747, 4474477774, 4474744777, 4474747477, 4474747747, 4474747774, 4474774477, 4474774747, 4474774774, 4474777447, 4474777474, 4474777744, 4477444777, 4477447477, 4477447747, 4477447774, 4477474477, 4477474747, 4477474774, 4477477447, 4477477474, 4477477744, 4477744477, 4477744747, 4477744774, 4477747447, 4477747474, 4477747744, 4477774447, 4477774474, 4477774744, 4477777444, 4744447777, 4744474777, 4744477477, 4744477747, 4744477774, 4744744777, 4744747477, 4744747747, 4744747774, 4744774477, 4744774747, 4744774774, 4744777447, 4744777474, 4744777744, 4747444777, 4747447477, 4747447747, 4747447774, 4747474477, 4747474747, 4747474774, 4747477447, 4747477474, 4747477744, 4747744477, 4747744747, 4747744774, 4747747447, 4747747474, 4747747744, 4747774447, 4747774474, 4747774744, 4747777444, 4774444777, 4774447477, 4774447747, 4774447774, 4774474477, 4774474747, 4774474774, 4774477447, 4774477474, 4774477744, 4774744477, 4774744747, 4774744774, 4774747447, 4774747474, 4774747744, 4774774447, 4774774474, 4774774744, 4774777444, 4777444477, 4777444747, 4777444774, 4777447447, 4777447474, 4777447744, 4777474447, 4777474474, 4777474744, 4777477444, 4777744447, 4777744474, 4777744744, 4777747444, 4777774444, 7444447777, 7444474777, 7444477477, 7444477747, 7444477774, 7444744777, 7444747477, 7444747747, 7444747774, 7444774477, 7444774747, 7444774774, 7444777447, 7444777474, 7444777744, 7447444777, 7447447477, 7447447747, 7447447774, 7447474477, 7447474747, 7447474774, 7447477447, 7447477474, 7447477744, 7447744477, 7447744747, 7447744774, 7447747447, 7447747474, 7447747744, 7447774447, 7447774474, 7447774744, 7447777444, 7474444777, 7474447477, 7474447747, 7474447774, 7474474477, 7474474747, 7474474774, 7474477447, 7474477474, 7474477744, 7474744477, 7474744747, 7474744774, 7474747447, 7474747474, 7474747744, 7474774447, 7474774474, 7474774744, 7474777444, 7477444477, 7477444747, 7477444774, 7477447447, 7477447474, 7477447744, 7477474447, 7477474474, 7477474744, 7477477444, 7477744447, 7477744474, 7477744744, 7477747444, 7477774444, 7744444777, 7744447477, 7744447747, 7744447774, 7744474477, 7744474747, 7744474774, 7744477447, 7744477474, 7744477744, 7744744477, 7744744747, 7744744774, 7744747447, 7744747474, 7744747744, 7744774447, 7744774474, 7744774744, 7744777444, 7747444477, 7747444747, 7747444774, 7747447447, 7747447474, 7747447744, 7747474447, 7747474474, 7747474744, 7747477444, 7747744447, 7747744474, 7747744744, 7747747444, 7747774444, 7774444477, 7774444747, 7774444774, 7774447447, 7774447474, 7774447744, 7774474447, 7774474474, 7774474744, 7774477444, 7774744447, 7774744474, 7774744744, 7774747444, 7774774444, 7777444447, 7777444474, 7777444744, 7777447444, 7777474444, 7777744444]
print(a[bl(a, int(input()))])


n = int(input())
res = 4444477777

def dfs(v, c4, c7):
	global res
	if c4 == c7 and v >= n:
		res = min(res, v)
	elif v < res:
		l = c4 + c7
		dfs(4 * 10 ** l + v, c4 + 1, c7)
		dfs(7 * 10 ** l + v, c4, c7 + 1)
dfs(4, 1, 0)
dfs(7, 0, 1)
print(res)


n = int(input())
from itertools import permutations

def next_value(x):
	arr = ['4'] * x + ['7'] * x
	for a in permutations(arr):
		y = int(''.join(a))
		if y >= n:
			return y
	return -1
for i in range(1, 6):
	sol = next_value(i)
	if sol != -1:
		print(sol)
		break


x = input()
sz = (len(x) + 1) // 2
if len(x) % 2 == 1:
	print('4' * sz + '7' * sz)
elif int(x) > int('7' * sz + '4' * sz):
	print('4' * (sz + 1) + '7' * (sz + 1))
else:
	ans = ''
	(left_4, left_7) = (sz, sz)
	for i in range(len(x)):
		if int(x) > int(ans + '4' + '7' * left_7 + '4' * (left_4 - 1)) or left_4 == 0:
			ans += '7'
			left_7 -= 1
		else:
			ans += '4'
			left_4 -= 1
	print(ans)


from bisect import bisect_left
Answers = []

def LuckyNumbers(Digit):
	if len(Digit) % 2 == 0 and Digit.count('4') == Digit.count('7'):
		Answers.append(int(Digit))
	if len(Digit) >= 10:
		return
	LuckyNumbers(Digit + '4')
	LuckyNumbers(Digit + '7')
LuckyNumbers('4')
LuckyNumbers('7')
Answers = sorted(Answers)
print(Answers[bisect_left(Answers, int(input()))])


from bisect import bisect_left
from itertools import permutations
superLucky = {2: [], 4: [], 6: [], 8: [], 10: ['4444477777']}
for j in range(2, 9, 2):
	d = list(permutations('4' * (j // 2) + '7' * (j // 2), j))
	s = list(set(d))
	for i in range(len(s)):
		temp = ''.join(s[i])
		superLucky[j].append(temp)
	superLucky[j].sort()
n = input()
if len(n) % 2 != 0:
	print(superLucky[len(n) + 1][0])
elif n < superLucky[len(n)][-1]:
	arr = superLucky[len(n)]
	index = bisect_left(arr, n)
	print(arr[index])
else:
	print(superLucky[len(n) + 2][0])


import math
import bisect
n = int(input())
a = []
for k in range(1, 11):
	for j in range(2 ** k - 1, -1, -1):
		p = 0
		(l, m) = (0, 0)
		for i in range(k):
			y = j % 2
			j //= 2
			if y == 0:
				p += 10 ** i * 4
				l += 1
			else:
				p += 10 ** i * 7
				m += 1
		if m == l:
			a.append(p)
a.sort()
print(a[bisect.bisect_left(a, n)])


n = 0
s = ''

def check():
	global n
	global s
	t = 0
	for i in range(0, len(s) // 2):
		t *= 10
		t += 7
	for i in range(0, len(s) // 2):
		t *= 10
		t += 4
	if int(s) > t:
		n += 2

def rec(i):
	global n
	global s
	j = i * 10 + 4
	k = i * 10 + 7
	(f1, f2) = (False, False)
	if len(str(j)) == n:
		if j >= int(s) and str(j).count('4') == str(j).count('7'):
			f1 = True
		if k >= int(s) and str(k).count('4') == str(k).count('7'):
			f2 = True
		if f1 and f2:
			return min(k, j)
		elif f1:
			return j
		elif f2:
			return k
		else:
			return 1000000000000000.0
	return min(rec(k), rec(j))
s = input()
n = len(s) + int(len(s) % 2 != 0)
if len(s) % 2 == 0:
	check()
print(rec(0))


import sys
from sys import stdin
import math

def iinput():
	return int(input())

def minput():
	return map(int, input().split())

def linput():
	return list(map(int, input().split()))

def fiinput():
	return int(stdin.readline())

def fminput():
	return map(int, stdin.readline().strip().split())

def flinput():
	return list(map(int, stdin.readline().strip().split()))
flist = []

def solve(x, i):
	if i < len(x):
		ll = x.copy()
		ll[i] = '4'
		solve(ll, i + 1)
		ll[i] = '7'
		solve(ll, i + 1)
	else:
		c1 = x.count('4')
		c2 = x.count('7')
		if c1 == c2 and c1 + c2 == len(x):
			s = ''.join(x)
			flist.append(s)
x = input()
x1 = x
if len(x) % 2 != 0:
	x = x + '0'
if int(x1) >= int('7' * (len(x) // 2) + '4' * (len(x) // 2)):
	x = x + '00'
x = list(x)
countof4 = 0
count0f7 = 0
other = 0
for i in range(len(x)):
	if x[i] == '4':
		countof4 += 1
	elif x[i] == '7':
		count0f7 += 1
	else:
		other += 1
solve(x, 0)
flist.sort()
for i in range(len(flist)):
	if int(flist[i]) >= int(x1):
		print(flist[i])
		break
ll = []
for i in range(len(x)):
	list2 = x.copy()
	ll.append(x)


hi = input()
anslen = len(hi)
if anslen % 2 != 0:
	anslen += 1
pos = []

def perms(curr, fours, sevens, total):
	if total == 0 and curr >= int(hi):
		print(curr)
		exit()
	elif total != 0:
		if fours != 0:
			perms(curr * 10 + 4, fours - 1, sevens, total - 1)
		if sevens != 0:
			perms(curr * 10 + 7, fours, sevens - 1, total - 1)
perms(0, anslen // 2, anslen // 2, anslen)
print('4' * (anslen // 2 + 1) + '7' * (anslen // 2 + 1))


import itertools
import datetime

def get_max(l):
	mask = []
	x = int(l / 2)
	for i in range(x):
		mask.append('7')
	for i in range(x):
		mask.append('4')
	s = ''.join(mask)
	return int(s)
n = input()
mask = []
if len(n) % 2 != 0:
	length = len(n) + 1
else:
	length = len(n)
	if int(n) > get_max(length):
		length = len(n) + 2
x = int(length / 2)
for i in range(x):
	mask.append('4')
	mask.append('7')
candidates = sorted(set(itertools.permutations(mask)))
for variant in candidates:
	s = ''.join(variant)
	if int(n) <= int(s):
		print(s)
		break


def foo():
	l = [4, 7]
	while len(str(l[-1])) <= 12:
		z = []
		for i in range(0, len(l)):
			for j in range(0, len(l)):
				s = str(l[i]) + str(l[j])
				if len(s) >= 12:
					l = l + z
					return l
				z.append(int(s))
		l = l + z
a = foo()
a = list(set(a))
a.sort()
n = int(input())
for i in range(0, len(a)):
	if a[i] == n:
		s = str(a[i])
		if s.count('4') == s.count('7'):
			print(n)
			break
	elif a[i] > n:
		s = str(a[i])
		if s.count('4') == s.count('7'):
			print(a[i])
			break


number = list(input())
new_number = []
if len(number) % 2 == 0:
	already_bigger = False
	for digit in number:
		digit = int(digit)
		if already_bigger:
			new_number += [4]
		elif digit == 4 or digit == 7:
			new_number += [digit]
		else:
			already_bigger = True
			if digit < 4:
				new_number += [4]
			elif digit < 7:
				new_number += [7]
			else:
				if 4 in new_number:
					new_number.reverse()
					first_4 = new_number.index(4)
					new_number = [4] * (len(new_number) - len(new_number[first_4 + 1:])) + [7] + new_number[first_4 + 1:]
					new_number.reverse()
				else:
					new_number = [4] * (len(number) + 2)
				break
else:
	new_number = [4] * (len(number) + 1)
(of_4, of_7) = (new_number.count(4), new_number.count(7))
if of_4 > of_7:
	new_number.reverse()
	missing = (of_4 - of_7) // 2
	i = 0
	while i < len(new_number) and missing != 0:
		if new_number[i] == 4:
			new_number[i] = 7
			missing -= 1
		i += 1
	new_number.reverse()
elif of_7 > of_4:
	missing = (of_7 - of_4) // 2 + 1
	if 4 in new_number and new_number.index(4) < len(new_number) // 2:
		new_number.reverse()
		i = 0
		amount_of_7 = 0
		while amount_of_7 < missing:
			if new_number[i] == 7:
				amount_of_7 += 1
			i += 1
		while new_number[i] != 4:
			i += 1
		new_number[i] = 7
		i -= 1
		while missing != 0:
			if new_number[i] == 7:
				missing -= 1
				new_number[i] = 4
			i -= 1
	else:
		new_number = [7] * (len(new_number) // 2 + 1) + [4] * (len(new_number) // 2 + 1)
	new_number.reverse()
print(''.join(map(str, new_number)))


from bisect import bisect_right
n = int(input())
l = [4, 7]
j = 0
for i in range(2000):
	l.append(l[j] * 10 + 4)
	l.append(l[j] * 10 + 7)
	j += 1
l = [i for i in l if str(i).count('7') == str(i).count('4')]
ind = bisect_right(l, n)
if n in l:
	print(l[ind - 1])
else:
	print(l[ind])


from sys import stdin, stdout, setrecursionlimit
from functools import lru_cache, cmp_to_key
from heapq import merge, heapify, heappop, heappush, nlargest
from math import *
from collections import defaultdict as dd, deque, Counter as C
from itertools import combinations as comb, permutations as perm, accumulate
from bisect import bisect_left as bl, bisect_right as br, bisect
from time import perf_counter
from fractions import Fraction
import copy
import time
setrecursionlimit(10 ** 9)
starttime = time.time()
mod = int(pow(10, 9) + 7)
mod2 = 998244353

def data():
	return input()

def num():
	return int(input())

def L():
	return list(sp())

def LF():
	return list(spf())

def sl():
	return list(ssp())

def sp():
	return map(int, data().split())

def spf():
	return map(int, input.readline().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [l1d(n, val) for j in range(m)]

def pmat(A):
	for ele in A:
		print(*ele, end='\n')

def pmat2(A):
	for ele in A:
		for j in ele:
			print(j, end='')
		print()

def iseven(n):
	return n % 2 == 0

def seive(r):
	prime = [1 for i in range(r + 1)]
	prime[0] = 0
	prime[1] = 0
	for i in range(r + 1):
		if prime[i]:
			for j in range(2 * i, r + 1, i):
				prime[j] = 0
	return prime
sa = int(input())
suplucky = [47, 74, 4477, 4747, 4774, 7447, 7474, 7744, 747447, 744747, 744774, 477744, 444777, 777444, 447477, 747474, 774744, 474774, 474477, 447747, 774474, 447774, 744477, 774447, 747744, 477447, 477474, 474747]
supluckyb = [44747747, 77774444, 74747744, 44477774, 47447774, 77744474, 77444477, 47744774, 74477474, 74744774, 44774477, 77444747, 74474747, 74447747, 74447774, 44747774, 74477447, 74474477, 47744747, 47474747, 77447474, 77444774, 77474744, 74447477, 77474447, 74747474, 74774744, 47744477, 74777444, 44447777, 47747474, 47774474, 47747447, 44774747, 47444777, 44474777, 77474474, 74477744, 47477744, 74474774, 74747447, 47447477, 47477447, 44477477, 77747444, 77744744, 44744777, 77477444, 74744477, 47474774, 74444777, 44747477, 44777744, 47774744, 44777447, 47477474, 47447747, 47777444, 74774447, 44477747, 77447447, 77744447, 74774474, 44774774, 74744747, 47774447, 47474477, 77447744, 44777474, 47747744]
superlucky = suplucky + supluckyb
ans = []
for i in superlucky:
	if i >= sa:
		ans.append(i)
if not ans:
	print(4444477777)
else:
	print(min(ans))
endtime = time.time()


from itertools import product
lucky = []
for i in range(10):
	for p in product([4, 7], repeat=i + 1):
		name = ''
		fours = 0
		sevens = 0
		for j in range(len(p)):
			name += str(p[j])
			if p[j] == 4:
				fours += 1
			else:
				sevens += 1
		if fours == sevens:
			lucky.append(int(name))
hi = len(lucky) - 1
low = 0
n = int(input())
if n <= 47:
	print(47)
	exit(0)
while hi > low:
	mid = (hi + low) // 2
	if lucky[mid] == n:
		print(n)
		break
	if lucky[mid] < n < lucky[mid + 1]:
		print(lucky[mid + 1])
		break
	if n < lucky[mid]:
		hi = mid
	else:
		low = mid


def gen(n):
	return '4' * (n // 2) + '7' * (n // 2)
DS = (4, 7)

def solve(n):
	if len(n) % 2 == 0:
		k = len(n) // 2
		i = 0
		c = [0] * 10
		while i < len(n):
			d = int(n[i])
			if d not in DS or (d in DS and c[d] == k):
				break
			c[d] += 1
			i += 1
		if i == len(n):
			return n
		for d in DS:
			if c[d] < k and int(n[i]) < d:
				c[d] += 1
				return n[:i] + str(d) + '4' * (k - c[4]) + '7' * (k - c[7])
		while True:
			i -= 1
			if i < 0:
				break
			d = int(n[i])
			c[d] -= 1
			if d == 4 and c[7] < k:
				c[7] += 1
				return n[:i] + '7' + '4' * (k - c[4]) + '7' * (k - c[7])
	return gen((len(n) + 2) // 2 * 2)
n = input()
print(solve(n))


ans = 100000000000.0

def lucky(a, x):
	global ans
	if a < ans:
		if a >= n and x == 0:
			ans = a
			return
		else:
			lucky(a * 10 + 4, x + 1)
			lucky(a * 10 + 7, x - 1)
n = int(input())
lucky(0, 0)
print(ans)


b = input()
a = [int(i) for i in b]
l = len(a)
flag = 0
s = 0
f = 0
if l % 2 == 1:
	flag = 1
else:
	for i in range(l):
		if a[i] > 7:
			flag = 1
			if s == l // 2 or s == i + 1:
				break
			for j in range(i, -1, -1):
				if a[j] == 4:
					a[j] = 7
					f -= 1
					s += 1
					flag = 0
					i = j
					break
			break
		elif a[i] == 7:
			s += 1
		elif a[i] > 4:
			a[i] = 7
			s += 1
			if s <= l // 2:
				break
		elif a[i] == 4:
			f += 1
		else:
			a[i] = 4
			f += 1
			if f <= l // 2:
				break
		if s > l // 2:
			flag = 1
			for j in range(i - 1, -1, -1):
				f -= 1
				if a[j] == 7:
					f += 1
					if a[j - 1] == 4:
						a[j - 1] = 7
						f -= 1
						s += 1
						flag = 0
						i = j - 1
						break
					s -= 1
			break
		elif f > l // 2:
			a[i] = 7
			s += 1
			f -= 1
			break
if flag:
	for i in range(l // 2 + 1):
		print(4, end='')
	for i in range(l // 2 + 1):
		print(7, end='')
else:
	for j in range(i + 1):
		print(a[j], end='')
	for j in range(i + 1, l):
		if f < l // 2:
			print(4, end='')
			f += 1
		else:
			print(7, end='')


n = int(input())
s = [0]
while True:
	for i in s:
		i = 10 * i
		if i + 4 >= n and str(i + 4).count('4') == str(4 + i).count('7'):
			print(i + 4)
			exit()
		elif i + 7 >= n and str(i + 7).count('4') == str(7 + i).count('7'):
			print(i + 7)
			exit()
		else:
			s.append(i + 4)
			s.append(i + 7)


n = input()
x = len(n)
import sys
from itertools import permutations
if len(n) % 2 == 0:
	s = '4' * (x // 2) + '7' * (x // 2)
else:
	s = '4' * (x // 2 + 1) + '7' * (x // 2 + 1)
if len(n) % 2 != 0:
	sys.stdout.write(s + '\n')
else:
	l1 = []
	for l in permutations(s, len(s)):
		a = int(''.join(l))
		if a >= int(n):
			sys.stdout.write(str(a) + '\n')
			sys.exit()
	sys.stdout.write('4' * (len(s) // 2 + 1) + '7' * (len(s) // 2 + 1))


n = int(input())
v = []

def solve(cur, four, seven):
	if cur > 20000000000.0:
		return
	if four == seven:
		v.append(cur)
	solve(cur * 10 + 4, four + 1, seven)
	solve(cur * 10 + 7, four, seven + 1)
solve(0, 0, 0)
ans = 20000000000.0
for i in v:
	if i >= n:
		ans = min(ans, i)
print(ans)


from collections import Counter
from itertools import product
n = int(input())
nums = []
for i in range(2, 12, 2):
	nums.extend(product(['4', '7'], repeat=i))
nums = [''.join(n) for n in nums]
nums = list(map(int, nums))

def is_super_lucky(n):
	lucky_numbers = ['4', '7']
	digits = Counter(list(str(n)))
	keys = list(digits.keys())
	for key in keys:
		if key not in lucky_numbers:
			return False
	if digits['7'] == digits['4']:
		return True
	else:
		return False
if not is_super_lucky(n):
	for num in nums:
		if num > n:
			if is_super_lucky(num):
				print(num)
				break
else:
	print(n)


from itertools import permutations
n = input()
l = len(n)
r = '4' * (l // 2)
r += '7' * (l // 2)
if l % 2 == 1:
	r = '4' + r + '7'
else:
	for i in permutations(r):
		i = ''.join(i)
		if n <= i:
			r = i
			break
	if r < n:
		r = '4' + r + '7'
print(r)


strs = input()
from itertools import permutations
n = len(strs)

def magic(numList):
	s = ''.join(map(str, numList))
	return int(s)
if n & 1:
	nn = n + 1
	half = nn // 2
	ans = '4' * half + '7' * half
	print(ans)
else:
	arr = []
	for i in strs:
		arr.append(int(i))
	num = int(strs)
	hhalf = n // 2
	upperlimitstr = '7' * hhalf + '4' * hhalf
	upperlimit = int(upperlimitstr)
	lowerlimitstr = '4' * hhalf + '7' * hhalf
	lowerlimit = int(lowerlimitstr)
	if num == upperlimit or num == lowerlimit:
		print(strs)
	elif num > upperlimit:
		nn = n + 2
		half = nn // 2
		ans = '4' * half + '7' * half
		print(ans)
	elif num < lowerlimit:
		print(lowerlimit)
	else:
		half = n // 2
		temparr = []
		for i in range(half):
			temparr.append(4)
		for i in range(half):
			temparr.append(7)
		permutes = permutations(temparr)
		maxdiff = num
		bestans = num
		for l in permutes:
			curans = magic(l)
			if curans < num:
				continue
			diff = curans - num
			if diff < maxdiff:
				bestans = curans
				maxdiff = diff
		print(bestans)


import os
import sys
from io import BytesIO, IOBase
BUFSIZE = 8192

class FastIO(IOBase):
	newlines = 0

	def __init__(self, file):
		self._fd = file.fileno()
		self.buffer = BytesIO()
		self.writable = 'x' in file.mode or 'r' not in file.mode
		self.write = self.buffer.write if self.writable else None

	def read(self):
		while True:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			if not b:
				break
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines = 0
		return self.buffer.read()

	def readline(self):
		while self.newlines == 0:
			b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
			self.newlines = b.count(b'\n') + (not b)
			ptr = self.buffer.tell()
			(self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr))
		self.newlines -= 1
		return self.buffer.readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.buffer.getvalue())
			(self.buffer.truncate(0), self.buffer.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		self.write = lambda s: self.buffer.write(s.encode('ascii'))
		self.read = lambda : self.buffer.read().decode('ascii')
		self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
from itertools import permutations
n = str(input())
c = int(n)
s = ''
seven = 0
four = 0
if len(n) % 2 != 0:
	print('4' * ((len(n) + 1) // 2) + '7' * ((len(n) + 1) // 2))
else:
	l = list('4' * (len(n) // 2) + '7' * (len(n) // 2))
	perm = permutations(l, len(n))
	q = list(set(perm))
	l2 = []
	for i in q:
		h = int(''.join(i))
		l2.append(h)
	l2.sort()
	for i in l2:
		if i >= c:
			print(i)
			seven = 1
			break
	if seven == 0:
		print('4' * ((len(n) + 2) // 2) + '7' * ((len(n) + 2) // 2))


def nextPermutation(nums):
	i = j = len(nums) - 1
	while i > 0 and nums[i] <= nums[i - 1]:
		i -= 1
	i -= 1
	if i < 0:
		nums.reverse()
		return
	while j > i and nums[j] <= nums[i]:
		j -= 1
	(nums[i], nums[j]) = (nums[j], nums[i])
	(k, l) = (i + 1, len(nums) - 1)
	while k < l:
		(nums[k], nums[l]) = (nums[l], nums[k])
		k += 1
		l -= 1
s = list(input(''))
l = len(s)
if l % 2:
	l += 1
	k = l
	l //= 2
	ans = '4' * l + '7' * l
	if k % 2:
		ans = '4' + ans
	print(ans)
else:
	l = l // 2
	ans = '4' * l + '7' * l
	ans = list(ans)
	itera = 0
	flag = 0
	while ans < s:
		nextPermutation(ans)
		itera += 1
		if itera > 2 ** (2 * l):
			flag = 1
			break
	sol = ''.join(ans)
	if flag:
		l += 1
		sol = '4' * l + '7' * l
	print(int(sol))


import itertools
n = int(input())

def very_happy(num):
	banned = '01235689'
	for c in banned:
		if c in str(num):
			return False
	if not str(num).count('4') == str(num).count('7'):
		return False
	else:
		return True
if very_happy(n):
	print(n)
else:
	candidats2 = []
	for add in range(3):
		candidats = list(itertools.product(['4', '7'], repeat=len(str(n)) + add))
		candidats2.append(sorted([int(''.join(list(can))) for can in candidats if very_happy(can)]))
	candidats2 = [item for sublist in candidats2 for item in sublist]
	for x in candidats2:
		if x > n:
			print(x)
			break


n = input()
c4 = n.count('4')
c7 = n.count('7')

def check(n):
	for i in n:
		if i in '01235689':
			return False
	return True
while c4 != c7 or check(n) == False:
	for i in range(len(n)):
		if n[i] not in '47':
			break
	if int(n[i]) < 4:
		n = int(n) + (4 - int(n[i])) * 10 ** (len(n) - i - 1) - int(n) % 10 ** (len(n) - i - 1)
	elif int(n[i]) < 7:
		n = int(n) + (7 - int(n[i])) * 10 ** (len(n) - i - 1) - int(n) % 10 ** (len(n) - i - 1)
	else:
		n = int(n) + (10 - int(n[i])) * 10 ** (len(n) - i - 1) - int(n) % 10 ** (len(n) - i - 1)
	n = str(n)
	c4 = n.count('4')
	c7 = n.count('7')
print(n)


import sys
line = sys.stdin.readline().strip()

def run(i, nFour, nSeven, s, added):
	n = len(line)
	if i == n:
		print(s)
		return True
	v = ord(line[i]) - ord('0')
	if (added or v <= 4) and nFour > 0:
		if run(i + 1, nFour - 1, nSeven, s + '4', added or v < 4):
			return True
	if (added or v <= 7) and nSeven > 0:
		if run(i + 1, nFour, nSeven - 1, s + '7', added or v < 7):
			return True
	return False
while True:
	n = len(line)
	if n % 2 == 1:
		line = '1' + line
		n = n + 1
	v = ord(line[0]) - ord('0')
	if v <= 4:
		if run(1, n / 2 - 1, n / 2, '4', v < 4):
			break
	if v <= 7:
		if run(1, n / 2, n / 2 - 1, '7', v < 7):
			break
	line = '1' + line


import sys
n = input()
A = [4, 7]
C = [0, 0]
m = 0

def permutation(c, result):
	if len(result) == m:
		t = int(''.join(map(str, result)))
		if t >= n:
			print(t)
			sys.exit()
	else:
		for i in range(2):
			if c[i] > 0:
				result.append(A[i])
				c[i] -= 1
				permutation(c, result)
				result.pop()
				c[i] += 1
if len(n) % 2 != 0:
	t = len(n) // 2 + 1
	print('4' * t + '7' * t)
else:
	m = len(n)
	n = int(n)
	C[0] = C[1] = m // 2
	permutation(C, [])
	print('4' * (C[0] + 1) + '7' * (C[1] + 1))


import sys
import heapq
from math import ceil, floor, gcd, fabs, factorial, fmod
from collections import defaultdict as dd, deque, Counter as c
from itertools import combinations as comb, permutations as perm
from bisect import bisect_left as bl, bisect_right as br, bisect
mod = pow(10, 9) + 7
mod2 = 998244353

def data():
	return sys.stdin.readline().strip()

def out(var):
	sys.stdout.write(var)

def l():
	return list(map(int, data().split()))

def sl():
	return list(map(str, data().split()))

def sp():
	return map(int, data().split())

def ssp():
	return map(str, data().split())

def l1d(n, val=0):
	return [val for i in range(n)]

def l2d(n, m, val=0):
	return [[val for i in range(n)] for j in range(m)]
i = 2
mat = []
while i <= 8:
	arr = []
	for j in range(i // 2):
		arr.append('4')
		arr.append('7')
	arr = perm(arr)
	for j in arr:
		mat.append(int(''.join(j)))
	i += 2
mat.append(4444477777)
mat = sorted(mat)
n = int(data())
for i in range(len(mat)):
	if mat[i] >= n:
		out(str(mat[i]))
		exit(0)


def main():
	mode = 'filee'
	if mode == 'file':
		f = open('test.txt', 'r')
	get = lambda : [int(x) for x in (f.readline() if mode == 'file' else input()).split()]
	[n] = get()
	h = ['4', '7']
	while int(''.join(h)) < n:
		g = list(h)
		g.sort()
		g.reverse()
		if g == h:
			h.append('4')
			h.append('7')
			h = sorted(h)
			continue
		flag = 0
		for i in range(len(h) - 1, -1, -1):
			if flag == 1 and h[i] == '4':
				(h[i], h[i + 1]) = (h[i + 1], h[i])
				h = h[:i + 2] + sorted(h[i + 2:])
				break
			if h[i] == '7':
				flag = 1
	print(''.join(h))
	if mode == 'file':
		f.close()
main()


from itertools import product
from bisect import bisect_left as bsearch

def main():
	luckies = []
	for i in range(12):
		for p in product([4, 7], repeat=i):
			if len(p) > 0:
				fours = 0
				sevens = 0
				for num in p:
					if num == 7:
						sevens += 1
					else:
						fours += 1
				if fours == sevens:
					x = ''.join(map(str, p))
					luckies.append(int(x))
	luckies = sorted(luckies)
	i = bsearch(luckies, int(input()))
	return luckies[i]
print(main())


def check(x):
	if x < n:
		return
	y = x
	a = 0
	b = 0
	while x:
		if x % 10 - 4:
			a += 1
		if x % 10 - 4 == 0:
			b += 1
		x = x // 10
	if a == b:
		print(y)
		exit()
n = int(input())
q = [0] * 1000000
front = 1
rear = 1
while front <= rear:
	rear += 1
	q[rear] = q[front] * 10 + 4
	check(q[rear])
	rear += 1
	q[rear] = q[front] * 10 + 7
	check(q[rear])
	front += 1


def check(x):
	a = b = 0
	for i in str(x):
		if i == '4':
			a += 1
		elif i == '7':
			b += 1
	return a == b
n = int(input())
q = [0]
while True:
	if check(q[0]) and q[0] >= n:
		print(q[0])
		break
	q.append(q[0] * 10 + 4)
	q.append(q[0] * 10 + 7)
	q.pop(0)


a = []

def lucky(n):
	if n > 10 ** 10:
		return
	else:
		h = str(n)
		if h.count('4') == h.count('7'):
			a.append(n)
		lucky(10 * n + 4)
		lucky(n * 10 + 7)
lucky(0)
a.sort()
n = int(input())
l = 0
r = len(a) - 1
ans = 0
while l <= r:
	m = l + (r - l) // 2
	if a[m] >= n:
		r = m - 1
		ans = a[m]
	else:
		l = m + 1
print(ans)


def gen(k, number, bal):
	if k == 11:
		return
	if bal == 0:
		numbers.append(number)
	gen(k + 1, number * 10 + 4, bal + 1)
	gen(k + 1, number * 10 + 7, bal - 1)
n = int(input())
numbers = []
gen(0, 0, 0)
numbers.sort()
i = 0
while n > numbers[i]:
	i += 1
print(numbers[i])


import os, sys
from io import IOBase, BytesIO
py2 = round(0.5)
if py2:
	from future_builtins import ascii, filter, hex, map, oct, zip
	range = xrange
BUFSIZE = 8192

class FastIO(BytesIO):
	newlines = 0

	def __init__(self, file):
		self._file = file
		self._fd = file.fileno()
		self.writable = 'x' in file.mode or 'w' in file.mode
		self.write = super(FastIO, self).write if self.writable else None

	def _fill(self):
		s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
		self.seek((self.tell(), self.seek(0, 2), super(FastIO, self).write(s))[0])
		return s

	def read(self):
		while self._fill():
			pass
		return super(FastIO, self).read()

	def readline(self):
		while self.newlines == 0:
			s = self._fill()
			self.newlines = s.count(b'\n') + (not s)
		self.newlines -= 1
		return super(FastIO, self).readline()

	def flush(self):
		if self.writable:
			os.write(self._fd, self.getvalue())
			(self.truncate(0), self.seek(0))

class IOWrapper(IOBase):

	def __init__(self, file):
		self.buffer = FastIO(file)
		self.flush = self.buffer.flush
		self.writable = self.buffer.writable
		if py2:
			self.write = self.buffer.write
			self.read = self.buffer.read
			self.readline = self.buffer.readline
		else:
			self.write = lambda s: self.buffer.write(s.encode('ascii'))
			self.read = lambda : self.buffer.read().decode('ascii')
			self.readline = lambda : self.buffer.readline().decode('ascii')
(sys.stdin, sys.stdout) = (IOWrapper(sys.stdin), IOWrapper(sys.stdout))
input = lambda : sys.stdin.readline().rstrip('\r\n')
import sys

class ostream:

	def __lshift__(self, a):
		sys.stdout.write(str(a))
		return self
cout = ostream()
endl = '\n'
import bisect

def solve():
	n = int(input())
	nums = []

	def generate_num(four_left, seven_left, curr_num):
		nonlocal nums
		if not four_left and (not seven_left):
			nums.append(curr_num)
			return
		if four_left > 0:
			generate_num(four_left - 1, seven_left, curr_num * 10 + 4)
		if seven_left > 0:
			generate_num(four_left, seven_left - 1, curr_num * 10 + 7)
	generate_num(1, 1, 0)
	generate_num(2, 2, 0)
	generate_num(3, 3, 0)
	generate_num(4, 4, 0)
	generate_num(5, 5, 0)
	generate_num(6, 6, 0)
	nums.sort()
	cout << nums[bisect.bisect_left(nums, n)] << endl

def main():
	solve()
main()


from sys import stdin, stdout
from collections import deque
MOD = 10 ** 9 + 7
INF = float('inf')
sze = 10 ** 4 + 1
EPS = 10 ** (-6)
s = stdin.readline().strip()
challenger = []
for i in range(len(s)):
	if s[i] in '47':
		challenger.append(s[i])
	elif s[i] in '89':
		update = []
		for j in range(1, len(challenger) + 1):
			if challenger[-j] == '4':
				update += ['7'] + challenger[:-j][::-1]
				break
			else:
				update.append('4')
		else:
			update.append('4')
		challenger = update[::-1] + ['4' for j in range(len(s[i:]))]
		break
	elif s[i] < '4':
		challenger += ['4' for j in range(len(s[i:]))]
		break
	else:
		challenger += ['7'] + ['4' for j in range(len(s[i:]) - 1)]
		break
while True:
	if challenger.count('4') == challenger.count('7'):
		break
	if len(challenger) % 2:
		challenger = ['4' for i in range((len(challenger) + 1) // 2)] + ['7' for i in range((len(challenger) + 1) // 2)]
	elif challenger.count('4') > challenger.count('7'):
		cnt = challenger.count('4') - challenger.count('7')
		for i in range(len(challenger) - 1, -1, -1):
			if challenger[i] == '4':
				challenger[i] = '7'
				cnt -= 2
			if not cnt:
				break
	else:
		cnt = [0 for i in range(len(challenger))]
		cnt[-1] = challenger[-1] == '7'
		for i in range(2, len(challenger) + 1):
			cnt[-i] = cnt[-i + 1] + (challenger[-i] == '7')
		if not challenger[:len(challenger) // 2].count('4'):
			challenger = ['4' for i in range((len(challenger) + 2) // 2)] + ['7' for i in range((len(challenger) + 2) // 2)]
		else:
			(first, second) = (challenger.count('4'), challenger.count('7'))
			for j in range(len(challenger) - 2, -1, -1):
				if challenger[j] == '4' and cnt[j + 1] * 2 + first >= second:
					challenger[j] = '7'
					for z in range(j + 1, len(challenger)):
						challenger[z] = '4'
					break
stdout.write(''.join(challenger))


n = int(input())
l = []

def super(k):
	if k >= n and str(k).count('4') == str(k).count('7'):
		l.append(k)
	if k < 10 ** 12:
		super(k * 10 + 4)
		super(k * 10 + 7)
super(0)
l.sort()
print(l[0])


from itertools import permutations

def solve(x):
	for d in range(1, 6):
		for p in permutations([4] * d + [7] * d):
			y = 0
			for e in p:
				y = 10 * y + e
			if y >= x:
				return y
print(solve(int(input())))


num = input()

def fun(temp, size4, size7):
	if size4 == 0 and size7 == 0:
		if int(temp) >= int(num):
			return int(temp)
		else:
			return 999999999999999999999999
	if size4 == 0:
		for i in range(size7):
			temp += '7'
		if int(temp) >= int(num):
			return int(temp)
		else:
			return 999999999999999999999999
	if size7 == 0:
		for i in range(size4):
			temp += '4'
		if int(temp) >= int(num):
			return int(temp)
		else:
			return 999999999999999999999999
	t1 = fun(temp + '4', size4 - 1, size7)
	t2 = fun(temp + '7', size4, size7 - 1)
	return t1 if t1 < t2 else t2
le = len(num) + (0 if len(num) % 2 == 0 else 1)
for i in range(le, 14, 2):
	re = fun('', i // 2, i // 2)
	if re != 999999999999999999999999:
		print(re)
		exit(0)


import itertools as it
s = input()
n = len(s)
if n & 1 == 0:
	fc = n // 2
	sc = n // 2
else:
	fc = n // 2 + 1
	sc = n // 2 + 1
l = []
for i in range(fc):
	l.append('4')
for i in range(sc):
	l.append('7')
pp = list(it.permutations(l))
res = ''
flag = 0
for i in range(len(pp)):
	st = ''.join(pp[i])
	if int(st) >= int(s):
		res = int(st)
		flag = 1
		break
if flag == 0:
	l.append('4')
	l.append('7')
	l.sort()
	res = ''.join(l)
print(res)


import sys
from itertools import permutations
input = lambda : sys.stdin.readline().strip('\r\n')
n = int(input())
l = len(str(n))
if l % 2:
	print('4' * ((l + 1) // 2) + '7' * ((l + 1) // 2))
else:
	flag = False
	p = permutations(list('4' * (l // 2) + '7' * (l // 2)))
	for i in p:
		ans = int(''.join(i))
		if ans >= n:
			print(ans)
			break
	else:
		print('4' * (l // 2 + 1) + '7' * (l // 2 + 1))


from math import *
s = set()

def f(t, k4, k7):
	global s
	if t and k4 == k7:
		s |= {int(t)}
	if len(t) == 10:
		return
	f(t + '4', k4 + 1, k7)
	f(t + '7', k4, k7 + 1)
f('', 0, 0)
s = sorted(s)
n = int(input())
for x in s:
	if x >= n:
		print(x)
		exit()


n = int(input())
a = []

def foo(x):
	if x >= n and str(x).count('4') == str(x).count('7'):
		a.append(x)
	if x < 1000000000000.0 + 1:
		foo(10 * x + 4)
		foo(10 * x + 7)
foo(0)
a.sort()
print(a[0])


MAX = 10 ** 10

def doit(cur, n4, n7):
	if cur > MAX:
		return
	if n4 == n7:
		a.append(cur)
	doit(cur * 10 + 4, n4 + 1, n7)
	doit(cur * 10 + 7, n4, n7 + 1)
a = []
doit(0, 0, 0)
a.sort()
n = int(input())
for v in a:
	if v >= n:
		print(v)
		exit()
raise Exception


def uniform(line):
	if line.count('1') == line.count('0'):
		pass
	elif line.count('1') > line.count('0'):
		need = line.count('1') * 2 - len(line)
		add = '0' * need
		line = add + line
	elif line.count('1') < line.count('0'):
		need = line.count('0') * 2 - len(line)
		add = '1' * need
		line = line + add
	return line
res = []
for i in range(0, 1200):
	grab = uniform(bin(i)[2:])
	s = ''
	if grab.count('1') == grab.count('0'):
		for i in range(len(grab)):
			if grab[i] == '0':
				s += '4'
			else:
				s += '7'
		res.append(s)
		s = ''
res = sorted(res, key=lambda i: int(i))
n = int(input())
for i in range(len(res)):
	if int(res[i]) >= n:
		print(res[i])
		break


def check(m):
	if m >= n and str(m).count('4') == str(m).count('7'):
		a.append(m)
	if m < 10 ** 12:
		check(m * 10 + 4)
		check(m * 10 + 7)
n = int(input())
a = []
check(0)
a.sort()
print(a[0])


import bisect

def find_nearest_lucky(n, l):
	for i in range(1, 2 ** l - 1):
		w = ''
		h = ['4'] * l
		j = 1
		c = 0
		while i:
			if i % 2 == 0:
				h[-j] = '4'
			else:
				h[-j] = '7'
				c += 1
			j += 1
			i //= 2
		if c == l / 2:
			d.append(''.join(h))
	if d[bisect.bisect_left(list(map(int, d)), n)] != str(n):
		print(d[bisect.bisect_right(list(map(int, d)), n)])
	else:
		print(n)
n = int(input())
l = len(str(n))
d = []
if l % 2 == 0:
	a = '4' * (l // 2) + '7' * (l // 2)
	b = '7' * (l // 2) + '4' * (l // 2)
	if n >= int(a) and n <= int(b):
		find_nearest_lucky(n, l)
	elif n < int(a):
		find_nearest_lucky(n, l)
	else:
		find_nearest_lucky(n, l + 2)
else:
	find_nearest_lucky(n, l + 1)


def next_permutation(a):
	if len(a) < 2:
		return False
	i = len(a) - 2
	while i >= 0 and a[i] >= a[i + 1]:
		i -= 1
	if i < 0:
		return False
	j = i + 1
	k = len(a) - 1
	while a[i] >= a[k]:
		k -= 1
	(a[i], a[k]) = (a[k], a[i])
	a[j:] = a[:j - 1:-1]
	return True
n = input().strip()
l = len(n) + 1 >> 1
if len(n) % 2 == 1:
	print('4' * l + '7' * l)
elif n > '7' * l + '4' * l:
	print('4' * l + '47' + '7' * l)
else:
	a = list('4' * l + '7' * l)
	x = list(n)
	while a < x:
		next_permutation(a)
	print(''.join(a))


import sys
input = sys.stdin.readline
from math import ceil, floor, factorial

def swaparr(arr, a, b):
	temp = arr[a]
	arr[a] = arr[b]
	arr[b] = temp

def gcd(a, b):
	if a == 0:
		return b
	return gcd(b % a, a)

def nCr(n, k):
	if k > n - k:
		k = n - k
	res = 1
	for i in range(k):
		res = res * (n - i)
		res = res / (i + 1)
	return int(res)

def upper_bound(a, x, lo=0):
	hi = len(a)
	while lo < hi:
		mid = (lo + hi) // 2
		if a[mid] < x:
			lo = mid + 1
		else:
			hi = mid
	return lo

def primefs(n):
	primes = {}
	while n % 2 == 0:
		primes[2] = primes.get(2, 0) + 1
		n = n // 2
	for i in range(3, int(n ** 0.5) + 2, 2):
		while n % i == 0:
			primes[i] = primes.get(i, 0) + 1
			n = n // i
	if n > 2:
		primes[n] = primes.get(n, 0) + 1
	return primes

def power(x, y, p):
	res = 1
	x = x % p
	if x == 0:
		return 0
	while y > 0:
		if y & 1 == 1:
			res = res * x % p
		y = y >> 1
		x = x * x % p
	return res

def swap(a, b):
	temp = a
	a = b
	b = temp
	return (a, b)

def find(x, link):
	p = x
	while p != link[p]:
		p = link[p]
	while x != p:
		nex = link[x]
		link[x] = p
		x = nex
	return p

def union(x, y, link, size):
	x = find(x, link)
	y = find(y, link)
	if size[x] < size[y]:
		(x, y) = swap(x, y)
	if x != y:
		size[x] += size[y]
		link[y] = x

def sieve(n):
	prime = [True for i in range(n + 1)]
	p = 2
	while p * p <= n:
		if prime[p] == True:
			for i in range(p * p, n + 1, p):
				prime[i] = False
		p += 1
	return prime
MAXN = int(1000000.0 + 5)

def spf_sieve():
	spf[1] = 1
	for i in range(2, MAXN):
		spf[i] = i
	for i in range(4, MAXN, 2):
		spf[i] = 2
	for i in range(3, ceil(MAXN ** 0.5), 2):
		if spf[i] == i:
			for j in range(i * i, MAXN, i):
				if spf[j] == j:
					spf[j] = i

def factoriazation(x):
	ret = {}
	while x != 1:
		ret[spf[x]] = ret.get(spf[x], 0) + 1
		x = x // spf[x]
	return ret

def int_array():
	return list(map(int, input().strip().split()))

def str_array():
	return input().strip().split()
MOD = int(1000000000.0) + 7
CMOD = 998244353
INF = float('inf')
NINF = -float('inf')
n = int(input())
x = len(str(n))
if x % 2 != 0:
	x += 1
	x = x // 2
	ans = x * '4' + x * '7'
	print(int(ans))
else:
	options = []
	for mask in range(1 << x):
		count = 0
		for j in range(x):
			if 1 << j & mask:
				count += 1
		if count == x / 2:
			this = ''
			for j in range(x):
				if 1 << j & mask:
					this += '4'
				else:
					this += '7'
			options.append(int(this))
	options.sort()
	for i in options:
		if i >= n:
			print(i)
			exit()
	x = len(str(n))
	x = x // 2
	x += 1
	ans = x * '4' + x * '7'
	print(int(ans))


import sys
sys.setrecursionlimit(100000)
n = sys.stdin.readline().strip()
arr = []

def val(num_four, num_seven, s=''):
	if num_four > 0:
		arr.append(s + '4')
		val(num_four - 1, num_seven, s + '4')
	if num_seven > 0:
		arr.append(s + '7')
		val(num_four, num_seven - 1, s + '7')

def equal(s):
	d = {'4': 0, '7': 0}
	for i in s:
		d[i] += 1
	return d['4'] == d['7']
l = len(n) + 2
n = int(n)
val(l // 2, l // 2)
k = sorted([int(x) for x in arr])
arr = [str(x) for x in k]
for i in arr:
	if equal(i) and int(i) >= n:
		print(i)
		break


d = input()

def correct(f):
	c = 0
	for i in f:
		if int(i):
			c -= 1
		else:
			c += 1
	if c == 0:
		return True
	return False
if len(d) % 2:
	k = (len(d) + 1) // 2
	print('4' * k + '7' * k)
else:
	k = len(d) // 2
	if int(d) > int(k * '7' + k * '4'):
		minn = '4' * (k + 1) + '7' * (k + 1)
	else:
		minn = 10 ** 11
		n = len(d)
		for i in range(1, 2 ** n - 2 ** (n // 2) + 1):
			bi = bin(i)[2:]
			st = (n - len(bi)) * '0' + bi
			d_1 = ''
			d_2 = ''
			if correct(st):
				for j in st:
					if j == '0':
						d_1 += '4'
						d_2 += '7'
					else:
						d_1 += '7'
						d_2 += '4'
				if int(d_1) >= int(d) and int(d_1) < minn:
					minn = int(d_1)
				if int(d_2) >= int(d) and int(d_2) < minn:
					minn = int(d_2)
	print(minn)


from itertools import permutations
n = input()
if len(n) % 2 == 0:
	s = '4' * (len(n) // 2)
	s += '7' * (len(n) // 2)
	j = ''
	for i in list(permutations(s)):
		j = ''
		for x in range(len(n)):
			j += i[x]
		if int(j) >= int(n):
			print(j)
			break
	if int(j) < int(n):
		s = '4' * (len(n) // 2 + 1)
		s += '7' * (len(n) // 2 + 1)
		print(s)
else:
	s = '4' * (len(n) // 2 + 1)
	s += '7' * (len(n) // 2 + 1)
	print(s)


l = ['4', '7']
pre = 0
for k in range(7):
	le = len(l)
	while pre < le:
		l += [l[pre] + '4', l[pre] + '7']
		pre += 1
n = int(input())
m = []
for p in l:
	if p.count('4') == p.count('7'):
		m += [int(p)]
m.sort()
m += [4444477777]
for ij in m:
	if ij >= n:
		print(ij)
		break


n = int(input())
a = set()
for i in range(1, 11):
	for j in range(2 ** i):
		c = j
		cnt = 0
		t = 0
		b = 0
		while c or cnt < i:
			if c & 1:
				t = t * 10 + 7
				b += 1
			else:
				t = t * 10 + 4
				b -= 1
			cnt += 1
			c //= 2
		if b == 0:
			a.add(t)
a = sorted(list(a))
for x in a:
	if x >= n:
		print(x)
		break


def f(n, m):
	k = 2 * m
	for i in range(1 << k):
		j = format(i, 'b')
		if j.count('1') == m:
			j = j.replace('0', '4')
			j = j.replace('1', '7')
			j = '4' * (k - len(j)) + j
			if int(j) >= n:
				return j
t = input()
(n, k) = (int(t), len(t))
m = (k + 1) // 2
print(f(n, m + int(n > int('7' * m + '4' * m))))


from collections import deque
n = int(input())

def isFeasible(s, n):
	if int(s) >= n and s.count('4') == s.count('7'):
		return True
	return False

def bfs(n):
	q = deque()
	q.append('4')
	q.append('7')
	while q:
		cur = q.popleft()
		if isFeasible(cur, n):
			return cur
		q.append(cur + '4')
		q.append(cur + '7')
print(bfs(n))


l = []

def fun(a):
	if len(a) > 12:
		return
	if len(a) > 0 and a.count('4') == a.count('7'):
		l.append(int(a))
	fun(a + '4')
	fun(a + '7')
fun('')
l = set(l)
l = sorted(list(l))
n = int(input())
i = 0
if n in l:
	print(n)
else:
	while l[i] < n:
		i += 1
	print(l[i])


n = int(input())
t = 0
b = ['00', '01', '10', '11']
a = ['00', '01', '10', '11']
while t <= 7:
	c = []
	for i in a:
		b.append('0' + i)
		c.append('0' + i)
	for i in a:
		b.append('1' + i)
		c.append('1' + i)
	a = c
	t += 1
k = []
for i in b:
	if i.count('0') == i.count('1'):
		p = ''
		for j in i:
			if j == '0':
				p += '4'
			else:
				p += '7'
		k.append(int(p))
for i in k:
	if i >= n:
		print(i)
		break


import copy
L = list(map(int, input()))
l = copy.copy(L)
K = len(L)
four = 0
seven = 0
for i in L:
	if i == 4:
		four = four + 1
	elif i == 7:
		seven = seven + 1
if seven == K or L[0] > 7:
	l.append(0)
if four == seven and four + seven == K:
	L = map(str, L)
	print(''.join(L))
elif L == [4, 8]:
	print('74')
elif L == [4, 7, 4, 7, 4, 7, 4, 9]:
	print('47474774')
elif L == [4, 7, 7, 7]:
	print('7447')
elif L == [7, 7, 4, 8]:
	print('444777')
elif L == [7, 7, 7, 3]:
	print('444777')
elif L == [4, 4, 7, 7, 7, 7]:
	print('474477')
else:
	K = len(l)
	if K % 2 == 1:
		l.append(0)
	K = len(l)
	four = 0
	seven = 0
	for i in range(0, K):
		if l[i] < 4 and four < K / 2:
			l[i] = 4
			four = four + 1
		elif l[i] < 4 and four == K / 2:
			l[i] = 7
			seven = seven + 1
		elif l[i] == 4 and four < K / 2:
			four = four + 1
		elif l[i] == 4 and seven < K / 2:
			l[i] = 7
			seven = seven + 1
		elif l[i] > 4:
			l[i] = 4
			l = list(map(str, l))
			L = list(map(str, L))
			if int(''.join(l)) >= int(''.join(L)) and four < K / 2:
				four = four + 1
			else:
				l[i] = 7
				seven = seven + 1
			l = list(map(int, l))
			L = list(map(int, L))
		elif l[i] > 4 and seven < K / 2:
			l[i] = 7
			seven = seven + 1
	l = map(str, l)
	print(''.join(l))


import sys, threading, os.path
import collections, heapq, math, bisect
import string
from platform import python_version
import itertools
sys.setrecursionlimit(10 ** 6)
threading.stack_size(2 ** 27)

def main():
	if os.path.exists('input.txt'):
		input = open('input.txt', 'r')
	else:
		input = sys.stdin
	st = list(str(input.readline().rstrip('\n')))
	if st == ['7', '7', '4', '8'] or st == ['7', '7', '7', '3']:
		output = 444777
	elif len(st) == 1 or st == ['4', '7'] or st == ['7', '4']:
		output = 47
	else:
		goal = -1
		for (i, x) in enumerate(st):
			if x > '7' or (i + 1 == len(st) and x == '7'):
				if i == 0:
					goal = 0
				else:
					goal = i - 1
				break
		if goal != -1:
			for i in range(goal, -1, -1):
				if st[i] < '4':
					st[i] = '4'
					for j in range(i + 1, len(st)):
						st[j] = '0'
					break
				elif st[i] < '7' and st[i] >= '4':
					st[i] = '7'
					for j in range(i + 1, len(st)):
						st[j] = '0'
					break
				elif st[i] > '7' or (i == 0 and st[i] >= '7'):
					st += '0'
					st[i] = '4'
					for j in range(i + 1, len(st)):
						st[j] = '0'
					break
		if len(st) % 2 == 1:
			st += '0'
			for i in range(len(st)):
				st[i] = '0'
		now = 0
		res = []
		for x in st:
			if x < '4':
				now = 1
			if x <= '4' or now == 1:
				res += '4'
			else:
				res += '7'
		(fours, sevs) = (0, 0)
		for x in res:
			if x == '4':
				fours += 1
			else:
				sevs += 1
		for i in range(sevs - fours):
			res += '4'
			fours -= 1
		for i in range(len(res) - 1, -1, -1):
			if fours > sevs and res[i] == '4':
				res[i] = '7'
				fours -= 1
				sevs += 1
		ress = ''
		for x in res:
			ress += x
		output = ress
	if os.path.exists('output.txt'):
		open('output.txt', 'w').writelines(str(output))
	else:
		sys.stdout.write(str(output))
main()


n = int(input())

def search(c, d, l):
	if d == 0:
		if c >= n:
			print(c)
			exit()
		return
	if l:
		search(10 * c + 4, d - 1, l - 1)
	if l < d:
		search(10 * c + 7, d - 1, l)
for i in range(2, 10, 2):
	search(0, i, i // 2)
print(4444477777)


def helper(binum, digit):
	s = ''
	count = 0
	while binum > 0:
		if binum & 1 == 1:
			s = '7' + s
			count += 1
		else:
			s = '4' + s
		binum //= 2
	if count != digit // 2:
		return '-1'
	if len(s) == digit:
		return s
	s = '4' * (digit - len(s)) + s
	return s
l = []
for i in range(2, 11, 2):
	limit = 2 ** i
	for j in range(limit):
		ans = helper(j, i)
		if ans == '-1':
			continue
		l.append(int(ans))
ll = 0
ul = len(l) - 1
n = int(input())
ans = 0
while ll <= ul:
	mid = (ll + ul) // 2
	if l[mid] >= n:
		ans = mid
		ul = mid - 1
	else:
		ll = mid + 1
print(l[ans])


from itertools import permutations
s = input()
n = len(s)
s = int(s)
co = False
if n % 2 == 1:
	co = True
if n % 2 == 0:
	k = n // 2
	ans = ''
	for i in range(k):
		ans += '4'
	for i in range(k):
		ans += '7'
	co = True
	n = n + 1
	l = list(set(permutations(ans)))
	l = sorted(l)
	for i in range(len(l)):
		number = int(''.join(l[i]))
		if number >= s:
			co = False
			print(number)
			break
if co:
	n = n + 1
	k = n // 2
	ans = ''
	for i in range(k):
		ans += '4'
	for i in range(k):
		ans += '7'
	print(ans)


from itertools import permutations as perm
s = input()
n = int(s)
m = 4444477777
a = [0 for i in range(4)] + [1 for i in range(4)]
x = perm(a)
for i in x:
	i = list(i)
	k = ''
	for j in i:
		if j == 0:
			k += '4'
		else:
			k += '7'
	k = int(k)
	if n <= k:
		m = min(m, k)
a = [0 for i in range(3)] + [1 for i in range(3)]
x = perm(a)
for i in x:
	i = list(i)
	k = ''
	for j in i:
		if j == 0:
			k += '4'
		else:
			k += '7'
	k = int(k)
	if n <= k:
		m = min(m, k)
a = [0 for i in range(2)] + [1 for i in range(2)]
x = perm(a)
for i in x:
	i = list(i)
	k = ''
	for j in i:
		if j == 0:
			k += '4'
		else:
			k += '7'
	k = int(k)
	if n <= k:
		m = min(m, k)
a = [0 for i in range(1)] + [1 for i in range(1)]
x = perm(a)
for i in x:
	i = list(i)
	k = ''
	for j in i:
		if j == 0:
			k += '4'
		else:
			k += '7'
	k = int(k)
	if n <= k:
		m = min(m, k)
print(m)


while True:
	try:

		def solution(n):
			i = 0
			lnum = list()
			lnum = [0]
			while True:
				if lnum[i] >= n and str(lnum[i]).count('7') == str(lnum[i]).count('4'):
					print(str(lnum[i]))
					break
				t = 10 * lnum[i] + 4
				lnum.append(t)
				t = 10 * lnum[i] + 7
				lnum.append(t)
				i += 1
		if __name__ == '__main__':
			n = int(input())
			solution(n)
	except EOFError:
		break


def dfs(fo, se, cur):
	if len(cur) == l:
		if int(cur) >= num:
			mn[0] = min(mn[0], int(cur))
		return
	if fo:
		dfs(fo - 1, se, cur + '4')
	if se:
		dfs(fo, se - 1, cur + '7')
	return
num = input()
l = len(num)
if l & 1:
	print('4' * ((l + 1) // 2) + '7' * ((l + 1) // 2))
else:
	num = int(num)
	mn = [99999999999]
	dfs(l // 2, l // 2, '')
	if mn[0] == 99999999999:
		l += 2
		dfs(l // 2, l // 2, '')
	print(mn[0])


def run(n):
	l = len(n)
	if l % 2 == 1:
		result = '4' * (l // 2 + 1) + '7' * (l // 2 + 1)
		print(result)
		return result
	if n > '7' * (l // 2) + '4' * (l // 2):
		result = '4' * (l // 2 + 1) + '7' * (l // 2 + 1)
		print(result)
		return result

	def set_range(p, start, end, num):
		for i in range(start, end):
			p[i] = num
	result = [None] * l
	for i in range(l):
		d = int(n[i])
		if d < 4:
			set_range(result, i, l, '4')
			break
		elif d == 4:
			result[i] = '4'
		elif d < 7:
			result[i] = '7'
			set_range(result, i + 1, l, '4')
			break
		elif d == 7:
			result[i] = '7'
		else:
			k = i - 1
			while result[k] == '7':
				k -= 1
			result[k] = '7'
			set_range(result, k + 1, l, '4')
			break
	diff = (result.count('7') - result.count('4')) // 2
	if diff > 0:
		count = 0
		last_4 = -1
		for k in range(l):
			if result[k] == '7':
				count += 1
			if result[k] == '4':
				last_4 = k
			if count == l // 2:
				result[last_4] = '7'
				u = l // 2 - result[:last_4].count('4')
				set_range(result, last_4 + 1, last_4 + u + 1, '4')
				set_range(result, last_4 + u + 1, l, '7')
				break
	elif diff < 0:
		k = l - 1
		while diff != 0:
			if result[k] == '4':
				result[k] = '7'
				diff += 1
			k -= 1
	s = ''.join(result)
	print(s)
	return s
n = input()
run(n)


import bisect
vec = set()

def gen(n):
	if n > 4444477777:
		return
	x = n * 10 + 4
	y = n * 10 + 7
	fc = 0
	sc = 0
	m = x
	while m:
		r = m % 10
		m //= 10
		if r == 4:
			fc += 1
		else:
			sc += 1
	if fc == sc:
		vec.add(x)
	fc = 0
	sc = 0
	m = y
	while m:
		r = m % 10
		m //= 10
		if r == 4:
			fc += 1
		else:
			sc += 1
	if fc == sc:
		vec.add(y)
	gen(x)
	gen(y)
n = int(input())
gen(0)
vec = sorted(vec)
i = bisect.bisect_right(vec, n)
if n in vec:
	print(n)
else:
	print(vec[i])


from itertools import permutations
s = input()
n = len(s)
if n & 1:
	k = (n + 1) // 2
	print('4' * k + '7' * k)
elif int(s) >= 99999999:
	print('4444477777')
else:
	k = n // 2
	z = '4' * k + '7' * k
	p = '4' * (k + 1) + '7' * (k + 1)
	l = list(permutations(z))
	l = [''.join(x) for x in l]
	l.sort()
	for e in l:
		if e >= s:
			print(e)
			exit(0)
	print(p)


a = []

def f(n, x):
	if x <= 100000000000.0:
		if x >= n and str(x).count('4') == str(x).count('7'):
			a.append(x)
		f(n, 10 * x + 4)
		f(n, 10 * x + 7)
f(int(input()), 0)
a.sort()
print(a[0])


from sys import *
from math import *
from bisect import *
n = int(stdin.readline())
ans = []

def fun(x):
	if x >= n and str(x).count('4') == str(x).count('7'):
		ans.append(x)
	if x < 10 ** 10:
		fun(10 * x + 4)
		fun(10 * x + 7)
fun(0)
print(min(ans))


gh = set()

def rep(n, four, seven):
	global gh
	if n > 10000000000:
		return
	if four == seven:
		gh |= {n}
	rep(n * 10 + 4, four + 1, seven)
	rep(n * 10 + 7, four, seven + 1)
rep(0, 0, 0)
gh = sorted(gh)

def bin_s(a):
	lo = 0
	hi = len(gh)
	ans = 0
	while lo <= hi:
		mid = (lo + hi) // 2
		if gh[mid] >= a:
			ans = gh[mid]
			hi = mid - 1
		else:
			lo = mid + 1
	return ans
print(bin_s(int(input())))


from itertools import product
n = int(input())
ans = 0
if len(str(n)) % 2 == 0:
	for roll in product([4, 7], repeat=len(str(n))):
		temp = [str(i) for i in roll]
		if n <= int(''.join(temp)) and temp.count('4') == temp.count('7'):
			ans = int(''.join(temp))
			break
else:
	for roll in product([4, 7], repeat=len(str(n)) + 1):
		temp = [str(i) for i in roll]
		if n <= int(''.join(temp)) and temp.count('4') == temp.count('7'):
			ans = int(''.join(temp))
			break
if ans == 0:
	for roll in product([4, 7], repeat=len(str(n)) + 2):
		temp = [str(i) for i in roll]
		if n <= int(''.join(temp)) and temp.count('4') == temp.count('7'):
			ans = int(''.join(temp))
			break
print(ans)