Problem: 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

Solution (in JAVA):

Solution:
import java.util.*;

public class LuckyNumbers {

	public void solve() {
		Scanner scanner = new Scanner(System.in);
		String s = scanner.nextLine();
		scanner.close();
		int length = s.length();
		if (length % 2 != 0)
			print(length + 1);
		else if (s.charAt(0) > '7')
			print(length + 2);
		else if (s.charAt(0) < '4')
			print(length);
		else if (s.charAt(0) > '4' && s.charAt(0) < '7') {
			System.out.print("74");
			print(length - 2);
		} else {
			int numOfSeven = 0;
			int count = 0;
			while (count < length && s.charAt(count) == '7') {
				numOfSeven++;
				count++;
			}
			if (numOfSeven > length / 2)
				print(length + 2);
			else if (numOfSeven == length / 2) {
				if (s.charAt(count) > '4')
					print(length + 2);
				else if (s.charAt(count) < '4') {
					System.out.print(s.substring(0, count));
					for (int i = 0; i < numOfSeven; i++)
						System.out.print("4");
				}
				else{
					int i = length - 1;
					for(; i > length - numOfSeven; i--)
						if(s.charAt(i) > '4')
							break;
					if(i == length - numOfSeven){
						System.out.print(s.substring(0, count));
						for (int j = 0; j < numOfSeven; j++)
							System.out.print("4");
					}
					else
						print(length + 2);
				}

			}

			else {
				int numOfFour = 0;
				while (count < length) {
					if (s.charAt(count) != '4' && s.charAt(count) != '7') {
						break;
					}
					if (s.charAt(count) == '4')
						numOfFour++;
					else
						numOfSeven++;
					count++;
				}// end while
				if (count == length) {
					if (numOfFour == numOfSeven)
						System.out.println(s);
					else if (numOfFour < numOfSeven) {
						int diff = (numOfSeven - numOfFour) / 2;
						String s1 = new String(s);
						int i = length - 1;
						for (; i >= 0 && diff >= 0; i--) {
							if (s.charAt(i) == '7') {
								diff--;
								numOfSeven--;
								// s1 = s1.substring(0, i) + "4" + s1.substring(i + 1,
								// length);
							}
							if(s.charAt(i) == '4'){
								numOfFour--;
							}
						}
						while (i >= 0) {
							if (s.charAt(i) == '4') {
								s1 = s1.substring(0, i) + "7" + s1.substring(i + 1, length);
								numOfSeven++;
								numOfFour--;
								break;
							}
							if(s.charAt(i) == '7')
								numOfSeven--;
							i--;
						}
						System.out.print(s1.substring(0, i + 1));
						for (int j = 0; j < (numOfSeven - numOfFour); j++) {
							System.out.print("4");
						}

						int max = numOfSeven > numOfFour ? numOfSeven : numOfFour;
						print(length - max * 2);
						for (int j = 0; j < (numOfFour - numOfSeven); j++) {
							System.out.print("7");
						}
					} else
						banFourAndSeven(s, numOfFour, numOfSeven, length);
				}// end if ==
				else {
					String s1 = "";
					if (s.charAt(count) < '4'
							|| (s.charAt(count) > '4' && s.charAt(count) < '7')) {
						if (s.charAt(count) < '4') {
							s1 = s.substring(0, count) + "4";
							numOfFour++;
						} else {
							s1 = s.substring(0, count) + "7";
							numOfSeven++;
						}
						if (numOfSeven + numOfFour != length) {
							if (numOfSeven == numOfFour) {
								System.out.print(s1);
								print(length - numOfSeven * 2);
							} else if (numOfSeven < numOfFour) {
								System.out.print(s1);
								print(length - numOfSeven - numOfFour
										- (numOfFour - numOfSeven));
								for (int i = 0; i < numOfFour - numOfSeven; i++)
									System.out.print("7");
							} else {
								System.out.print(s1);
								for (int i = 0; i < numOfSeven - numOfFour; i++)
									System.out.print("4");

								print(length - numOfSeven - numOfFour
										- (numOfSeven - numOfFour));
							}
						}
						else{
							banFourAndSeven(s1, numOfFour, numOfSeven, length);
						}
					} else {
						int i = count - 1;
						for (; i >= 0; i--) {
							if (s.charAt(i) == '4')
								break;
							if (s.charAt(i) == '7')
								numOfSeven--;
						}// end for
						if (i < 0) {
							print(length + 2);
						} else {
							s1 = s.substring(0, i) + "7";
							numOfSeven++;
							numOfFour--;
							if ((numOfFour + numOfSeven) % 2 != 0) {
								s1 += "4";
								numOfFour++;
							}
							if (numOfFour != numOfSeven) {
								banFourAndSeven(s1, numOfFour, numOfSeven,
										numOfFour + numOfSeven);
								print(length - numOfFour - numOfSeven);
							} else {
								System.out.print(s1);
								print(length - numOfFour * 2);
							}
						}// end else
					}

				}
			}
		}

	}

	public void banFourAndSeven(String s, int numOfFour, int numOfSeven,
			int length) {
		if (numOfSeven > numOfFour) {
			int diff = (numOfSeven - numOfFour) / 2;
			String s1 = new String(s);
			int i = length - 1;
			for (; i >= 0 && diff >= 0; i--) {
				if (s.charAt(i) == '7') {
					diff--;
					numOfSeven--;
					// s1 = s1.substring(0, i) + "4" + s1.substring(i + 1,
					// length);
				}
				if(s.charAt(i) == '4'){
					numOfFour--;
				}
			}
			while (i >= 0) {
				if (s.charAt(i) == '4') {
					s1 = s1.substring(0, i) + "7" + s1.substring(i + 1, length);
					numOfSeven++;
					numOfFour--;
					break;
				}
				if(s.charAt(i) == '7')
					numOfSeven--;
				i--;
			}
			System.out.print(s1.substring(0, i + 1));
			for (int j = 0; j < (numOfSeven - numOfFour); j++) {
				System.out.print("4");
			}
			print(length - numOfSeven * 2);

		} else {
			int diff = (numOfFour - numOfSeven) / 2;
			String s1 = new String(s);
			for (int i = length - 1; i >= 0 && diff >= 1; i--) {
				if (s.charAt(i) == '4') {
					diff--;
					s1 = s1.substring(0, i) + "7" + s1.substring(i + 1, length);
				}
			}
			System.out.print(s1);
		}
	}

	public void print(int length) {
		for (int i = 1; i <= length / 2; i++) {
			System.out.print("4");
		}
		for (int i = 1; i <= length / 2; i++) {
			System.out.print("7");
		}
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new LuckyNumbers().solve();
	}

}