#define _USE_MATH_DEFINES
#include <iostream>
#include <complex>
#include <algorithm>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <cmath>
#include <math.h>
#include <numeric>
#include <list>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <climits>
#include <set>
#include <memory.h>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <map>
#include <cassert>
#include <time.h>
#include <ctime>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int , P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 27;
const double EPS = 1e-10;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;

#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)

int nums[200000];

int main(){
    string number;
    cin >> number;
    int sz = number.size();
    nums[0] = 0;
    rep(i, (int)number.size()) nums[i+1] = number[i] - '0';

    for(int i = sz; i > 0; i--){
        if(nums[i] > 7){
            nums[i] = 0;
            nums[i-1]++;
            int j = i;
            while(j > 0 && nums[j] >= 10){
                nums[j] -= 10;
                nums[--j]++;
            }
        }
    }
    int ind = 0;
    int four = 0;
    int seven = 0;

    while(nums[ind] == 0) ind++;

    bool room = false;
    rep2(i, ind, sz + 1){
        if(four == (sz + 1 - ind) / 2) nums[i] = 7;
        else if(room || nums[i] <= 4){
            if(nums[i] < 4)room = true;
            nums[i] = 4;
            four++;
        }else{
            if(nums[i] < 7) room = true;
            nums[i] = 7;
        }
    }

    four = 0;
    seven = 0;
    rep2(i, ind, sz + 1){
        if(nums[i] <= 4){
            nums[i] = 4;
            four++;
        }
        else{
            nums[i] = 7;
            seven++;
        }
    }

    if((seven + four) % 2 == 1){
        int d = (seven + four + 1);
        rep(i, d / 2) cout << 4;
        rep(i, d / 2) cout << 7;
        cout << endl;
    }else if(four > seven){
        int dif = four - seven;
        for(int i = sz; i >= ind; i--){
            if(dif == 0) break;
            if(nums[i] == 4){
                nums[i] = 7;
                dif -= 2;
            }
        }
        rep2(i, ind, sz + 1) cout << nums[i];
        cout << endl;
    }else if(seven > four){
        int d = seven + four;
        bool ok = false;
        rep2(i, ind, d / 2 + ind) if(nums[i] == 4) ok = true;
        if(ok){
            int f2 = four;
            int rest = four;
            int cnt = 0;
            //cout << f2 << endl;
            bool f = false;
            rep2(i, ind,  d + ind){
                if(!f && nums[i] == 4) rest--;
                if(!f && nums[i] == 7) cnt++;
                if(f && f2 < d / 2 && nums[i] == 7) f2++, nums[i] = 4;
                if(!f && (rest == 0 || cnt == (d / 2) - 1) && nums[i] == 4){
                    f2--;
                    nums[i] = 7;
                    f = true;
                }

            }
            rep2(i, ind, d + ind) cout << nums[i];
            cout << endl;
        }else{
            d += 2;
            rep(i, d / 2) cout << 4;
            rep(i, d / 2) cout << 7;
            cout << endl;
        }
    }else{
        rep2(i, ind, sz + 1) cout << nums[i];
        cout << endl;
    }
    return 0;
}


#define _USE_MATH_DEFINES
#include <iostream>
#include <complex>
#include <algorithm>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <cmath>
#include <math.h>
#include <numeric>
#include <list>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <climits>
#include <set>
#include <memory.h>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <map>
#include <cassert>
#include <time.h>
#include <ctime>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int , P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF = 1 << 27;
const double EPS = 1e-10;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;

#define rep(i, n) for(int i = 0; i < n; i++)
#define rep2(i, m, n) for(int i = m; i < n; i++)

int nums[200000];

int main(){
    string number;
    cin >> number;
    int sz = number.size();
    nums[0] = 0;
    rep(i, (int)number.size()) nums[i+1] = number[i] - '0';

    for(int i = sz; i > 0; i--){
        if(nums[i] > 7){
            nums[i] = 0;
            nums[i-1]++;
            int j = i;
            while(j > 0 && nums[j] >= 10){
                nums[j] -= 10;
                nums[--j]++;
            }
        }
    }
    int ind = 0;
    int four = 0;
    int seven = 0;

    while(nums[ind] == 0) ind++;
    rep2(i, ind, sz + 1){
        if(nums[i] <= 4){
            nums[i] = 4;
            four++;
        }
        else{
            nums[i] = 7;
            seven++;
        }
    }

    if((seven + four) % 2 == 1){
        int d = (seven + four + 1);
        rep(i, d / 2) cout << 4;
        rep(i, d / 2) cout << 7;
        cout << endl;
    }else if(four > seven){
        int dif = four - seven;
        for(int i = sz; i >= ind; i--){
            if(dif == 0) break;
            if(nums[i] == 4){
                nums[i] = 7;
                dif -= 2;
            }
        }
        rep2(i, ind, sz + 1) cout << nums[i];
        cout << endl;
    }else if(seven > four){
        int d = seven + four;
        bool ok = false;
        rep2(i, ind, d / 2 + ind) if(nums[i] == 4) ok = true;
        if(ok){
            int f2 = four;
            int cnt = 0;
            int rest = four;
            //cout << f2 << endl;
            bool f = false;
            rep2(i, ind,  d + ind){
                if(!f && nums[i] == 7) cnt++;
                if(!f && nums[i] == 4) rest--;
                if(f && f2 < d / 2 && nums[i] == 7) f2++, nums[i] = 4;
                if(!f && (rest == 0 || cnt == d / 2 - 1) && nums[i] == 4){
                    f2--;
                    nums[i] = 7;
                    f = true;
                }

            }
            rep2(i, ind, d + ind) cout << nums[i];
            cout << endl;
        }else{
            d += 2;
            rep(i, d / 2) cout << 4;
            rep(i, d / 2) cout << 7;
            cout << endl;
        }
    }else{
        rep2(i, ind, sz + 1) cout << nums[i];
        cout << endl;
    }
    return 0;
}


//+++++++++++++++++++++++++++++//
////      Karan Aggarwal         //
////       IIIT-Hyderabad        //
////+++++++++++++++++++++++++++++//
//// Topic:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<sstream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
#define PB push_back
#define MP make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define F first
#define S second
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,b) for(i=0;i<b;i++)
#define rep1(i,b) for(i=1;i<=b;i++)
#define mod 1000000007
#define pi(n) printf("%d",n)
#define pin(n) printf("%d\n",n)
#define piw(n) printf("%d ",n)
#define pll(n) printf("%lld",n)
#define plln(n) printf("%lld\n",n)
#define pllw(n) printf("%lld ",n)
#define sll(n) scanf("%lld",&n)
#define ss(s) scanf("%s",s)
#define ps(s) printf("%s",s)
#define psn(s) printf("%s\n",s)
#define psw(s) printf("%s ",s)
#define si(n) scanf("%d",&n)
#define pn printf("\n")
#define pw printf(" ")
/*
inline int ri()
{
    int n=0;char c;
    while(1)
    {
        c=getchar_unlocked();
        if(c=='\n'||c==' '||c==EOF)break;
        n=(n<<3) + (n<<1) +c-48;
    }
    return n;
}
inline long long rll()
{
    long long n=0;
    char c;
    while(1)
    {
        c=getchar_unlocked();
        if(c=='\n'||c==' '||c==EOF)break;
        n=(n<<3) + (n<<1) + c - 48;
    }
    return n;
}
*/
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
string number,n1="",n2="";
int main()
{
    int y,T,n,m,i,t,j,k,x;

    cin>>number;n = number.size();
    n += (n&1);

    int c4=n/2,c7=n/2;

    rep(i,n/2) n1 += '4';
    rep(i,n/2) n1 += '7';
    rep(i,n/2) n2 += '7';
    rep(i,n/2) n2 += '4';

    if(number.size()&1   || number<=n1  )
    {
        cout<<n1;return 0;
    }

    if(number>n2)
    {
        n1 = '4'+n1+'7';
        cout<<n1;return 0;
    }
    string tp="";
    rep(i,n)
        if(number[i]<='4' && c4)
            {
                c4--;tp+='4';
            }
            else
            { c7--;tp+='7';}
    cout<<tp;
    return 0;
}


//+++++++++++++++++++++++++++++//
////      Karan Aggarwal         //
////       IIIT-Hyderabad        //
////+++++++++++++++++++++++++++++//
//// Topic:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<sstream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
#define PB push_back
#define MP make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define F first
#define S second
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,b) for(i=0;i<b;i++)
#define rep1(i,b) for(i=1;i<=b;i++)
#define mod 1000000007
#define pi(n) printf("%d",n)
#define pin(n) printf("%d\n",n)
#define piw(n) printf("%d ",n)
#define pll(n) printf("%lld",n)
#define plln(n) printf("%lld\n",n)
#define pllw(n) printf("%lld ",n)
#define sll(n) scanf("%lld",&n)
#define ss(s) scanf("%s",s)
#define ps(s) printf("%s",s)
#define psn(s) printf("%s\n",s)
#define psw(s) printf("%s ",s)
#define si(n) scanf("%d",&n)
#define pn printf("\n")
#define pw printf(" ")
/*
   inline int ri()
   {
   int n=0;char c;
   while(1)
   {
   c=getchar_unlocked();
   if(c=='\n'||c==' '||c==EOF)break;
   n=(n<<3) + (n<<1) +c-48;
   }
   return n;
   }
   inline long long rll()
   {
   long long n=0;
   char c;
   while(1)
   {
   c=getchar_unlocked();
   if(c=='\n'||c==' '||c==EOF)break;
   n=(n<<3) + (n<<1) + c - 48;
   }
   return n;
   }
   */
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
string number,n1="",n2="";
PII dp[100005];
int main()
{
    int y,T,n,m,i,t,j,k,x;

    cin>>number;
    n = number.size();
    n += (n&1);

    int c4=n/2,c7=n/2;

    rep(i,n/2) n1 += '4';
    rep(i,n/2) n1 += '7';
    rep(i,n/2) n2 += '7';
    rep(i,n/2) n2 += '4';

    if(number.size()&1   || number<=n1  )
    {
        cout<<n1;return 0;
    }

    if(number>n2)
    {
        n1 = '4'+n1+'7';
        cout<<n1;return 0;
    }
    char tp[100005];
    int isalg=0;
    rep(i,n)
    {
//        piw(i);piw(isalg);
//        piw(c4);pin(c7);
        if(isalg)
        {
            if(c4)
            {
                tp[i]='4';c4--;
            }
            else
            {
                tp[i]='7';c7--;
            }
            continue;
        }
        else
        {
            dp[i] = MP(c4,c7);
            if(number[i]<'4')
            {
                isalg=1;
                if(c4){tp[i]='4';c4--;}
                else
                {tp[i]='7';c7--;}
                continue;
            }
            if(number[i]=='4')
            {
                if(c4){tp[i]='4';c4--;}
                else
                {
                    tp[i]='7';c7--;isalg=1;
                }
                continue;
            }
            if(number[i]>'4' && number[i]<'7' && c7)
            {
                tp[i]='7';isalg=1;c7--;
                continue;
            }

            if(c7 && number[i]=='7')
            {
                tp[i]='7';c7--;
            }
            else
            {
                for(j=i-1;j>=0;j--)
                {
                    if(number[j]<'7' && tp[j]=='4' && dp[j].S>0)
                    {
                        isalg=1;
                        tp[j]='7';
                        c7 = dp[j].S-1;
                        c4 = dp[j].F;
                        i = j;
                        break;
                    }
                }
            }
        }
    }
//        cout<<tp[3];
//    pn;
    rep(i,n)
        cout<<tp[i];
    return 0;
}


//+++++++++++++++++++++++++++++//
////      Karan Aggarwal         //
////       IIIT-Hyderabad        //
////+++++++++++++++++++++++++++++//
//// Topic:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<sstream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
#define PB push_back
#define MP make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define F first
#define S second
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,b) for(i=0;i<b;i++)
#define rep1(i,b) for(i=1;i<=b;i++)
#define mod 1000000007
#define pi(n) printf("%d",n)
#define pin(n) printf("%d\n",n)
#define piw(n) printf("%d ",n)
#define pll(n) printf("%lld",n)
#define plln(n) printf("%lld\n",n)
#define pllw(n) printf("%lld ",n)
#define sll(n) scanf("%lld",&n)
#define ss(s) scanf("%s",s)
#define ps(s) printf("%s",s)
#define psn(s) printf("%s\n",s)
#define psw(s) printf("%s ",s)
#define si(n) scanf("%d",&n)
#define pn printf("\n")
#define pw printf(" ")
/*
   inline int ri()
   {
   int n=0;char c;
   while(1)
   {
   c=getchar_unlocked();
   if(c=='\n'||c==' '||c==EOF)break;
   n=(n<<3) + (n<<1) +c-48;
   }
   return n;
   }
   inline long long rll()
   {
   long long n=0;
   char c;
   while(1)
   {
   c=getchar_unlocked();
   if(c=='\n'||c==' '||c==EOF)break;
   n=(n<<3) + (n<<1) + c - 48;
   }
   return n;
   }
   */
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
string number,n1="",n2="";
PII dp[100005];
int main()
{
    int y,T,n,m,i,t,j,k,x;

    cin>>number;n = number.size();
    n += (n&1);

    int c4=n/2,c7=n/2;

    rep(i,n/2) n1 += '4';
    rep(i,n/2) n1 += '7';
    rep(i,n/2) n2 += '7';
    rep(i,n/2) n2 += '4';

    if(number.size()&1   || number<=n1  )
    {
        cout<<n1;return 0;
    }

    if(number>n2)
    {
        n1 = '4'+n1+'7';
        cout<<n1;return 0;
    }
    char tp[100005];
    int isalg=0;
    rep(i,n)
    {
//        piw(i);piw(isalg);
//        piw(c4);pin(c7);
        if(isalg)
        {
            if(c4)
            {
                tp[i]='4';c4--;
            }
            else
            {
                tp[i]='7';c7--;
            }
            continue;
        }
        else
        {
            dp[i] = MP(c4,c7);
            if(number[i]<'4')
            {
                isalg=1;
                if(c4){tp[i]='4';c4--;}
                else
                {tp[i]='7';c7--;}
            }
            if(number[i]=='4')
            {
                if(c4){tp[i]='4';c4--;}
                else
                {
                    tp[i]='7';c7--;isalg=1;
                }
            }
            if(number[i]>'4' && number[i]<'7' && c7)
            {
                tp[i]='7';isalg=1;c7--;
            }
            if(c7 && tp[i]=='7')
            {
                tp[i]='7';c7--;
            }
            else
            {
                for(j=i-1;j>=0;j--)
                {
                    if(number[j]<'7' && tp[j]=='4' && dp[j].S>0)
                    {
                        isalg=1;
                        tp[j]='7';
                        c7 = dp[j].S-1;
                        c4 = dp[j].F;
                        i = j;
                        break;
                    }
                }
            }
        }
    }
//        cout<<tp[3];
//    pn;
    rep(i,n)
        cout<<tp[i];
    return 0;
}


//+++++++++++++++++++++++++++++//
////      Karan Aggarwal         //
////       IIIT-Hyderabad        //
////+++++++++++++++++++++++++++++//
//// Topic:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<sstream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
#define PB push_back
#define MP make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define F first
#define S second
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,b) for(i=0;i<b;i++)
#define rep1(i,b) for(i=1;i<=b;i++)
#define mod 1000000007
#define pi(n) printf("%d",n)
#define pin(n) printf("%d\n",n)
#define piw(n) printf("%d ",n)
#define pll(n) printf("%lld",n)
#define plln(n) printf("%lld\n",n)
#define pllw(n) printf("%lld ",n)
#define sll(n) scanf("%lld",&n)
#define ss(s) scanf("%s",s)
#define ps(s) printf("%s",s)
#define psn(s) printf("%s\n",s)
#define psw(s) printf("%s ",s)
#define si(n) scanf("%d",&n)
#define pn printf("\n")
#define pw printf(" ")
/*
   inline int ri()
   {
   int n=0;char c;
   while(1)
   {
   c=getchar_unlocked();
   if(c=='\n'||c==' '||c==EOF)break;
   n=(n<<3) + (n<<1) +c-48;
   }
   return n;
   }
   inline long long rll()
   {
   long long n=0;
   char c;
   while(1)
   {
   c=getchar_unlocked();
   if(c=='\n'||c==' '||c==EOF)break;
   n=(n<<3) + (n<<1) + c - 48;
   }
   return n;
   }
   */
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
string number,n1="",n2="";
PII dp[100005];
int main()
{
    int y,T,n,m,i,t,j,k,x;

    cin>>number;n = number.size();
    n += (n&1);

    int c4=n/2,c7=n/2;

    rep(i,n/2) n1 += '4';
    rep(i,n/2) n1 += '7';
    rep(i,n/2) n2 += '7';
    rep(i,n/2) n2 += '4';

    if(number.size()&1   || number<=n1  )
    {
        cout<<n1;return 0;
    }

    if(number>n2)
    {
        n1 = '4'+n1+'7';
        cout<<n1;return 0;
    }
    char tp[100005];
    int isalg=0;
    rep(i,n)
    {
//        piw(i);piw(isalg);
//        piw(c4);pin(c7);
        if(isalg)
        {
            if(c4)
            {
                tp[i]='4';c4--;
            }
            else
            {
                tp[i]='7';c7--;
            }
            continue;
        }
        else
        {
            dp[i] = MP(c4,c7);
            if(number[i]<'4')
            {
                isalg=1;
                if(c4){tp[i]='4';c4--;}
                else
                {tp[i]='7';c7--;}
            }
            if(number[i]=='4')
            {
                if(c4){tp[i]='4';c4--;}
                else
                {
                    tp[i]='7';c7--;isalg=1;
                }
            }
            if(number[i]>'4' && number[i]<'7' && c7)
            {
                tp[i]='7';isalg=1;c7--;
            }
            if(c7 && tp[i]=='7')
            {
                tp[i]='7';c7--;
            }
            else
            {
                for(j=i-1;j>=0;j--)
                {
                    if(number[j]<'7' && tp[j]=='4' && dp[j].S>0)
                    {
                        isalg=1;
                        tp[j]='7';
                        c7 = dp[j].S-1;
                        c4 = dp[j].F;
                        i = j;
                        break;
                    }
                }
            }
        }
    }
//        cout<<tp[3];
//    pn;
    tp[n]='\0';
    tp[n+1]='\0';
    ps(tp);
    return 0;
}


//+++++++++++++++++++++++++++++//
////      Karan Aggarwal         //
////       IIIT-Hyderabad        //
////+++++++++++++++++++++++++++++//
//// Topic:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<sstream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
#define PB push_back
#define MP make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define F first
#define S second
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,b) for(i=0;i<b;i++)
#define rep1(i,b) for(i=1;i<=b;i++)
#define mod 1000000007
#define pi(n) printf("%d",n)
#define pin(n) printf("%d\n",n)
#define piw(n) printf("%d ",n)
#define pll(n) printf("%lld",n)
#define plln(n) printf("%lld\n",n)
#define pllw(n) printf("%lld ",n)
#define sll(n) scanf("%lld",&n)
#define ss(s) scanf("%s",s)
#define ps(s) printf("%s",s)
#define psn(s) printf("%s\n",s)
#define psw(s) printf("%s ",s)
#define si(n) scanf("%d",&n)
#define pn printf("\n")
#define pw printf(" ")
/*
   inline int ri()
   {
   int n=0;char c;
   while(1)
   {
   c=getchar_unlocked();
   if(c=='\n'||c==' '||c==EOF)break;
   n=(n<<3) + (n<<1) +c-48;
   }
   return n;
   }
   inline long long rll()
   {
   long long n=0;
   char c;
   while(1)
   {
   c=getchar_unlocked();
   if(c=='\n'||c==' '||c==EOF)break;
   n=(n<<3) + (n<<1) + c - 48;
   }
   return n;
   }
   */
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
string number,n1="",n2="";
PII dp[100005];
int main()
{
    int y,T,n,m,i,t,j,k,x;

    cin>>number;n = number.size();
    n += (n&1);

    int c4=n/2,c7=n/2;

    rep(i,n/2) n1 += '4';
    rep(i,n/2) n1 += '7';
    rep(i,n/2) n2 += '7';
    rep(i,n/2) n2 += '4';

    if(number.size()&1   || number<=n1  )
    {
        cout<<n1;return 0;
    }

    if(number>n2)
    {
        n1 = '4'+n1+'7';
        cout<<n1;return 0;
    }
    char tp[100005];
    int isalg=0;
    rep(i,n)
    {
//        piw(i);piw(isalg);
//        piw(c4);pin(c7);
        if(isalg)
        {
            if(c4)
            {
                tp[i]='4';c4--;
            }
            else
            {
                tp[i]='7';c7--;
            }
            continue;
        }
        else
        {
            dp[i] = MP(c4,c7);
            if(number[i]<'4')
            {
                isalg=1;
                if(c4){tp[i]='4';c4--;}
                else
                {tp[i]='7';c7--;}
            }
            if(number[i]=='4')
            {
                if(c4){tp[i]='4';c4--;}
                else
                {
                    tp[i]='7';c7--;isalg=1;
                }
            }
            if(number[i]>'4' && number[i]<'7' && c7)
            {
                tp[i]='7';isalg=1;c7--;
            }
            if(c7 && tp[i]=='7')
            {
                tp[i]='7';c7--;
            }
            else
            {
                for(j=i-1;j>=0;j--)
                {
                    if(number[j]<'7' && tp[j]=='4' && dp[j].S>0)
                    {
                        isalg=1;
                        tp[j]='7';
                        c7 = dp[j].S-1;
                        c4 = dp[j].F;
                        i = j+1;
                        break;
                    }
                }
            }
        }
    }
//        cout<<tp[3];
//    pn;
    tp[n]='\0';
    tp[n+1]='\0';
    ps(tp);
    return 0;
}


//+++++++++++++++++++++++++++++//
////      Karan Aggarwal         //
////       IIIT-Hyderabad        //
////+++++++++++++++++++++++++++++//
//// Topic:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<sstream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
#define PB push_back
#define MP make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define F first
#define S second
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,b) for(i=0;i<b;i++)
#define rep1(i,b) for(i=1;i<=b;i++)
#define mod 1000000007
#define pi(n) printf("%d",n)
#define pin(n) printf("%d\n",n)
#define piw(n) printf("%d ",n)
#define pll(n) printf("%lld",n)
#define plln(n) printf("%lld\n",n)
#define pllw(n) printf("%lld ",n)
#define sll(n) scanf("%lld",&n)
#define ss(s) scanf("%s",s)
#define ps(s) printf("%s",s)
#define psn(s) printf("%s\n",s)
#define psw(s) printf("%s ",s)
#define si(n) scanf("%d",&n)
#define pn printf("\n")
#define pw printf(" ")
/*
   inline int ri()
   {
   int n=0;char c;
   while(1)
   {
   c=getchar_unlocked();
   if(c=='\n'||c==' '||c==EOF)break;
   n=(n<<3) + (n<<1) +c-48;
   }
   return n;
   }
   inline long long rll()
   {
   long long n=0;
   char c;
   while(1)
   {
   c=getchar_unlocked();
   if(c=='\n'||c==' '||c==EOF)break;
   n=(n<<3) + (n<<1) + c - 48;
   }
   return n;
   }
   */
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
string number,n1="",n2="";
PII dp[100005];
int main()
{
    int y,T,n,m,i,t,j,k,x;

    cin>>number;
    n = number.size();
    n += (n&1);

    int c4=n/2,c7=n/2;

    rep(i,n/2) n1 += '4';
    rep(i,n/2) n1 += '7';
    rep(i,n/2) n2 += '7';
    rep(i,n/2) n2 += '4';

    if(number.size()&1   || number<=n1  )
    {
        cout<<n1;return 0;
    }

    if(number>n2)
    {
        n1 = '4'+n1+'7';
        cout<<n1;return 0;
    }
    char tp[100005];
    int isalg=0;
    rep(i,n)
    {
//        piw(i);piw(isalg);
//        piw(c4);pin(c7);
        if(isalg)
        {
            if(c4)
            {
                tp[i]='4';c4--;
            }
            else
            {
                tp[i]='7';c7--;
            }
            continue;
        }
        else
        {
            dp[i] = MP(c4,c7);
            if(number[i]<'4')
            {
                isalg=1;
                if(c4){tp[i]='4';c4--;}
                else
                {tp[i]='7';c7--;}
            }
            if(number[i]=='4')
            {
                if(c4){tp[i]='4';c4--;}
                else
                {
                    tp[i]='7';c7--;isalg=1;
                }
            }
            if(number[i]>'4' && number[i]<'7' && c7)
            {
                tp[i]='7';isalg=1;c7--;
            }
            if(c7 && number[i]=='7')
            {
                tp[i]='7';c7--;
            }
            else
            {
                for(j=i-1;j>=0;j--)
                {
                    if(number[j]<'7' && tp[j]=='4' && dp[j].S>0)
                    {
                        isalg=1;
                        tp[j]='7';
                        c7 = dp[j].S-1;
                        c4 = dp[j].F;
                        i = j;
                        break;
                    }
                }
            }
        }
    }
//        cout<<tp[3];
//    pn;
    rep(i,n)
        cout<<tp[i];
    return 0;
}


//+++++++++++++++++++++++++++++//
////      Karan Aggarwal         //
////       IIIT-Hyderabad        //
////+++++++++++++++++++++++++++++//
//// Topic:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<sstream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
#define PB push_back
#define MP make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define F first
#define S second
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,b) for(i=0;i<b;i++)
#define rep1(i,b) for(i=1;i<=b;i++)
#define mod 1000000007
#define pi(n) printf("%d",n)
#define pin(n) printf("%d\n",n)
#define piw(n) printf("%d ",n)
#define pll(n) printf("%lld",n)
#define plln(n) printf("%lld\n",n)
#define pllw(n) printf("%lld ",n)
#define sll(n) scanf("%lld",&n)
#define ss(s) scanf("%s",s)
#define ps(s) printf("%s",s)
#define psn(s) printf("%s\n",s)
#define psw(s) printf("%s ",s)
#define si(n) scanf("%d",&n)
#define pn printf("\n")
#define pw printf(" ")
/*
inline int ri()
{
    int n=0;char c;
    while(1)
    {
        c=getchar_unlocked();
        if(c=='\n'||c==' '||c==EOF)break;
        n=(n<<3) + (n<<1) +c-48;
    }
    return n;
}
inline long long rll()
{
    long long n=0;
    char c;
    while(1)
    {
        c=getchar_unlocked();
        if(c=='\n'||c==' '||c==EOF)break;
        n=(n<<3) + (n<<1) + c - 48;
    }
    return n;
}
*/
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
string number,n1="",n2="";
int main()
{
    int y,T,n,m,i,t,j,k,x;

    cin>>number;n = number.size();
    n += (n&1);

    int c4=n/2,c7=n/2;

    rep(i,n/2) n1 += '4';
    rep(i,n/2) n1 += '7';
    rep(i,n/2) n2 += '7';
    rep(i,n/2) n2 += '4';

    if(number.size()&1   || number<=n1  )
    {
        cout<<n1;return 0;
    }

    if(number>n2)
    {
        n1 = '4'+n1+'7';
        cout<<n1;return 0;
    }
    string tp="";
    int isalg=0;
    rep(i,n)
        if((isalg || number[i]<='4') && c4)
            {
                c4--;tp+='4';
                if(number[i]<'4')isalg=1;
            }
            else
            { c7--;tp+='7';
                if(number[i]<'7')
                    isalg=1;
            }
    cout<<tp;
    return 0;
}


//+++++++++++++++++++++++++++++//
////      Karan Aggarwal         //
////       IIIT-Hyderabad        //
////+++++++++++++++++++++++++++++//
//// Topic:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<vector>
#include<sstream>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<algorithm>
using namespace std;
#define PB push_back
#define MP make_pair
#define clr(x) x.clear()
#define sz(x) ((int)(x).size())
#define F first
#define S second
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,b) for(i=0;i<b;i++)
#define rep1(i,b) for(i=1;i<=b;i++)
#define mod 1000000007
#define pi(n) printf("%d",n)
#define pin(n) printf("%d\n",n)
#define piw(n) printf("%d ",n)
#define pll(n) printf("%lld",n)
#define plln(n) printf("%lld\n",n)
#define pllw(n) printf("%lld ",n)
#define sll(n) scanf("%lld",&n)
#define ss(s) scanf("%s",s)
#define ps(s) printf("%s",s)
#define psn(s) printf("%s\n",s)
#define psw(s) printf("%s ",s)
#define si(n) scanf("%d",&n)
#define pn printf("\n")
#define pw printf(" ")
/*
   inline int ri()
   {
   int n=0;char c;
   while(1)
   {
   c=getchar_unlocked();
   if(c=='\n'||c==' '||c==EOF)break;
   n=(n<<3) + (n<<1) +c-48;
   }
   return n;
   }
   inline long long rll()
   {
   long long n=0;
   char c;
   while(1)
   {
   c=getchar_unlocked();
   if(c=='\n'||c==' '||c==EOF)break;
   n=(n<<3) + (n<<1) + c - 48;
   }
   return n;
   }
   */
typedef pair<int,int> PII;
typedef vector<PII> VPII;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef long long LL;
string number,n1="",n2="";
PII dp[100005];
int main()
{
    int y,T,n,m,i,t,j,k,x;

    cin>>number;n = number.size();
    n += (n&1);

    int c4=n/2,c7=n/2;

    rep(i,n/2) n1 += '4';
    rep(i,n/2) n1 += '7';
    rep(i,n/2) n2 += '7';
    rep(i,n/2) n2 += '4';

    if(number.size()&1   || number<=n1  )
    {
        cout<<n1;return 0;
    }

    if(number>n2)
    {
        n1 = '4'+n1+'7';
        cout<<n1;return 0;
    }
    char tp[100005];
    int isalg=0;
    rep(i,n)
    {
        if(isalg)
        {
            if(c4)
            {
                tp[i]='4';c4--;
            }
            else
                if(c7)
                {
                    tp[i]='7';c7--;
                }
        }
        else
        {
            dp[i] = MP(c4,c7);
            if(number[i]<'4')
            {
                isalg=1;
                if(c4){tp[i]='4';c4--;}
                else
                {tp[i]='7';c7--;}
            }
            if(number[i]=='4')
            {
                if(c4){tp[i]='4';c4--;}
                else
                {
                    tp[i]='7';c7--;isalg=1;
                }
            }
            if(number[i]>'4' && number[i]<'7' && c7)
            {
                tp[i]='7';isalg=1;c7--;
            }
            if(c7 && tp[i]=='7')
            {
                tp[i]='7';c7--;
            }
            else
            {
                for(j=i-1;j>=0;j--)
                {
                    if(number[j]<'7' && tp[j]=='4' && dp[j].S>0)
                    {
                        isalg=1;
                        tp[j]='7';
                        c7 = dp[j].S-1;
                        c4 = dp[j].F;
                        i = j+1;
                        break;
                    }
                }
            }
        }
    }
    ps(tp);
    return 0;
}


// by shik
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#define N 100010
using namespace std;
string gen( int n, string s ) {
    string ret;
    while ( n-- ) ret+=s;
    return ret;
}
int d4[N],d7[N];
int main()
{
    string s;
    cin>>s;
    int n=s.size();
    d4[n]=d7[n]=n;
    for ( int i=n-1; i>=0; i-- ) {
        if ( s[i]!=4 ) d4[i]=i;
        else d4[i]=d4[i+1];
        if ( s[i]!=7 ) d7[i]=i;
        else d7[i]=d7[i+1];
    }
    if ( n%2==0 ) {
        string t1=gen(n/2,"7")+gen(n/2,"4");
        if ( t1<s ) {
            cout<<gen(n/2+1,"4")<<gen(n/2+1,"7")<<endl;
        } else {
            string now="";
            int flg=0,r4=n/2,r7=n/2;
            for ( int i=0; i<n; i++ ) {
                int tmt=0,p;
                if ( r4>0 ) {
                    if ( flg || s[i]<'4' ) tmt=1;
                    else if ( s[i]=='4' ) {
                        p=i+1;
                        while ( p<=n ) {
                            if ( p==n ) {
                                tmt=1;
                                break;
                            }
                            char c;
                            if ( p<=i+r7 ) c='7';
                            else c='4';
                            if ( s[p]<c ) tmt=1;
                            if ( s[p]!=c ) break;
                            if ( s[p]=='4' ) p=max(p+1,d4[p]);
                            else if ( s[p]=='7' ) p=max(p+1,min(d7[p],i+r7));
                        }
                    }
                }
                if ( tmt ) now+="4",r4--;
                else now+="7",r7--;
                if ( now[i]>s[i] ) flg=1;
            }
            cout<<now<<endl;
        }
    } else {
        n++;
        cout<<gen(n/2,"4")<<gen(n/2,"7")<<endl;
    }
    return 0;
}


// by shik
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#define N 100010
using namespace std;
string gen( int n, string s ) {
	string ret;
	while ( n-- ) ret+=s;
	return ret;
}
int d4[N],d7[N];
int main()
{
	string s;
	cin>>s;
	int n=s.size();
	d4[n]=d7[n]=n;
	for ( int i=n-1; i>=0; i-- ) {
		if ( s[i]!=4 ) d4[i]=i;
		else d4[i]=d4[i+1];
		if ( s[i]!=7 ) d7[i]=i;
		else d7[i]=d7[i+1];
	}
	if ( n%2==0 ) {
		string t1=gen(n/2,"7")+gen(n/2,"4");
		if ( t1<s ) {
			cout<<gen(n/2+1,"4")<<gen(n/2+1,"7")<<endl;
		} else {
			string now="";
			int flg=0,r4=n/2,r7=n/2;
			for ( int i=0; i<n; i++ ) {
				int tmt=0,p;
				if ( r4>0 ) {
					if ( flg || s[i]<'4' ) tmt=1;
					else if ( s[i]=='4' ) {
						p=i+1;
						while ( p<n ) {
							char c;
							if ( p<=i+r7 ) c='7';
							else c='4';
							if ( s[p]<c ) tmt=1;
							if ( s[p]!=c ) break;
						}
					}
				}
				if ( tmt ) now+="4",r4--;
				else now+="7",r7--;
				if ( now[i]>s[i] ) flg=1;
			}
			cout<<now<<endl;
		}
	} else {
		n++;
		cout<<gen(n/2,"4")<<gen(n/2,"7")<<endl;
	}
	return 0;
}


// by shik
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#define N 100010
using namespace std;
string gen( int n, string s ) {
	string ret;
	while ( n-- ) ret+=s;
	return ret;
}
int d4[N],d7[N];
int main()
{
	string s;
	cin>>s;
	int n=s.size();
	d4[n]=d7[n]=n;
	for ( int i=n-1; i>=0; i-- ) {
		if ( s[i]!=4 ) d4[i]=i;
		else d4[i]=d4[i+1];
		if ( s[i]!=7 ) d7[i]=i;
		else d7[i]=d7[i+1];
	}
	if ( n%2==0 ) {
		string t1=gen(n/2,"7")+gen(n/2,"4");
		if ( t1<s ) {
			cout<<gen(n/2+1,"4")<<gen(n/2+1,"7")<<endl;
		} else {
			string now="";
			int flg=0,r4=n/2,r7=n/2;
			for ( int i=0; i<n; i++ ) {
				int tmt=0,p;
				if ( r4>0 ) {
					if ( flg || s[i]<'4' ) tmt=1;
					else if ( s[i]=='4' ) {
						p=i+1;
						while ( p<n ) {
							char c;
							if ( p<=i+r7 ) c='7';
							else c='4';
							if ( s[i]<c ) tmt=1;
							if ( s[i]!=c ) break;
						}
					}
				}
				if ( tmt ) now+="4",r4--;
				else now+="7",r7--;
				if ( now[i]>s[i] ) flg=1;
			}
			cout<<now<<endl;
		}
	} else {
		n++;
		cout<<gen(n/2,"4")<<gen(n/2,"7")<<endl;
	}
	return 0;
}


// by shik
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
int main()
{
	string s,t;
	cin>>s;
	int n=s.size();
	if ( n%2==0 ) {
		if ( string(n/2,'7')+string(n/2,'4')<s ) cout<<string(n/2+1,'4')<<string(n/2+1,'4')<<endl;
		else {
			int flg=0,r4=n/2,r7=n/2;
			for ( int i=0; i<n; i++ ) {
				int tmt=0,p;
				if ( r4>0 ) {
					if ( flg || s[i]<'4' ) tmt=1;
					else if ( s[i]=='4' ) {
						p=i+1;
						for ( p=i+1; p<=n; p++ ) {
							if ( p==n ) { tmt=1; break; }
							char c=(p<=i+r7?'7':'4');
							if ( c>s[p] ) tmt=1;
							if ( c!=s[p] ) break;
						}
					}
				}
				if ( tmt ) t+="4",r4--;
				else t+="7",r7--;
				if ( t[i]>s[i] ) flg=1;
			}
			cout<<t<<endl;
		}
	} else {
		n++;
		cout<<string(n/2,'4')<<string(n/2,'7')<<endl;
	}
	return 0;
}


// by shik
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#define N 100010
using namespace std;
string gen( int n, string s ) {
	string ret;
	while ( n-- ) ret+=s;
	return ret;
}
int d4[N],d7[N];
int main()
{
	string s;
	cin>>s;
	int n=s.size();
	d4[n]=d7[n]=n;
	for ( int i=n-1; i>=0; i-- ) {
		if ( s[i]!=4 ) d4[i]=i;
		else d4[i]=d4[i+1];
		if ( s[i]!=7 ) d7[i]=i;
		else d7[i]=d7[i+1];
	}
	if ( n%2==0 ) {
		string t1=gen(n/2,"7")+gen(n/2,"4");
		if ( t1<s ) {
			cout<<gen(n/2+1,"4")<<gen(n/2+1,"7")<<endl;
		} else {
			string now="";
			int flg=0,r4=n/2,r7=n/2;
			for ( int i=0; i<n; i++ ) {
				int tmt=0,p;
				if ( r4>0 ) {
					if ( flg || s[i]<'4' ) tmt=1;
					else if ( s[i]=='4' ) {
						p=i+1;
						while ( p<=n ) {
							if ( p==n ) {
								tmt=1;
								break;
							}
							char c;
							if ( p<=i+r7 ) c='7';
							else c='4';
							if ( s[p]<c ) tmt=1;
							if ( s[p]!=c ) break;
							if ( s[p]=='4' ) p=d4[p];
							else if ( s[p]=='7' ) p=max(p+1,min(d7[p],i+r7));
						}
					}
				}
				if ( tmt ) now+="4",r4--;
				else now+="7",r7--;
				if ( now[i]>s[i] ) flg=1;
			}
			cout<<now<<endl;
		}
	} else {
		n++;
		cout<<gen(n/2,"4")<<gen(n/2,"7")<<endl;
	}
	return 0;
}


// by shik
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
using namespace std;
int main()
{
	string s,t;
	cin>>s;
	int n=s.size();
	if ( n%2==0 ) {
		if ( string(n/2,'7')+string(n/2,'4')<s ) cout<<string(n/2+1,'4')<<string(n/2+1,'7')<<endl;
		else {
			int flg=0,r4=n/2,r7=n/2;
			for ( int i=0; i<n; i++ ) {
				int tmt=0,p;
				if ( r4>0 ) {
					if ( flg || s[i]<'4' ) tmt=1;
					else if ( s[i]=='4' ) {
						p=i+1;
						for ( p=i+1; p<=n; p++ ) {
							if ( p==n ) { tmt=1; break; }
							char c=(p<=i+r7?'7':'4');
							if ( c>s[p] ) tmt=1;
							if ( c!=s[p] ) break;
						}
					}
				}
				if ( tmt ) t+="4",r4--;
				else t+="7",r7--;
				if ( t[i]>s[i] ) flg=1;
			}
			cout<<t<<endl;
		}
	} else {
		n++;
		cout<<string(n/2,'4')<<string(n/2,'7')<<endl;
	}
	return 0;
}


// by shik
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <iostream>
#define N 100010
using namespace std;
string gen( int n, string s ) {
	string ret;
	while ( n-- ) ret+=s;
	return ret;
}
int d4[N],d7[N];
int main()
{
	string s;
	cin>>s;
	int n=s.size();
	d4[n]=d7[n]=n;
	for ( int i=n-1; i>=0; i-- ) {
		if ( s[i]!=4 ) d4[i]=i;
		else d4[i]=d4[i+1];
		if ( s[i]!=7 ) d7[i]=i;
		else d7[i]=d7[i+1];
	}
	if ( n%2==0 ) {
		string t1=gen(n/2,"7")+gen(n/2,"4");
		if ( t1<s ) {
			cout<<gen(n/2+1,"4")<<gen(n/2+1,"7")<<endl;
		} else {
			string now="";
			int flg=0,r4=n/2,r7=n/2;
			for ( int i=0; i<n; i++ ) {
				int tmt=0,p;
				if ( r4>0 ) {
					if ( flg || s[i]<'4' ) tmt=1;
					else if ( s[i]=='4' ) {
						p=i+1;
						while ( p<n ) {
							char c;
							if ( p<=i+r7 ) c='7';
							else c='4';
							if ( s[p]<c ) tmt=1;
							if ( s[p]!=c ) break;
							if ( s[p]=='4' ) p=d4[p];
							else if ( s[p]=='7' ) p=max(p+1,min(d7[p],i+r7));
						}
					}
				}
				if ( tmt ) now+="4",r4--;
				else now+="7",r7--;
				if ( now[i]>s[i] ) flg=1;
			}
			cout<<now<<endl;
		}
	} else {
		n++;
		cout<<gen(n/2,"4")<<gen(n/2,"7")<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char n[100005];
char ans[100005];
int len;

bool dfs(int pos,int cnt4,int cnt7,bool toup)
{
	if(pos==len) return true;
	if(toup)
	{
		for(int i=0;i<cnt4;i++)
			ans[pos++]='4';
		for(int i=0;i<cnt7;i++)
			ans[pos++]='7';
		return true;
	}
	if(n[pos]<='4'&&cnt4)
	{
		if(dfs(pos+1,cnt4-1,cnt7,n[pos]<'4'))
		{
			ans[pos]='4';
			return true;
		}
	}
	if(n[pos]<='7'&&cnt7)
	{
		if(dfs(pos+1,cnt4,cnt7-1,n[pos]<'7'))
		{
			ans[pos]='7';
			return true;
		}
	}
	return false;
}

int main()
{
	while(cin>>n)
	{
		len=strlen(n);
		if(len%2||!dfs(0,len/2,len/2,false))
		{
			//cout<<1<<endl;
			int x=(len%2?(len+1)/2:(len+2)/2);
			for(int i=1;i<=x;i++)
				cout<<4;
			for(int i=1;i<=x;i++)
				cout<<7;
			cout<<endl;
			continue;
		}
		cout<<ans<<endl;
	}
	return 0;
}


#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cstdio>
#define inf 0x3f3f3f3f
using namespace std;
char s[100005];
int main()
{
	while(cin>>s)
	{
		char *p=new char[100005];
		int len=strlen(s);
		if(len%2)
		{
			for(int i=1;i<=(len+1)/2;i++)
				cout<<4;
			for(int i=1;i<=(len+1)/2;i++)
				cout<<7;
				cout<<endl;
		}
		else
		{
			for(int i=0;i<len/2;i++)
				p[i]='4';
			for(int i=len/2;i<len;i++)
				p[i]='7';
			p[len]='\0';
			int flog=0;
			do
			{
				for(int i=0;i<len;i++)
				{
					if(s[i]<p[i])
					{
						flog=1;break;
					}
					else if(s[i]==p[i])
					{
						if(i==len-1) flog=1;
						continue;
					}
					else if(s[i]>p[i])
					{
						flog=2;break;
					}
				}
				if(flog==1) break;
				else if(flog==2)
				{
					 flog=0;continue;
				}
			}while(next_permutation(p,p+len));
			if(!flog)
			{
				for(int i=1;i<=(len+2)/2;i++)
					cout<<4;
				for(int i=1;i<=(len+2)/2;i++)
					cout<<7;
					cout<<endl;
			}
			else cout<<p<<endl;
		}
	}
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char n[100005];

int main()
{
	while(cin>>n)
	{
		int len=strlen(n);
		if(len%2)
		{
			for(int i=1;i<=(len+1)/2;i++)
				cout<<4;
			for(int i=1;i<=(len+1)/2;i++)
				cout<<7;
			cout<<endl;
			continue;
		}
		char s[100005];
		int flag=0;
		for(int i=0;i<len/2;i++)
			s[i]='4';
		for(int i=len/2;i<len;i++)
			s[i]='7';
		do{
			for(int i=0;i<len;i++)
			{
				if(n[i]<s[i]||(i==len-1&&s[i]==n[i]))
				{
					flag=1;
					for(int i=0;i<len;i++)
						cout<<s[i];
					cout<<endl;
					break;
				}
				if(n[i]>s[i])
					break;
			}
			if(flag)
				break;
		}while(next_permutation(s,s+len));
		if(flag==0)
		{
			for(int i=1;i<=(len+2)/2;i++)
				cout<<4;
			for(int i=1;i<=(len+2)/2;i++)
				cout<<7;
			cout<<endl;
		}
	}
	return 0;
}


#include <cstdio>

#include <cmath>

#include <cstring>

#include <algorithm>

using namespace std;

void pr(int l)

{

	for (int i = 1 ; i <= l ; i++)

		printf ("4");

	for (int i = 1 ; i <= l ; i++)

		printf ("7");

	printf ("\n");

}

int main()

{

	char a[15];

	char t[15];

	int i;

	int l;		//位数

	scanf ("%s",a);

	l = strlen(a);

	if (l & 1)		//l为奇数时，扩充一位就行

	{

		l = (l + 1) >> 1;

		pr(l);

	}

	else

	{

		for (i = 0 ; i < (l / 2) ; i++)		//先构造最大的

			t[i] = '7';

		for ( ; i < l ; i++)

			t[i] = '4';

		int r = strcmp(a , t);

		if (r == 1)

			pr ((l + 2) >> 1);

		else

		{

			for (i = 0 ; i < (l / 2) ; i++)		//再从最小的开始

				t[i] = '4';

			for ( ; i < l ; i++)

				t[i] = '7';

			do

			{

				r = strcmp(a , t);

				if (r == 0 || r == -1)		//比它小，就输出，否则字典序递增

				{

					for (i = 0 ; i < l ; i++)		//为什么t[i]!='\0'提交到cf就多了个@呢！！

						printf ("%c",t[i]);

					printf ("\n");

					break;

				}

			}while (next_permutation(t , t + l));

		}

	}

	return 0;

}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
char n[100005];
char ans[100005];
int len;

bool dfs(int pos,int cnt4,int cnt7,bool toup)
{
	if(pos==len) return true;
	if(toup)
	{
		for(int i=0;i<cnt4;i++)
			ans[pos++]='4';
		for(int i=0;i<cnt7;i++)
			ans[pos++]='7';
		return true;
	}
	if(n[pos]<='4'&&cnt4)
	{
		if(dfs(pos+1,cnt4-1,cnt7,n[pos]<4))
		{
			ans[pos]='4';
			return true;
		}
	}
	if(n[pos]<='7'&&cnt7)
	{
		if(dfs(pos+1,cnt4,cnt7-1,n[pos]<7))
		{
			ans[pos]='7';
			return true;
		}
	}
	return false;
}

int main()
{
	while(cin>>n)
	{
		len=strlen(n);
		if(len%2||!dfs(0,len/2,len/2,false))
		{
			int x=(len%2?(len+1)/2:(len+2)/2);
			for(int i=1;i<=x;i++)
				cout<<4;
			for(int i=1;i<=x;i++)
				cout<<7;
			cout<<endl;
			continue;
		}
		cout<<ans<<endl;
	}
	return 0;
}


//Author: Sharad Gupta
#include<iostream>
#include<sstream>
#include<cstdio>
#include<climits>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<deque>
#include<bitset>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<vector>
#include<queue>

using namespace std;

#define VI vector < int >
#define VVI(A,N,M) vector< VI > A( N, VI (M) )
#define LL long long
#define LLU unsigned long long
#define SI ({int x;scanf("%d",&x);x;})
#define SC ({char x;scanf("%c",&x);x;})
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int) (x).size())
#define SORT(c) sort(ALL(c))
#define FIT(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); it++)
#define FITD(it,v) for (typeof(v.rbegin()) it = v.rbegin(); it != v.rend(); it++)
#define FOR(i,start,end) for(int i=start;i<end;i++)
#define IATOV(a) ({vector<int> v(a,a+sizeof(a)/sizeof(int));v;})
#define CATOV(a) ({vector<char> v(a,a+sizeof(a)/sizeof(char));v;})
#define sieve(a) ({int b=ceil(sqrt(a));VI d(a,0);VI e;int f=2;e.pb(2);e.pb(3);for(int x=1;x<b+1;x++){for(int y=1;y<b+1;y++){int n=(4*x*x)+(y*y);if(n<=a&&(n%12==1||n%12==5)){d[n]^=1;}n=(3*x*x)+(y*y);if(n<=a&&n%12==7){d[n]^=1;}n=(3*x*x)-(y*y);if(x>y&&n<=a&&n%12==11){d[n]^=1;}}}for(int r=5;r<b+1;r++){if(d[r]){for(int i=r*r;i<a;i+=(r*r)){d[i]=0;}}}for(int c=5;c<a;c++){if(d[c]){e.pb(c);}}e;})
#define INF 1000000007
#define EPS 1e-9
#define mt(x, y, z) mp(mp(x,y),z)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define setbits(x) __builtin_popcount(x) //tell number of 1's in binary form
#define GC getchar_unlocked
template <class T> string str(T Number){

			string Result; // string which will contain the result

						ostringstream convert; // stream used for the conversion

										convert << Number; // insert the textual representation of 'Number' in the characters in the stream

															Result = convert.str();
																					return Result;
}
int StringToNumber ( const string &Text )
{
			istringstream ss(Text);
						int result;
										return ss >> result ? result : 0;
}
template<class T> inline vector<pair<T,int> > FACTORISE(T n){vector<pair<T,int> >R;for (T i=2;n>1;){if (n%i==0){int C=0;for (;n%i==0;C++,n/=i);R.push_back(make_pair(i,C));}i++;if (i>n/i) i=n;}if (n>1) R.push_back(make_pair(n,1));return R;}
template<class T> inline T TOTIENT(T n) {vector<pair<T,int> > R=FACTORISE(n);T r=n;for (int i=0;i<R.size();i++)r=r/R[i].first*(R[i].first-1);return r;}
double rnd(float d) //for rounding values
{
			return floor(d + 0.49);
}
//template<class T>inline T input(T x){char c=GC();x=0;T s=1;while(c<48||c>57){if(c=='-')s=-1;c=GC();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+c-48;c=GC();}return x*s;}
int main()
{
	char s[100004];
	scanf("%s",s);
	int f=0,se=0;
	if(strlen(s)%2==0)
	{
		f=strlen(s)/2;
		se=strlen(s)/2;
		char t[1000000];
		if(s[0]-48<=7)
		{
			int u=0;
			FOR(u,0,f)
				t[u]='4';
			FOR(u,f,se+f)
				t[u]='7';
			int pos=f,pre=f+1;
		while(strcmp(t,s)<0)
		{
			char y;
			y=t[pos];
			pos--;
			if(pos<0)
			{
				pos=pre;
				pre++;
			}
			t[pos+1]=t[pos];
			t[pos]=y;
		}
			cout<<t<<endl;
		}
		else
		{
		f=(strlen(s)+2)/2;
		se=(strlen(s)+2)/2;
		int i;
		FOR(i,0,f)
			cout<<4;
		FOR(i,0,se)
			cout<<7;
		cout<<endl;
		}

	}
	else
	{
		f=(strlen(s)+1)/2;
		se=(strlen(s)+1)/2;
		int i;
		FOR(i,0,f)
			cout<<4;
		FOR(i,0,se)
			cout<<7;
		cout<<endl;

	}
	return 0;
}


//Author: Sharad Gupta
#include<iostream>
#include<sstream>
#include<cstdio>
#include<climits>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<deque>
#include<bitset>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<vector>
#include<queue>

using namespace std;

#define VI vector < int >
#define VVI(A,N,M) vector< VI > A( N, VI (M) )
#define LL long long
#define LLU unsigned long long
#define SI ({int x;scanf("%d",&x);x;})
#define SC ({char x;scanf("%c",&x);x;})
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int) (x).size())
#define SORT(c) sort(ALL(c))
#define FIT(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); it++)
#define FITD(it,v) for (typeof(v.rbegin()) it = v.rbegin(); it != v.rend(); it++)
#define FOR(i,start,end) for(int i=start;i<end;i++)
#define IATOV(a) ({vector<int> v(a,a+sizeof(a)/sizeof(int));v;})
#define CATOV(a) ({vector<char> v(a,a+sizeof(a)/sizeof(char));v;})
#define sieve(a) ({int b=ceil(sqrt(a));VI d(a,0);VI e;int f=2;e.pb(2);e.pb(3);for(int x=1;x<b+1;x++){for(int y=1;y<b+1;y++){int n=(4*x*x)+(y*y);if(n<=a&&(n%12==1||n%12==5)){d[n]^=1;}n=(3*x*x)+(y*y);if(n<=a&&n%12==7){d[n]^=1;}n=(3*x*x)-(y*y);if(x>y&&n<=a&&n%12==11){d[n]^=1;}}}for(int r=5;r<b+1;r++){if(d[r]){for(int i=r*r;i<a;i+=(r*r)){d[i]=0;}}}for(int c=5;c<a;c++){if(d[c]){e.pb(c);}}e;})
#define INF 1000000007
#define EPS 1e-9
#define mt(x, y, z) mp(mp(x,y),z)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define setbits(x) __builtin_popcount(x) //tell number of 1's in binary form
#define GC getchar_unlocked
template <class T> string str(T Number){

			string Result; // string which will contain the result

						ostringstream convert; // stream used for the conversion

										convert << Number; // insert the textual representation of 'Number' in the characters in the stream

															Result = convert.str();
																					return Result;
}
int StringToNumber ( const string &Text )
{
			istringstream ss(Text);
						int result;
										return ss >> result ? result : 0;
}
template<class T> inline vector<pair<T,int> > FACTORISE(T n){vector<pair<T,int> >R;for (T i=2;n>1;){if (n%i==0){int C=0;for (;n%i==0;C++,n/=i);R.push_back(make_pair(i,C));}i++;if (i>n/i) i=n;}if (n>1) R.push_back(make_pair(n,1));return R;}
template<class T> inline T TOTIENT(T n) {vector<pair<T,int> > R=FACTORISE(n);T r=n;for (int i=0;i<R.size();i++)r=r/R[i].first*(R[i].first-1);return r;}
double rnd(float d) //for rounding values
{
			return floor(d + 0.49);
}
//template<class T>inline T input(T x){char c=GC();x=0;T s=1;while(c<48||c>57){if(c=='-')s=-1;c=GC();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+c-48;c=GC();}return x*s;}
int check(char s[])
{
	int f=0,se=0;
	int i;
	FOR(i,0,strlen(s))
	{
		if(s[i]-48==4)
			f++;
		if(s[i]-48==7)
			se++;
	}
	if(f+se==strlen(s)&&f==se&&f!=0)
		return 1;
	else
		return 0;

}
int main()
{
	char s[100004];
	scanf("%s",s);
	int f=0,se=0;
	if(strlen(s)%2==0)
	{
		f=strlen(s)/2;
		se=strlen(s)/2;
		char t[1000000];
		if(s[0]-48<=7)
		{
			if(check(t)!=1)
			{
			int u=0;
			FOR(u,0,f)
				t[u]='4';
			FOR(u,f,se+f)
				t[u]='7';
			int pos=f,h=0,pre=f+1,count=0;
		while(strcmp(t,s)<0)
		{
			char y;
			y=t[pos];
			pos--;
			if(pos<h)
			{
				pos=pre;
				pre++;
				y=t[pos];
				pos--;
				count++;
				h++;
			}
			if(count>=f)
			{
				f+=1;
				se+=1;
				FOR(u,0,f)
					t[u]='4';
				FOR(u,f,se+f)
					t[u]='7';
				pos=f;
				pre=pos+1;
				count=0;
				break;
			}
			t[pos+1]=t[pos];
			t[pos]=y;
			}
			cout<<t<<endl;
		}
			else
				cout<<s<<endl;
		}
		else
		{
		f=(strlen(s)+2)/2;
		se=(strlen(s)+2)/2;
		int i;
		FOR(i,0,f)
			cout<<4;
		FOR(i,0,se)
			cout<<7;
		cout<<endl;
		}

	}
	else
	{
		f=(strlen(s)+1)/2;
		se=(strlen(s)+1)/2;
		int i;
		FOR(i,0,f)
			cout<<4;
		FOR(i,0,se)
			cout<<7;
		cout<<endl;

	}
	return 0;
}


//Author: Sharad Gupta
#include<iostream>
#include<sstream>
#include<cstdio>
#include<climits>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<deque>
#include<bitset>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<vector>
#include<queue>

using namespace std;

#define VI vector < int >
#define VVI(A,N,M) vector< VI > A( N, VI (M) )
#define LL long long
#define LLU unsigned long long
#define SI ({int x;scanf("%d",&x);x;})
#define SC ({char x;scanf("%c",&x);x;})
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int) (x).size())
#define SORT(c) sort(ALL(c))
#define FIT(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); it++)
#define FITD(it,v) for (typeof(v.rbegin()) it = v.rbegin(); it != v.rend(); it++)
#define FOR(i,start,end) for(int i=start;i<end;i++)
#define IATOV(a) ({vector<int> v(a,a+sizeof(a)/sizeof(int));v;})
#define CATOV(a) ({vector<char> v(a,a+sizeof(a)/sizeof(char));v;})
#define sieve(a) ({int b=ceil(sqrt(a));VI d(a,0);VI e;int f=2;e.pb(2);e.pb(3);for(int x=1;x<b+1;x++){for(int y=1;y<b+1;y++){int n=(4*x*x)+(y*y);if(n<=a&&(n%12==1||n%12==5)){d[n]^=1;}n=(3*x*x)+(y*y);if(n<=a&&n%12==7){d[n]^=1;}n=(3*x*x)-(y*y);if(x>y&&n<=a&&n%12==11){d[n]^=1;}}}for(int r=5;r<b+1;r++){if(d[r]){for(int i=r*r;i<a;i+=(r*r)){d[i]=0;}}}for(int c=5;c<a;c++){if(d[c]){e.pb(c);}}e;})
#define INF 1000000007
#define EPS 1e-9
#define mt(x, y, z) mp(mp(x,y),z)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define setbits(x) __builtin_popcount(x) //tell number of 1's in binary form
#define GC getchar_unlocked
template <class T> string str(T Number){

			string Result; // string which will contain the result

						ostringstream convert; // stream used for the conversion

										convert << Number; // insert the textual representation of 'Number' in the characters in the stream

															Result = convert.str();
																					return Result;
}
int StringToNumber ( const string &Text )
{
			istringstream ss(Text);
						int result;
										return ss >> result ? result : 0;
}
template<class T> inline vector<pair<T,int> > FACTORISE(T n){vector<pair<T,int> >R;for (T i=2;n>1;){if (n%i==0){int C=0;for (;n%i==0;C++,n/=i);R.push_back(make_pair(i,C));}i++;if (i>n/i) i=n;}if (n>1) R.push_back(make_pair(n,1));return R;}
template<class T> inline T TOTIENT(T n) {vector<pair<T,int> > R=FACTORISE(n);T r=n;for (int i=0;i<R.size();i++)r=r/R[i].first*(R[i].first-1);return r;}
double rnd(float d) //for rounding values
{
			return floor(d + 0.49);
}
//template<class T>inline T input(T x){char c=GC();x=0;T s=1;while(c<48||c>57){if(c=='-')s=-1;c=GC();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+c-48;c=GC();}return x*s;}
int main()
{
	char s[100004];
	scanf("%s",s);
	int f=0,se=0;
	if(strlen(s)%2==0)
	{
		f=strlen(s)/2;
		se=strlen(s)/2;
		char t[1000000];
		if(s[0]-48<=7)
		{
			int u=0;
			FOR(u,0,f)
				t[u]='4';
			FOR(u,f,se+f)
				t[u]='7';
			int pos=f-1,pre=f-2;
		while(strcmp(t,s)<0)
		{
			char y;
			y=t[pos];
			pos++;
			if(pos==f+se)
			{
				pos=pre;
				pre--;
			}
			t[pos-1]=t[pos];
			t[pos]=y;
		}
			cout<<t<<endl;
		}
		else
		{
		f=(strlen(s)+2)/2;
		se=(strlen(s)+2)/2;
		int i;
		FOR(i,0,f)
			cout<<4;
		FOR(i,0,se)
			cout<<7;
		cout<<endl;
		}

	}
	else
	{
		f=(strlen(s)+1)/2;
		se=(strlen(s)+1)/2;
		int i;
		FOR(i,0,f)
			cout<<4;
		FOR(i,0,se)
			cout<<7;
		cout<<endl;

	}
	return 0;
}


//Author: Sharad Gupta
#include<iostream>
#include<sstream>
#include<cstdio>
#include<climits>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<deque>
#include<bitset>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<vector>
#include<queue>

using namespace std;

#define VI vector < int >
#define VVI(A,N,M) vector< VI > A( N, VI (M) )
#define LL long long
#define LLU unsigned long long
#define SI ({int x;scanf("%d",&x);x;})
#define SC ({char x;scanf("%c",&x);x;})
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int) (x).size())
#define SORT(c) sort(ALL(c))
#define FIT(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); it++)
#define FITD(it,v) for (typeof(v.rbegin()) it = v.rbegin(); it != v.rend(); it++)
#define FOR(i,start,end) for(int i=start;i<end;i++)
#define IATOV(a) ({vector<int> v(a,a+sizeof(a)/sizeof(int));v;})
#define CATOV(a) ({vector<char> v(a,a+sizeof(a)/sizeof(char));v;})
#define sieve(a) ({int b=ceil(sqrt(a));VI d(a,0);VI e;int f=2;e.pb(2);e.pb(3);for(int x=1;x<b+1;x++){for(int y=1;y<b+1;y++){int n=(4*x*x)+(y*y);if(n<=a&&(n%12==1||n%12==5)){d[n]^=1;}n=(3*x*x)+(y*y);if(n<=a&&n%12==7){d[n]^=1;}n=(3*x*x)-(y*y);if(x>y&&n<=a&&n%12==11){d[n]^=1;}}}for(int r=5;r<b+1;r++){if(d[r]){for(int i=r*r;i<a;i+=(r*r)){d[i]=0;}}}for(int c=5;c<a;c++){if(d[c]){e.pb(c);}}e;})
#define INF 1000000007
#define EPS 1e-9
#define mt(x, y, z) mp(mp(x,y),z)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define setbits(x) __builtin_popcount(x) //tell number of 1's in binary form
#define GC getchar_unlocked
template <class T> string str(T Number){

			string Result; // string which will contain the result

						ostringstream convert; // stream used for the conversion

										convert << Number; // insert the textual representation of 'Number' in the characters in the stream

															Result = convert.str();
																					return Result;
}
int StringToNumber ( const string &Text )
{
			istringstream ss(Text);
						int result;
										return ss >> result ? result : 0;
}
template<class T> inline vector<pair<T,int> > FACTORISE(T n){vector<pair<T,int> >R;for (T i=2;n>1;){if (n%i==0){int C=0;for (;n%i==0;C++,n/=i);R.push_back(make_pair(i,C));}i++;if (i>n/i) i=n;}if (n>1) R.push_back(make_pair(n,1));return R;}
template<class T> inline T TOTIENT(T n) {vector<pair<T,int> > R=FACTORISE(n);T r=n;for (int i=0;i<R.size();i++)r=r/R[i].first*(R[i].first-1);return r;}
double rnd(float d) //for rounding values
{
			return floor(d + 0.49);
}
//template<class T>inline T input(T x){char c=GC();x=0;T s=1;while(c<48||c>57){if(c=='-')s=-1;c=GC();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+c-48;c=GC();}return x*s;}
int check(char s[])
{
	int f=0,se=0;
	int i;
	FOR(i,0,strlen(s))
	{
		if(s[i]-48==4)
			f++;
		if(s[i]-48==7)
			se++;
	}
	if(f+se==strlen(s)&&f==se&&f!=0)
		return 1;
	else
		return 0;

}
int main()
{
	char s[100004];
	scanf("%s",s);
	int f=0,se=0;
	if(strlen(s)%2==0)
	{
		f=strlen(s)/2;
		se=strlen(s)/2;
		char t[1000000];
		if(s[0]-48<=7)
		{
			if(check(s)!=1)
			{
			int u=0;
			FOR(u,0,f)
				t[u]='4';
			FOR(u,f,se+f)
				t[u]='7';
			int pos=f,h=0,pre=f+1,count=0;
		while(strcmp(t,s)<0)
		{
			char y;
			y=t[pos];
			pos--;
			if(pos<h)
			{
				pos=pre;
				pre++;
				y=t[pos];
				pos--;
				count++;
				h++;
			}
			if(count>=f)
			{
				f+=1;
				se+=1;
				FOR(u,0,f)
					t[u]='4';
				FOR(u,f,se+f)
					t[u]='7';
				pos=f;
				pre=pos+1;
				count=0;
				break;
			}
			t[pos+1]=t[pos];
			t[pos]=y;
			}
			cout<<t<<endl;
		}
			else
				cout<<s<<endl;
		}
		else
		{
		f=(strlen(s)+2)/2;
		se=(strlen(s)+2)/2;
		int i;
		FOR(i,0,f)
			cout<<4;
		FOR(i,0,se)
			cout<<7;
		cout<<endl;
		}

	}
	else
	{
		f=(strlen(s)+1)/2;
		se=(strlen(s)+1)/2;
		int i;
		FOR(i,0,f)
			cout<<4;
		FOR(i,0,se)
			cout<<7;
		cout<<endl;

	}
	return 0;
}


//Author: Sharad Gupta
#include<iostream>
#include<sstream>
#include<cstdio>
#include<climits>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<deque>
#include<bitset>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<vector>
#include<queue>

using namespace std;

#define VI vector < int >
#define VVI(A,N,M) vector< VI > A( N, VI (M) )
#define LL long long
#define LLU unsigned long long
#define SI ({int x;scanf("%d",&x);x;})
#define SC ({char x;scanf("%c",&x);x;})
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int) (x).size())
#define SORT(c) sort(ALL(c))
#define FIT(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); it++)
#define FITD(it,v) for (typeof(v.rbegin()) it = v.rbegin(); it != v.rend(); it++)
#define FOR(i,start,end) for(int i=start;i<end;i++)
#define IATOV(a) ({vector<int> v(a,a+sizeof(a)/sizeof(int));v;})
#define CATOV(a) ({vector<char> v(a,a+sizeof(a)/sizeof(char));v;})
#define sieve(a) ({int b=ceil(sqrt(a));VI d(a,0);VI e;int f=2;e.pb(2);e.pb(3);for(int x=1;x<b+1;x++){for(int y=1;y<b+1;y++){int n=(4*x*x)+(y*y);if(n<=a&&(n%12==1||n%12==5)){d[n]^=1;}n=(3*x*x)+(y*y);if(n<=a&&n%12==7){d[n]^=1;}n=(3*x*x)-(y*y);if(x>y&&n<=a&&n%12==11){d[n]^=1;}}}for(int r=5;r<b+1;r++){if(d[r]){for(int i=r*r;i<a;i+=(r*r)){d[i]=0;}}}for(int c=5;c<a;c++){if(d[c]){e.pb(c);}}e;})
#define INF 1000000007
#define EPS 1e-9
#define mt(x, y, z) mp(mp(x,y),z)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define setbits(x) __builtin_popcount(x) //tell number of 1's in binary form
#define GC getchar_unlocked
template <class T> string str(T Number){

			string Result; // string which will contain the result

						ostringstream convert; // stream used for the conversion

										convert << Number; // insert the textual representation of 'Number' in the characters in the stream

															Result = convert.str();
																					return Result;
}
int StringToNumber ( const string &Text )
{
			istringstream ss(Text);
						int result;
										return ss >> result ? result : 0;
}
template<class T> inline vector<pair<T,int> > FACTORISE(T n){vector<pair<T,int> >R;for (T i=2;n>1;){if (n%i==0){int C=0;for (;n%i==0;C++,n/=i);R.push_back(make_pair(i,C));}i++;if (i>n/i) i=n;}if (n>1) R.push_back(make_pair(n,1));return R;}
template<class T> inline T TOTIENT(T n) {vector<pair<T,int> > R=FACTORISE(n);T r=n;for (int i=0;i<R.size();i++)r=r/R[i].first*(R[i].first-1);return r;}
double rnd(float d) //for rounding values
{
			return floor(d + 0.49);
}
//template<class T>inline T input(T x){char c=GC();x=0;T s=1;while(c<48||c>57){if(c=='-')s=-1;c=GC();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+c-48;c=GC();}return x*s;}
int check(char s[])
{
	int f=0,se=0;
	int i;
	FOR(i,0,strlen(s))
	{
		if(s[i]-48==4)
			f++;
		if(s[i]-48==7)
			se++;
	}
	if(f+se==strlen(s)&&f==se)
		return 1;
	else
		return 0;

}
int main()
{
	char s[100004];
	scanf("%s",s);
	int f=0,se=0;
	if(strlen(s)%2==0)
	{
		f=strlen(s)/2;
		se=strlen(s)/2;
		char t[1000000];
		if(s[0]-48<=7)
		{
			if(check(t)!=1)
			{
			int u=0;
			FOR(u,0,f)
				t[u]='4';
			FOR(u,f,se+f)
				t[u]='7';
			int pos=f,h=0,pre=f+1,count=0;
		while(strcmp(t,s)<0)
		{
			char y;
			y=t[pos];
			pos--;
			if(pos<h)
			{
				pos=pre;
				pre++;
				y=t[pos];
				pos--;
				count++;
				h++;
			}
			if(count>=f)
			{
				f+=1;
				se+=1;
				FOR(u,0,f)
					t[u]='4';
				FOR(u,f,se+f)
					t[u]='7';
				pos=f;
				pre=pos+1;
				count=0;
				break;
			}
			t[pos+1]=t[pos];
			t[pos]=y;
			}
			cout<<t<<endl;
		}
			else
				cout<<s<<endl;
		}
		else
		{
		f=(strlen(s)+2)/2;
		se=(strlen(s)+2)/2;
		int i;
		FOR(i,0,f)
			cout<<4;
		FOR(i,0,se)
			cout<<7;
		cout<<endl;
		}

	}
	else
	{
		f=(strlen(s)+1)/2;
		se=(strlen(s)+1)/2;
		int i;
		FOR(i,0,f)
			cout<<4;
		FOR(i,0,se)
			cout<<7;
		cout<<endl;

	}
	return 0;
}


//Author: Sharad Gupta
#include<iostream>
#include<sstream>
#include<cstdio>
#include<climits>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<deque>
#include<bitset>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<vector>
#include<queue>

using namespace std;

#define VI vector < int >
#define VVI(A,N,M) vector< VI > A( N, VI (M) )
#define LL long long
#define LLU unsigned long long
#define SI ({int x;scanf("%d",&x);x;})
#define SC ({char x;scanf("%c",&x);x;})
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int) (x).size())
#define SORT(c) sort(ALL(c))
#define FIT(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); it++)
#define FITD(it,v) for (typeof(v.rbegin()) it = v.rbegin(); it != v.rend(); it++)
#define FOR(i,start,end) for(int i=start;i<end;i++)
#define IATOV(a) ({vector<int> v(a,a+sizeof(a)/sizeof(int));v;})
#define CATOV(a) ({vector<char> v(a,a+sizeof(a)/sizeof(char));v;})
#define sieve(a) ({int b=ceil(sqrt(a));VI d(a,0);VI e;int f=2;e.pb(2);e.pb(3);for(int x=1;x<b+1;x++){for(int y=1;y<b+1;y++){int n=(4*x*x)+(y*y);if(n<=a&&(n%12==1||n%12==5)){d[n]^=1;}n=(3*x*x)+(y*y);if(n<=a&&n%12==7){d[n]^=1;}n=(3*x*x)-(y*y);if(x>y&&n<=a&&n%12==11){d[n]^=1;}}}for(int r=5;r<b+1;r++){if(d[r]){for(int i=r*r;i<a;i+=(r*r)){d[i]=0;}}}for(int c=5;c<a;c++){if(d[c]){e.pb(c);}}e;})
#define INF 1000000007
#define EPS 1e-9
#define mt(x, y, z) mp(mp(x,y),z)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define setbits(x) __builtin_popcount(x) //tell number of 1's in binary form
#define GC getchar_unlocked
template <class T> string str(T Number){

			string Result; // string which will contain the result

						ostringstream convert; // stream used for the conversion

										convert << Number; // insert the textual representation of 'Number' in the characters in the stream

															Result = convert.str();
																					return Result;
}
int StringToNumber ( const string &Text )
{
			istringstream ss(Text);
						int result;
										return ss >> result ? result : 0;
}
template<class T> inline vector<pair<T,int> > FACTORISE(T n){vector<pair<T,int> >R;for (T i=2;n>1;){if (n%i==0){int C=0;for (;n%i==0;C++,n/=i);R.push_back(make_pair(i,C));}i++;if (i>n/i) i=n;}if (n>1) R.push_back(make_pair(n,1));return R;}
template<class T> inline T TOTIENT(T n) {vector<pair<T,int> > R=FACTORISE(n);T r=n;for (int i=0;i<R.size();i++)r=r/R[i].first*(R[i].first-1);return r;}
double rnd(float d) //for rounding values
{
			return floor(d + 0.49);
}
//template<class T>inline T input(T x){char c=GC();x=0;T s=1;while(c<48||c>57){if(c=='-')s=-1;c=GC();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+c-48;c=GC();}return x*s;}
int main()
{
	char s[100004];
	scanf("%s",s);
	int f=0,se=0;
	if(strlen(s)%2==0)
	{
		f=strlen(s)/2;
		se=strlen(s)/2;
		char t[1000000];
		if(s[0]-48<=7)
		{
			int u=0;
			FOR(u,0,f)
				t[u]='4';
			FOR(u,f,se+f)
				t[u]='7';
			int pos=f,h=0,pre=f+1,count=0;
		while(strcmp(t,s)<0)
		{
			char y;
			y=t[pos];
			pos--;
			if(pos<h)
			{
				pos=pre;
				pre++;
				y=t[pos];
				pos--;
				count++;
				h++;
			}
		//	if(count>=f)
		//		break;
	//	cout<<1<<endl;
			if(count>=f)
			{
				f+=1;
				se+=1;
				FOR(u,0,f)
					t[u]='4';
				FOR(u,f,se+f)
					t[u]='7';
				pos=f;
				pre=pos+1;
				count=0;
				break;
			}
			t[pos+1]=t[pos];
			t[pos]=y;
		}
			cout<<t<<endl;
		}
		else
		{
		f=(strlen(s)+2)/2;
		se=(strlen(s)+2)/2;
		int i;
		FOR(i,0,f)
			cout<<4;
		FOR(i,0,se)
			cout<<7;
		cout<<endl;
		}

	}
	else
	{
		f=(strlen(s)+1)/2;
		se=(strlen(s)+1)/2;
		int i;
		FOR(i,0,f)
			cout<<4;
		FOR(i,0,se)
			cout<<7;
		cout<<endl;

	}
	return 0;
}


//Author: Sharad Gupta
#include<iostream>
#include<sstream>
#include<cstdio>
#include<climits>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<string>
#include<deque>
#include<bitset>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<vector>
#include<queue>

using namespace std;

#define VI vector < int >
#define VVI(A,N,M) vector< VI > A( N, VI (M) )
#define LL long long
#define LLU unsigned long long
#define SI ({int x;scanf("%d",&x);x;})
#define SC ({char x;scanf("%c",&x);x;})
#define PI acos(-1)
#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define sz(x) ((int) (x).size())
#define SORT(c) sort(ALL(c))
#define FIT(it,v) for (typeof(v.begin()) it = v.begin(); it != v.end(); it++)
#define FITD(it,v) for (typeof(v.rbegin()) it = v.rbegin(); it != v.rend(); it++)
#define FOR(i,start,end) for(int i=start;i<end;i++)
#define IATOV(a) ({vector<int> v(a,a+sizeof(a)/sizeof(int));v;})
#define CATOV(a) ({vector<char> v(a,a+sizeof(a)/sizeof(char));v;})
#define sieve(a) ({int b=ceil(sqrt(a));VI d(a,0);VI e;int f=2;e.pb(2);e.pb(3);for(int x=1;x<b+1;x++){for(int y=1;y<b+1;y++){int n=(4*x*x)+(y*y);if(n<=a&&(n%12==1||n%12==5)){d[n]^=1;}n=(3*x*x)+(y*y);if(n<=a&&n%12==7){d[n]^=1;}n=(3*x*x)-(y*y);if(x>y&&n<=a&&n%12==11){d[n]^=1;}}}for(int r=5;r<b+1;r++){if(d[r]){for(int i=r*r;i<a;i+=(r*r)){d[i]=0;}}}for(int c=5;c<a;c++){if(d[c]){e.pb(c);}}e;})
#define INF 1000000007
#define EPS 1e-9
#define mt(x, y, z) mp(mp(x,y),z)
#define present(c,x) ((c).find(x) != (c).end())
#define cpresent(c,x) (find(all(c),x) != (c).end())
#define setbits(x) __builtin_popcount(x) //tell number of 1's in binary form
#define GC getchar_unlocked
template <class T> string str(T Number){

			string Result; // string which will contain the result

						ostringstream convert; // stream used for the conversion

										convert << Number; // insert the textual representation of 'Number' in the characters in the stream

															Result = convert.str();
																					return Result;
}
int StringToNumber ( const string &Text )
{
			istringstream ss(Text);
						int result;
										return ss >> result ? result : 0;
}
template<class T> inline vector<pair<T,int> > FACTORISE(T n){vector<pair<T,int> >R;for (T i=2;n>1;){if (n%i==0){int C=0;for (;n%i==0;C++,n/=i);R.push_back(make_pair(i,C));}i++;if (i>n/i) i=n;}if (n>1) R.push_back(make_pair(n,1));return R;}
template<class T> inline T TOTIENT(T n) {vector<pair<T,int> > R=FACTORISE(n);T r=n;for (int i=0;i<R.size();i++)r=r/R[i].first*(R[i].first-1);return r;}
double rnd(float d) //for rounding values
{
			return floor(d + 0.49);
}
//template<class T>inline T input(T x){char c=GC();x=0;T s=1;while(c<48||c>57){if(c=='-')s=-1;c=GC();}while(c>=48&&c<=57){x=(x<<3)+(x<<1)+c-48;c=GC();}return x*s;}
int s4[100005], s7[100005], a, b;
int main(){
	string s;
	int n;
	cin>> s;
	n= s.length();
	if(n&1){
		n++;
		FOR(i, 0, n/2) printf("4");
		FOR(i, 0, n/2) printf("7");
		return 0;
	}
	a = 0; b = 0;
	FOR(i, 0, n){
		s4[i] = a;
		s7[i] = b;
		if (s[i] == '4') a++;
		if (s[i] == '7') b++;
	}
	if (a+b == n && a == b){
		cout<<s;
		return 0;
	}
	int i;
	for(i=n-1;i>=0;i--){

		a= s4[i];
		b= s7[i];
		if (a+b !=i || a > n/2 || b > n/2)
			continue;

		int c =-1;
		if (s[i] < '4' && (a+1) <= n/2)
			c=4;
		else if (s[i] < '7' && (b+1) <= n/2)
			c=7;
		if(c == -1)
			continue;
		FOR(j, 0, i)
			printf("%c", s[j]);
		printf("%c", char(c+48));

		if (c == 4) a++;
		else b++;

		FOR(j, 0, n-(i+1)){
			if (a+1 <= n/2) cout<<'4' ,a++;
			else cout<<'7';
		}
		return 0;
	}
	FOR(i, 0, n/2+1) cout<<4;
	FOR(i, 0, n/2 +1)cout<<7;
	return 0;
}


#include <algorithm>
#include <cstdlib>
#include <cstdarg>
#include <cassert>
#include <cstring>
#include <complex>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long i64;
typedef long double d64;

#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()

#define pb push_back
#define mp make_pair

#define eprintf(...) {fprintf(stderr,__VA_ARGS__),fflush(stderr);}

#define forn(i,n) for( int i = 0 ; i < (n) ; i++ )
#define forit(it,c) for( __typeof((c).begin()) it = (c).begin() ; it != (c).end() ; it++ )

#ifdef WIN32
#define INT64 "%I64d"
#else
#define INT64 "%lld"
#endif

const int maxn = 1<<20;
char str[maxn];
char res[maxn];
int n;

// dif = 7 - 4;

int check(int index,int dif,int large){
	if(index==n) return dif==0;
	//eprintf("%d %d %d\n",index,dif,large);
	char c = str[index];
	if(large){
		int q = n - index;
		if(dif<0) dif = -dif;
		q-=dif;
		return q>=0;
	}else{
		int res = 0;
		if(c<='4')  res |= check(index+1,dif-1,'4'>c);
		if(c<='7')  res |= check(index+1,dif+1,'7'>c);
		return res;
	}
}
void get(int index,int dif,int large){
	if(index==n) return void(res[index]=0);
	char c = str[index];
	char &t = res[index];
	if(large){
		if(check(index+1,dif-1,1)){
			t = '4';
			return get(index+1,dif-1,1);
		}
		if(check(index,dif+1,1)){
			t = '7';
			return get(index+1,dif+1,1);
		}
		//assert(0);
	}else{
		if(c<='4'&&check(index+1,dif-1,'4'>c)){
			t = '4';
			return get(index+1,dif-1,'4'>c);
		}
		if(c<='7'&&check(index+1,dif+1,'7'>c)){
			t = '7';
			return get(index+1,dif+1,'7'>c);
		}
		//assert(0);
	}
}
int main(){
	//scanf("%s",str); n = strlen(str);
	gets(str);
	n = strlen(str);
	int bad = n&1;
	bad = bad || !check(0,0,0);
	//eprintf("!!!\n");
	if(bad){
		do{
			n++;
		}while(n&1);
		str[0] = '1';
		for(int i = 1 ; i < n ; i++ ) str[i] = '0';
		str[n] = 0;
		for(int i = 0 ; i < n/2 ; i++ ) res[i] = '4';
		for(int i = n/2  ; i < n ; i++ ) res[i] = '7';
		res[n] =0;
		puts(res);
		return 0;
	}
	get(0,0,0);
	//printf("%s\n",res);
	puts(res);
	return 0;
}


#include <algorithm>
#include <cstdlib>
#include <cstdarg>
#include <cassert>
#include <cstring>
#include <complex>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long i64;
typedef long double d64;

#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()

#define pb push_back
#define mp make_pair

#define eprintf(...) {fprintf(stderr,__VA_ARGS__),fflush(stderr);}

#define forn(i,n) for( int i = 0 ; i < (n) ; i++ )
#define forit(it,c) for( __typeof((c).begin()) it = (c).begin() ; it != (c).end() ; it++ )

#ifdef WIN32
#define INT64 "%I64d"
#else
#define INT64 "%lld"
#endif

const int maxn = 1<<20;
char str[maxn];
char res[maxn];
int n;

// dif = 7 - 4;

int check(int index,int dif,int large){
	if(index==n) return dif==0;
	//eprintf("%d %d %d\n",index,dif,large);
	char c = str[index];
	if(large){
		int q = n - index;
		q-=abs(dif);
		return q>=0;
	}else{
		int res = 0;
		if(c<='4')  res |= check(index+1,dif-1,'4'>c);
		if(c<='7')  res |= check(index+1,dif+1,'7'>c);
		return res;
	}
}
void get(int index,int dif,int large){
	if(index==n) return void(res[index]=0);
	char c = str[index];
	char &t = res[index];
	if(large){
		if(check(index+1,dif-1,1)){
			t = '4';
			return get(index+1,dif-1,1);
		}
		if(check(index,dif+1,1)){
			t = '7';
			return get(index+1,dif+1,1);
		}
		//assert(0);
	}else{
		if(c<='4'&&check(index+1,dif-1,'4'>c)){
			t = '4';
			return get(index+1,dif-1,'4'>c);
		}
		if(c<='7'&&check(index+1,dif+1,'7'>c)){
			t = '7';
			return get(index+1,dif+1,'7'>c);
		}
		//assert(0);
	}
}
int main(){
	//scanf("%s",str); n = strlen(str);
	gets(str);
	n = strlen(str);
	int bad = n&1;
	bad = bad || !check(0,0,0);
	//eprintf("!!!\n");
	if(bad){
		do{
			n++;
		}while(n&1);
		str[0] = '1';
		for(int i = 1 ; i < n ; i++ ) str[i] = '0';
		str[n] = 0;
	}
	get(0,0,0);
	//printf("%s\n",res);
	puts(res);
	return 0;
}


#include <algorithm>
#include <cstdlib>
#include <cstdarg>
#include <cassert>
#include <cstring>
#include <complex>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long i64;
typedef long double d64;

#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()

#define pb push_back
#define mp make_pair

#define eprintf(...) {fprintf(stderr,__VA_ARGS__),fflush(stderr);}

#define forn(i,n) for( int i = 0 ; i < (n) ; i++ )
#define forit(it,c) for( __typeof((c).begin()) it = (c).begin() ; it != (c).end() ; it++ )

#ifdef WIN32
#define INT64 "%I64d"
#else
#define INT64 "%lld"
#endif

const int maxn = 1<<20;
char str[maxn];
char res[maxn];
int n;
int d[maxn];

// dif = 7 - 4;

int check(int index,int dif,int large){
	if(index==n) return dif==0;
	//eprintf("%d %d %d\n",index,dif,large);
	char c = str[index];
	if(large){
		int q = n - index;
		if(dif<0) dif = -dif;
		q-=dif;
		return q>=0;
	}else{
		int &res = d[index];
		if(res!=-1) return res;
		res = 0;
		if(c<='4')  res |= check(index+1,dif-1,'4'>c);
		if(c<='7')  res |= check(index+1,dif+1,'7'>c);
		return res;
	}
}
void get(int index,int dif,int large){
	if(index==n) return void(res[index]=0);
	char c = str[index];
	char &t = res[index];
	if(large){
		if(check(index+1,dif-1,1)){
			t = '4';
			return get(index+1,dif-1,1);
		}
		if(check(index,dif+1,1)){
			t = '7';
			return get(index+1,dif+1,1);
		}
		//assert(0);
	}else{
		if(c<='4'&&check(index+1,dif-1,'4'>c)){
			t = '4';
			return get(index+1,dif-1,'4'>c);
		}
		if(c<='7'&&check(index+1,dif+1,'7'>c)){
			t = '7';
			return get(index+1,dif+1,'7'>c);
		}
		//assert(0);
	}
}
int main(){
	//scanf("%s",str); n = strlen(str);
	gets(str);
	n = strlen(str);
	int bad = n&1;
	memset(d,-1,sizeof d);

	bad = bad || !check(0,0,0);
	//eprintf("!!!\n");
	if(bad){
		do{
			n++;
		}while(n&1);
		str[0] = '1';
		for(int i = 1 ; i < n ; i++ ) str[i] = '0';
		str[n] = 0;
		for(int i = 0 ; i < n/2 ; i++ ) res[i] = '4';
		for(int i = n/2  ; i < n ; i++ ) res[i] = '7';
		res[n] =0;
		puts(res);
		return 0;
	}
	get(0,0,0);
	//printf("%s\n",res);
	puts(res);
	return 0;
}


#include <algorithm>
#include <cstdlib>
#include <cstdarg>
#include <cassert>
#include <cstring>
#include <complex>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long i64;
typedef long double d64;

#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()

#define pb push_back
#define mp make_pair

#define eprintf(...) {fprintf(stderr,__VA_ARGS__),fflush(stderr);}

#define forn(i,n) for( int i = 0 ; i < (n) ; i++ )
#define forit(it,c) for( __typeof((c).begin()) it = (c).begin() ; it != (c).end() ; it++ )

#ifdef WIN32
#define INT64 "%I64d"
#else
#define INT64 "%lld"
#endif

const int maxn = 1<<20;
char str[maxn];
char res[maxn];
int n;

// dif = 7 - 4;

int check(int index,int dif,int large){
	if(index==n) return dif==0;
	//eprintf("%d %d %d\n",index,dif,large);
	char c = str[index];
	if(large){
		int q = n - index;
		q-=abs(dif);
		return q>=0;
	}else{
		int res = 0;
		if(c<='4')  res |= check(index+1,dif-1,'4'>c);
		if(c<='7')  res |= check(index+1,dif+1,'7'>c);
		return res;
	}
}
void get(int index,int dif,int large){
	if(index==n) return void(res[index]=0);
	char c = str[index];
	char &t = res[index];
	if(large){
		if(check(index+1,dif-1,1)){
			t = '4';
			return get(index+1,dif-1,1);
		}
		if(check(index,dif+1,1)){
			t = '7';
			return get(index+1,dif+1,1);
		}
		//assert(0);
	}else{
		if(c<='4'&&check(index+1,dif-1,'4'>c)){
			t = '4';
			return get(index+1,dif-1,'4'>c);
		}
		if(c<='7'&&check(index+1,dif+1,'7'>c)){
			t = '7';
			return get(index+1,dif+1,'7'>c);
		}
		//assert(0);
	}
}
int main(){
	//scanf("%s",str); n = strlen(str);
	gets(str);
	n = strlen(str);
	int bad = n&1;
	bad = bad || !check(0,0,0);
	//eprintf("!!!\n");
	if(bad){
		do{
			n++;
		}while(n&1);
		str[0] = '1';
		for(int i = 1 ; i < n ; i++ ) str[i] = '0';
		str[n] = 0;
		for(int i = 0 ; i < n/2 ; i++ ) res[i] = '4';
		for(int i = n/2  ; i < n ; i++ ) res[i] = '7';
		res[n] =0;
		puts(res);
		return 0;
	}
	get(0,0,0);
	//printf("%s\n",res);
	puts(res);
	return 0;
}


#include <algorithm>
#include <cstdlib>
#include <cstdarg>
#include <cassert>
#include <cstring>
#include <complex>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long i64;
typedef long double d64;

#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()

#define pb push_back
#define mp make_pair

#define eprintf(...) {fprintf(stderr,__VA_ARGS__),fflush(stderr);}

#define forn(i,n) for( int i = 0 ; i < (n) ; i++ )
#define forit(it,c) for( __typeof((c).begin()) it = (c).begin() ; it != (c).end() ; it++ )

#ifdef WIN32
#define INT64 "%I64d"
#else
#define INT64 "%lld"
#endif

const int maxn = 1<<20;
char str[maxn];
char res[maxn];
int n;

// dif = 7 - 4;

int check(int index,int dif,int large){
	if(index==n) return dif==0;
	//eprintf("%d %d %d\n",index,dif,large);
	if(large){
		int q = n - index;
		q-=abs(dif);
		return q>=0;
	}else{
		int res = 0;
		if(str[index]<='4')  res |= check(index+1,dif-1,'4'>str[index]);
		if(str[index]<='7')  res |= check(index+1,dif+1,'7'>str[index]);
		return res;
	}
}
void get(int index,int dif,int large){
	if(index==n) return void(res[index]=0);
	if(large){
		if(check(index+1,dif-1,1)){
			res[index] = '4';
			return get(index+1,dif-1,1);
		}
		if(check(index,dif+1,1)){
			res[index] = '7';
			return get(index+1,dif+1,1);
		}
		assert(0);
	}else{
		if(str[index]<='4'&&check(index+1,dif-1,'4'>str[index])){
			res[index] = '4';
			return get(index+1,dif-1,'4'>str[index]);
		}
		if(str[index]<='7'&&check(index+1,dif+1,'7'>str[index])){
			res[index] = '7';
			return get(index+1,dif+1,'7'>str[index]);
		}
		assert(0);
	}
}
int main(){
	scanf("%s",str); n = strlen(str);
	int bad = n&1;
	bad = bad || !check(0,0,0);
	//eprintf("!!!\n");
	if(bad){
		do{
			n++;
		}while(n&1);
		str[0] = '1';
		for(int i = 1 ; i < n ; i++ ) str[i] = '0';
		str[n] = 0;
	}
	get(0,0,0);
	printf("%s\n",res);
	return 0;
}


#include <algorithm>
#include <cstdlib>
#include <cstdarg>
#include <cassert>
#include <cstring>
#include <complex>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long i64;
typedef long double d64;

#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()

#define pb push_back
#define mp make_pair

#define eprintf(...) {fprintf(stderr,__VA_ARGS__),fflush(stderr);}

#define forn(i,n) for( int i = 0 ; i < (n) ; i++ )
#define forit(it,c) for( __typeof((c).begin()) it = (c).begin() ; it != (c).end() ; it++ )

#ifdef WIN32
#define INT64 "%I64d"
#else
#define INT64 "%lld"
#endif

const int maxn = 1<<20;
char str[maxn];
char res[maxn];
int n;
map<int,int> d;
// dif = 7 - 4;

int check(int index,int dif,int large){
	if(index==n) return dif==0;
	//eprintf("%d %d %d\n",index,dif,large);
	char c = str[index];
	if(large){
		int q = n - index;
		if(dif<0) dif = -dif;
		q-=dif;
		return q>=0;
	}else{
		if(d.count(dif)) return d[dif];
		int res = 0;
		if(c<='4')  res |= check(index+1,dif-1,'4'>c);
		if(c<='7')  res |= check(index+1,dif+1,'7'>c);
		d[dif] = res;
		return res;
	}
}
void get(int index,int dif,int large){
	if(index==n) return void(res[index]=0);
	char c = str[index];
	char &t = res[index];
	if(large){
		if(check(index+1,dif-1,1)){
			t = '4';
			return get(index+1,dif-1,1);
		}
		if(check(index,dif+1,1)){
			t = '7';
			return get(index+1,dif+1,1);
		}
		//assert(0);
	}else{
		if(c<='4'&&check(index+1,dif-1,'4'>c)){
			t = '4';
			return get(index+1,dif-1,'4'>c);
		}
		if(c<='7'&&check(index+1,dif+1,'7'>c)){
			t = '7';
			return get(index+1,dif+1,'7'>c);
		}
		//assert(0);
	}
}
int main(){
	//scanf("%s",str); n = strlen(str);
	gets(str);
	n = strlen(str);
	int bad = n&1;
	bad = bad || !check(0,0,0);
	//eprintf("!!!\n");
	if(bad){
		do{
			n++;
		}while(n&1);
		str[0] = '1';
		for(int i = 1 ; i < n ; i++ ) str[i] = '0';
		str[n] = 0;
		for(int i = 0 ; i < n/2 ; i++ ) res[i] = '4';
		for(int i = n/2  ; i < n ; i++ ) res[i] = '7';
		res[n] =0;
		puts(res);
		return 0;
	}
	get(0,0,0);
	//printf("%s\n",res);
	puts(res);
	return 0;
}


#include <algorithm>
#include <cstdlib>
#include <cstdarg>
#include <cassert>
#include <cstring>
#include <complex>
#include <cstdio>
#include <vector>
#include <string>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long i64;
typedef long double d64;

#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()

#define pb push_back
#define mp make_pair

#define eprintf(...) {fprintf(stderr,__VA_ARGS__),fflush(stderr);}

#define forn(i,n) for( int i = 0 ; i < (n) ; i++ )
#define forit(it,c) for( __typeof((c).begin()) it = (c).begin() ; it != (c).end() ; it++ )

#ifdef WIN32
#define INT64 "%I64d"
#else
#define INT64 "%lld"
#endif

const int maxn = 1<<20;
char str[maxn];
char res[maxn];
int n;
map<int,int> d[maxn];

// dif = 7 - 4;

int check(int index,int dif,int large){
	if(index==n) return dif==0;
	//eprintf("%d %d %d\n",index,dif,large);
	char c = str[index];
	if(large){
		int q = n - index;
		if(dif<0) dif = -dif;
		q-=dif;
		return q>=0;
	}else{
		if(d[index].count(dif)) return d[index][dif];
		int res = 0;
		if(c<='4')  res |= check(index+1,dif-1,'4'>c);
		if(c<='7')  res |= check(index+1,dif+1,'7'>c);
		return d[index][dif]=res;
	}
}
void get(int index,int dif,int large){
	if(index==n) return void(res[index]=0);
	char c = str[index];
	char &t = res[index];
	if(large){
		if(check(index+1,dif-1,1)){
			t = '4';
			return get(index+1,dif-1,1);
		}
		if(check(index,dif+1,1)){
			t = '7';
			return get(index+1,dif+1,1);
		}
		//assert(0);
	}else{
		if(c<='4'&&check(index+1,dif-1,'4'>c)){
			t = '4';
			return get(index+1,dif-1,'4'>c);
		}
		if(c<='7'&&check(index+1,dif+1,'7'>c)){
			t = '7';
			return get(index+1,dif+1,'7'>c);
		}
		//assert(0);
	}
}
int main(){
	//scanf("%s",str); n = strlen(str);
	gets(str);
	n = strlen(str);
	int bad = n&1;
	bad = bad || !check(0,0,0);
	//eprintf("!!!\n");
	if(bad){
		do{
			n++;
		}while(n&1);
		str[0] = '1';
		for(int i = 1 ; i < n ; i++ ) str[i] = '0';
		str[n] = 0;
		for(int i = 0 ; i < n/2 ; i++ ) res[i] = '4';
		for(int i = n/2  ; i < n ; i++ ) res[i] = '7';
		res[n] =0;
		puts(res);
		return 0;
	}
	get(0,0,0);
	//printf("%s\n",res);
	puts(res);
	return 0;
}


#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

string s;
int n;
string f;
bool ans;

void dfs(int idx, int cnt4, int cnt7, int tight, string p)
{
     if ( ans ) return;
     if ( abs(cnt4-cnt7) > n-idx ) return;
     if ( idx == n ) {
          if ( cnt4 == cnt7 ) {
             ans = true;
             f = p;
          }
          return;
     }
     if ( ans ) return;
     if ( tight == 1 ) {
        if ( s[idx] <= '4' ) {
           if ( !ans ) dfs(idx+1, cnt4+1, cnt7, !(s[idx] < '4'), p+"4");
        }
        if ( s[idx] <= '7' ) {
           if ( !ans ) dfs(idx+1, cnt4, cnt7+1, !(s[idx] < '7'), p+"7");
        }
     }
     else {
          int l4s = (cnt7-cnt4+n-idx);
          if ( l4s%2 != 0 ) return;
          l4s /= 2;
          int l7s = n-idx-l4s;
          if ( l4s >= 0 && l7s >= 0 ) {
             ans = true;
             for ( int i = 0; i < l4s; i++ ) p += "4";
             for ( int i = 0; i < l7s; i++ ) p += "7";
             f = p;
          }
          return;
     }
}

int main()
{
    cin >> s;
    n = (int)s.size();
    ans = false;
    dfs(0,0,0,1,"");

    if ( !ans ) {
       if ( n%2 == 0 ) n += 2;
       else n += 1;
       f = "";
       for ( int i = 0; i < n; i++ ) {
           if ( i < n/2 ) f += "4";
           else f += "7";
       }
    }
    cout << f << endl;
    return 0;
}


#include<bits/stdc++.h>
#define ll long long int
using namespace std;

int main()
{
    ll n;
    scanf("%lld",&n);
    string s;
    ll a[n],g[n];

    for(ll i=0;i<n;i++)
        scanf("%lld%lld",&a[i],&g[i]);

    ll sa=0,sg=0;

    for(ll i=0;i<n;i++)
    {
        if(sa+a[i]<=sg+500)
        {
            sa+=a[i];
            s.push_back('A');
        }
        else
        {
            sg+=g[i];
            s.push_back('G');
        }
    }

    if(abs(sg-sa)>500)
        printf("-1");
    else
       cout<<s;

}


#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
#include<cstring>
#include<climits>
#include<memory.h>
#include<282vector>
#include<string>
#include<set>
#include<map>
typedef long long int ll;
using namespace std;

int main()
{
    ll n,ai,gi,a=0,g=0;
    string s;

    cin>>n;
    for(ll i=0;i<n;i++)
    {
        cin>>ai>>gi;

        if(abs((g+gi)-(a))<=500)
        {
            s+="G";
            g=g+gi;
        }
        else
        {

        s+="A";
        a=a+ai;

        }

    }

    if(abs(a-g)<=500)
    {
        cout<<s;

    }
    else
    cout<<"-1";

}


#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
#include<cstring>
#include<climits>
#include<memory.h>
#include<vector>
#include<string>
#include<set>
#include<map>
typedef long long int ll;
using namespace std;

int main()
{
    ll n,ai,gi,a=0,g=0;
    string s;

    cin>>n;
    for(ll i=0;i<n;i++)
    {
        cin>>ai>>gi;

        if((g+gi)-(a)<=500)
        {
            cout<<"G";
            g=g+gi;
        }
        else
        {

        cout<<"A";
        a=a+ai;

        }

    }

}


#include<bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin>>n;
    string s;
    int a[n],g[n];

    for(int i=0;i<n;i++)
        cin>>a[i]>>g[i];

    int sa=0,sg=0;

    for(int i=0;i<n;i++)
    {
        if(sa+a[i]<=sg+500)
        {
            sa+=a[i];
            s.push_back('A');
        }
        else
        {
            sg+=g[i];
            s.push_back('G');
        }
    }

    if(abs(sg-sa)>500)
        cout<<"-1";
    else
       cout<<s;

}


#include<iostream>
#include<cstdio>
#include<queue>
#include<vector>
#include<algorithm>
#include<cstring>
#include<climits>
#include<memory.h>
#include<vector>
#include<string>
#include<set>
#include<map>
typedef long long int ll;
using namespace std;

int main()
{
    ll n,ai,gi,a=0,g=0;
    string s;

    cin>>n;
    for(ll i=0;i<n;i++)
    {
        cin>>ai>>gi;

        if(abs((g+gi)-(a))<=500)
        {
            s+="G";
            g=g+gi;
        }
        else
        {

        s+="A";
        a=a+ai;

        }

    }

    if(abs(a-g)<=500)
    {
        cout<<s;

    }
    else
    cout<<"-1";

    return 0;
}


#include<iostream>
typedef long long int ll;
using namespace std;

int main() {
    int n;
    cin >> n;
    int tot = 0;
    for(int i=0;i<n;i++) {
        int a, b;
        cin >> a >> b;
        if (tot+a <= 500) {
            tot += a;
            cout << "A";
        } else {
            tot -= b;
            cout << "G";
        }
    }
    cout << endl;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long n,A[1000003],G[1000003],tota,totg;
string res = "A";
int main()
{
    cin >> n;
    for(int i=0;i<n;i++)
        cin >> A[i] >> G[i];

    tota = A[0];
    for(int i=1;i<n;i++)
    {
        if(tota + A[i] - totg < 500)
            totg += A[i],res += 'A';
        else
            tota += G[i],res += 'G';
    }
    cout << res;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long n,A[1000003],G[1000003],tota,totg;
string res = "A";
int main()
{
    cin >> n;
    for(int i=0;i<n;i++)
        cin >> A[i] >> G[i];

    tota = A[0];
    for(int i=1;i<n;i++)
    {
        if(tota + A[i] - totg < 500)
            tota += A[i],res += 'A';
        else
            totg += G[i],res += 'G';
    }
    cout << res;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int n,A[1000003],G[1000003];
long long tota,totg;
string res = "A";
int main()
{
    ios::sync_with_stdio(0),cin.tie(),cout.tie();
    cin >> n;
    for(int i=0;i<n;i++)
        cin >> A[i] >> G[i];

    tota = A[0];
    for(int i=1;i<n;i++)
    {
        if(tota + A[i] - totg < 500)
            tota += A[i],res += 'A';
        else
            totg += G[i],res += 'G';
    }
    cout << res;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long n,A[1000003],G[1000003],tota,totg;
string res = "A";
int main()
{
    cin >> n;
    for(int i=0;i<n;i++)
        cin >> A[i] >> G[i];

    tota = A[0];
    for(int i=1;i<n;i++)
    {
        if(tota > totg)
            totg += G[i],res += 'G';
        else
            tota += A[i],res += 'A';
    }
    cout << res;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int n,A[1000003],G[1000003];
long long tota,totg
string res = "A";
int main()
{
    ios::sync_with_stdio(0),cin.tie(),cout.tie();
    cin >> n;
    for(int i=0;i<n;i++)
        cin >> A[i] >> G[i];

    tota = A[0];
    for(int i=1;i<n;i++)
    {
        if(tota + A[i] - totg < 500)
            tota += A[i],res += 'A';
        else
            totg += G[i],res += 'G';
    }
    cout << res;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
long long n,A[1000003],G[1000003],tota,totg;
string res = "A";
int main()
{
    cin >> n;
    for(int i=0;i<n;i++)
        cin >> A[i] >> G[i];

    tota = A[0];
    for(int i=1;i<n;i++)
    {
        if(tota + A[i] - totg < 500)
            totg += A[i],res += 'A';
        else
            tota += A[i],res += 'A';
    }
    cout << res;
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int n,A[1000003],G[1000003];
long long tota,totg;
string res ;
int main()
{
    ios::sync_with_stdio(0),cin.tie(),cout.tie();
    cin >> n;
    for(int i=0;i<n;i++)
        cin >> A[i] >> G[i];

    for(int i=0;i<n;i++)
    {
        if(tota + A[i] - totg < 500)
            tota += A[i],res += 'A';
        else
            totg += G[i],res += 'G';
    }
    cout << res;
    return 0;
}


#include <iostream>
#include<stdio.h>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
	int n , sum=0 ;
	int mn , mx;
///vector<int > V ;
vector<char> res ;

int main() {
scanf("%d",&n);
//string res=""  ;

//1	int sum=0;
//	bool l=1;
	for(int i=0 ;  i < n ; i++)
	{scanf(" %d %d",&mn , &mx);

//	cin>>mn>>mx ;
//	cout<<sum<<' '<<mn<<' '<<mx <<endl;
if(abs(sum+mn)<=500){sum+=mn;
res.push_back('A');}
else  {sum-=mx;
res.push_back('G');}
	//else l=0;
	}
for(int i=0  ;i < n  ; i++)
printf("%c" , res[i]);
	return 0;
}


#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
	int n , sum ;
	int mn[31] , mx[31];
vector<int > V ;
vector<char> res  ;

int main() {
	cin>>n  ;
	res.resize(n);
	int sum=0;
	bool l=1;
	for(int i=0 ;  i < n ; i++)
	{	cin>>mn[i]>>mx[i] ;
//	cout<<sum<<' '<<mn[i]<<' '<<mx[i] <<endl;
if(abs(sum+mn[i])<=500){sum+=mn[i];res[i]='A';}
else  {sum-=mx[i];res[i]='G';}
	//else l=0;
	}

if(l){
	for(int i=0 ; i< n ; i++)cout<<res[i];
}

	return 0;
}


#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
	int n , sum ;
	int mn[31] , mx[31];
vector<int > V ;

int main() {
	cin>>n  ;
string res=""  ;

	int sum=0;
	bool l=1;
	for(int i=0 ;  i < n ; i++)
	{	cin>>mn[i]>>mx[i] ;
//	cout<<sum<<' '<<mn[i]<<' '<<mx[i] <<endl;
if(abs(sum+mn[i])<=500){sum+=mn[i];res+="A";}
else  {sum-=mx[i];res+="G";}
	//else l=0;
	}

cout<<res;

	return 0;
}


#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
	int n , sum=0 ;
	int mn , mx;
///vector<int > V ;

int main() {
	cin>>n  ;
string res=""  ;

//1	int sum=0;
//	bool l=1;
	for(int i=0 ;  i < n ; i++)
	{	cin>>mn>>mx ;
//	cout<<sum<<' '<<mn<<' '<<mx <<endl;
if(abs(sum+mn)<=500){sum+=mn;
res+="A";}
else  {sum-=mx;
res+="G";}
	//else l=0;
	}

cout<<res;

	return 0;
}


#include <iostream>
#include<vector>

using namespace std;
	int n , sum ;
	int mn[31] , mx[31];
vector<int > V ;
vector<char> res  ;

int main() {
	cin>>n  ;
	res.resize(n);
	int sum=0;
	bool l=1;
	for(int i=0 ;  i < n ; i++)
	{	cin>>mn[i]>>mx[i] ;
//	cout<<sum<<' '<<mn[i]<<' '<<mx[i] <<endl;
if(abs(sum+mn[i])<=500){sum+=mn[i];res[i]='A';}
else  {sum-=mx[i];res[i]='G';}
	//else l=0;
	}

if(l){
	for(int i=0 ; i< n ; i++)cout<<res[i];
}

	return 0;
}


#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
	int n , sum ;
	int mn[31] , mx[31];
vector<int > V ;
string res=""  ;

int main() {
	cin>>n  ;
	res.resize(n);
	int sum=0;
	bool l=1;
	for(int i=0 ;  i < n ; i++)
	{	cin>>mn[i]>>mx[i] ;
//	cout<<sum<<' '<<mn[i]<<' '<<mx[i] <<endl;
if(abs(sum+mn[i])<=500){sum+=mn[i];res+="A";}
else  {sum-=mx[i];res+="G";}
	//else l=0;
	}

cout<<res;

	return 0;
}


#include <iostream>
#include<vector>
#include<algorithm>
#include<string>
using namespace std;
	int n , sum=0 ;
	int mn , mx;
///vector<int > V ;
vector<char> res ;

int main() {
	cin>>n  ;
//string res=""  ;

//1	int sum=0;
//	bool l=1;
	for(int i=0 ;  i < n ; i++)
	{	cin>>mn>>mx ;
//	cout<<sum<<' '<<mn<<' '<<mx <<endl;
if(abs(sum+mn)<=500){sum+=mn;
res.push_back('A');}
else  {sum-=mx;
res.push_back('G');}
	//else l=0;
	}
for(int i=0  ;i < n  ; i++)
cout<<res[i];

	return 0;
}


#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 1000005
struct st
{
    int x,y,num;
    char ch;
}p[N];
int sum[N];
bool fan(struct st A,struct st B)
{
    if(A.x < B.x)
    return true;
    return false;
}
bool gn(struct st A,struct st B)
{
    if(A.num < B.num)
    return true;
    return false;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i = 1;i <= n;i ++)
        {
            scanf("%d %d",&p[i].x,&p[i].y);
            p[i].num = i;
        }
//        if(n == 1)
//        {
//            if(p[0].x < p[i].y)
//            puts("A");
//            else
//            puts("G");
//            continue;
//        }
        sort(p + 1,p + n + 1,fan);
        sum[1] = p[1].x;
        for(int i = 2;i <= n;i ++)
        sum[i] = sum[i - 1] + p[i].x;
        int max = 1000000009,v,k,s;
        for(int i = 0;i <= n;i ++)
        {
            v = 1000 * (n - i) - (sum[n] - sum[i]);
            s = fabs(v - sum[i]);
            if(s < max)
            {
                max = s;
                k = i;
            }
            else
            break;
        }
        for(int i = 1;i <= k;i ++)
        p[i].ch = 'A';
        for(int i = k + 1;i <= n;i ++)
        p[i].ch = 'G';
        sort(p + 1,p + n + 1,gn);
        if(max > 500)
        {printf("-1\n");}
        else
        {for(int i = 1;i <= n;i ++)
        printf("%c",p[i].ch);
        printf("\n");}

    }
    return 0;
}


#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
#define N 1000005
struct st
{
    int x,y,num;
    char ch;
}p[N];
int sum[N];
bool fan(struct st A,struct st B)
{
    if(A.x < B.x)
    return true;
    return false;
}
bool gn(struct st A,struct st B)
{
    if(A.num < B.num)
    return true;
    return false;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        for(int i = 0;i < n;i ++)
        {
            scanf("%d %d",&p[i].x,&p[i].y);
            p[i].num = i;
        }
        sort(p,p + n,fan);
        sum[0] = p[0].x;
        for(int i = 1;i < n;i ++)
        sum[i] = sum[i - 1] + p[i].x;
        int max = 1000000009,v,k,s;
        for(int i = 0;i < n;i ++)
        {
            v = 1000 * (n - 1 - i) - (sum[n - 1] - sum[i]);
            s = fabs(v - sum[i]);
            if(s < max)
            {
                max = s;
                k = i;
            }
        }
        for(int i = 0;i <= k;i ++)
        p[i].ch = 'A';
        for(int i = k + 1;i < n;i ++)
        p[i].ch = 'G';
        sort(p,p + n,gn);
        if(max <= 500)
        {printf("-1\n");cont
        for(int i = 0;i < n;i ++)
        printf("%c",p[i].ch);
        printf("\n");

    }
    return 0;
}