#include <bits/stdc++.h>   using namespace std;   inline bool eq(double a, double b) { return fabs(a - b) < 1e-9; }   const long long int inf = 1e15;   inline int two(int n) { return 1 << n; }   inline int test(int n, int b) { return (n >> b) & 1; }   inline void set_bit(int& n, int b) { n |= two(b); }   inline void unset_bit(int& n, int b) { n &= ~two(b); }   inline int last_bit(int n) { return n & (-n); }   inline int ones(int n) {     int res = 0;     while (n && ++res) n -= n & (-n);     return res;   }   string str;   string sol(int n) {     string ans;     for (int i = (0); i < (n); i++) {       ans = ans + (i < n / 2 ?  4  :  7 );     }     return ans;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     cin >> str;     int n = str.length();     if (n & 1) {       cout << sol(n + 1) << endl;     } else {       int ind = -1, c4 = 0, c7 = 0;       for (int i = (0); i < (n); i++) {         if (str[i] >  7 ) {           if (ind == -1) {             cout << sol(n + 2) << endl;             return 0;           } else {             str[ind] =  7 ;             str[i] =  4 ;             c7++;             if (abs(c4 - c7) > n - i - 1) {               cout << sol(n + 2) << endl;               return 0;             } else {               int j = i + 1;               for (int k = (j); k < (n); k++) {                 str[k] =  4 ;                 c4++;               }               for (int k = (n - 1); k >= (i + 1); k--) {                 if (c4 > c7) {                   str[k] =  7 ;                   c4--;                   c7++;                 } else {                   break;                 }               }               cout << str << endl;               return 0;             }           }         } else if (str[i] !=  4  && str[i] !=  7 ) {           if (str[i] <  4 ) {             str[i] =  4 ;             c4++;           } else {             str[i] =  7 ;             c7++;           }           if (abs(c4 - c7) > n - i - 1) {             cout << sol(n + 2) << endl;             return 0;           } else {             int j = i + 1;             for (int k = (j); k < (n); k++) {               str[k] =  4 ;               c4++;             }             for (int k = (n - 1); k >= (i + 1); k--) {               if (c4 > c7) {                 str[k] =  7 ;                 c4--;                 c7++;               } else {                 break;               }             }             cout << str << endl;             return 0;           }         } else {           if (str[i] ==  4 ) {             ind = i;             c4++;           } else             c7++;         }       }       if (c4 > c7) {         for (int i = (ind); i >= (0); i--) {           if (c4 > c7) {             str[i] =  7 ;             c4--;             c7++;           } else {             break;           }         }         cout << str << endl;       } else if (c7 > c4) {         int cc4 = 0, cc7 = 0;         for (int i = 0; i <= ind; i++) {           if (str[i] ==  4 )             cc4++;           else             cc7++;         }         c4 = cc4, c7 = cc7;         for (int i = (ind); i >= (0); i--) {           if (str[i] ==  4 ) {             if (abs(c4 - c7 - 1) <= n - i - 1) {               str[i] =  7 ;               c4--;               c7++;               int j = i + 1;               for (int k = (j); k < (n); k++) {                 str[k] =  4 ;                 c4++;               }               for (int k = (n - 1); k >= (i + 1); k--) {                 if (c4 > c7) {                   str[k] =  7 ;                   c4--;                   c7++;                 } else {                   break;                 }               }               cout << str << endl;               return 0;             }             c4--;           } else {             c7--;           }         }         cout << sol(n + 2) << endl;       } else {         cout << str << endl;       }     }     return 0;   }