#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <cstring>
#include <map>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <bitset>
#define f first
#define s second
#define ll long long
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define vi vector <int>
#define ld long double
#define pii pair<int, int>
using namespace std;
const int N = int(3e5), mod = int(1e9)  + 7;
char s[N];
int n;
int a[N],b[N];

int main () {
	scanf("%s",s + 1);
	n = strlen(s + 1);
	int ok = 1;
	for(int i = 1; i <= n; i++){
		if(s[i] != '7'){
			if(s[i] > '7') ok = 0;
			break;
		}
	}
	if(n % 2 == 0){
		for(int i = 1; i <= n / 2; i++){
			if(s[i] != '7'){
				if(s[i] > '7') ok = 0;
				else ok = 2;
				break;
			}
		}
		if(ok == 1){
			for(int i = n / 2 + 1; i <= n; i++){
				if(s[i] != '4'){
					if(s[i] > '4') ok = 0;
					else ok = 2;
					break;
				}
			}
		}
	}
	if((n & 1) || !ok){
		int len = n / 2 + 1;
		for(int i = 1; i <= len; i++){
	    	printf("4");
		}
		for(int i = 1; i <= len; i++){
			printf("7");
		}
		return 0;
	}
	for(int i = 1; i <= n; i++){
		a[i] = a[i - 1];
		b[i] = b[i - 1];
		if(s[i] == '4') a[i]++;
		if(s[i] == '7') b[i]++;
	}
	if(a[n] == n / 2 && b[n] == n / 2){
		printf("%s",s + 1);
		return 0;
	}
	int mx = -1,cur = 0;
	int c4 = 0,c7 = 0;
	for(int i = 1; i <= n; i++){
		int n4 = a[i - 1];
		int n7 = b[i - 1];
		if(n4 + n7 != i - 1) break;
		bool ok = 1;
		if(s[i] < '4'){
			n4++;
			if(n4 <= n / 2 && n7 <= n / 2){
				mx = i - 1;
				cur = 4;
				c4 = n4;
				c7 = n7;
				ok = 0;
			}
		}
		if(ok && s[i] < '7'){
			n7++;
			if(n4 <= n / 2 && n7 <= n / 2){
				mx = i - 1;
				cur = 7;
				c4 = n4;
				c7 = n7;
			}
		}
	}
	for(int i = 1; i <= mx; i++) printf("%c",s[i]);
	printf("%d",cur);
	while(c4 < n / 2){
		printf("4");
		c4++;
	}
	while(c7 < n / 2){
		printf("7");
		c7++;
	}

return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <cstring>
#include <map>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <bitset>
#define f first
#define s second
#define ll long long
#define ull unsigned long long
#define mp make_pair
#define pb push_back
#define vi vector <int>
#define ld long double
#define pii pair<int, int>
using namespace std;
const int N = int(3e5), mod = int(1e9)  + 7;
char s[N];
int n;
int a[N],b[N];

int main () {
	scanf("%s",s + 1);
	n = strlen(s + 1);
	int ok = 1;
	for(int i = 1; i <= n; i++){
		if(s[i] != '7'){
			if(s[i] > '7') ok = 0;
			break;
		}
	}
	if(n % 2 == 0){
		for(int i = 1; i <= n / 2; i++){
			if(s[i] != '7'){
				if(s[i] > '7') ok = 0;
				else ok = 2;
				break;
			}
		}
		if(ok == 1){
			for(int i = n / 2 + 1; i <= n; i++){
				if(s[i] != '4'){
					if(s[i] > '4') ok = 0;
					else ok = 2;
					break;
				}
			}
		}
	}
	if((n & 1) || !ok){
		int len = n / 2 + 1;
		for(int i = 1; i <= len; i++){
	    	printf("4");
		}
		for(int i = 1; i <= len; i++){
			printf("7");
		}
		return 0;
	}
	for(int i = 1; i <= n; i++){
		a[i] = a[i - 1];
		b[i] = b[i - 1];
		if(s[i] == '4') a[i]++;
		if(s[i] == '7') b[i]++;
	}
	int mx = -1,cur = 0;
	int c4 = 0,c7 = 0;
	for(int i = 1; i <= n; i++){
		int n4 = a[i - 1];
		int n7 = b[i - 1];
		if(n4 + n7 != i - 1) break;
		bool ok = 0;
		if(s[i] <= '4'){
			n4++;
			if(n4 <= n / 2 && n7 <= n / 2){
				mx = i - 1;
				cur = 4;
				ok = 1;
				c4 = n4;
				c7 = n7;
			}
			n4--;
		}
		if(!ok && s[i] <= '7'){
			n7++;
			if(n4 <= n / 2 && n7 <= n / 2){
				mx = i - 1;
				cur = 7;
				ok = 1;
				c4 = n4;
				c7 = n7;
			}
		}
	}
	for(int i = 1; i <= mx; i++){
		printf("%c",s[i]);
	}
	printf("%d",cur);
	while(c4 < n / 2){
		printf("4");
		c4++;
	}
	while(c7 < n / 2){
		printf("7");
		c7++;
	}

return 0;
}


#include <bits/stdc++.h>

#define O4 10007
#define O5 100003
#define O6 1000003
#define O7 10000019
#define O8 100000007
#define O9 1000000007
#define all(v) ((v).begin()), ((v).end())
#define rall(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define setZero(a) memset(a,0,sizeof(a))
#define setNeg(a) memset(a,-1,sizeof(a))
#define fa(i,n) for(int i=0;i<n;i++)
#define fb(i,n) for(int i=1;i<=n;i++)
#define fz(i,n) for(int i=n-1;i>=0;--i)
#define rep(i,v) for(int i=0;i<sz(v);++i)
#define rap(i,v) for(int i=sz(v)-1;i>=0;--i)
#define tr(it,v) for(typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define print(arr,n,str) { cout<<str<<" : [ " ; fa(_i,n){ cout<<arr[_i]<<" "; } cout<<"]\n";}
#define pnt(v,s) print(v,sz(v),s)
#define pntAd(adj) rep(i,adj){ cout<<i+1<<" : " ; rep(j,adj[i])cout<<adj[i][j]+1<<" "; ln ; }
#define read(file) freopen(file,"r",stdin)
#define write(file) freopen(file,"w",stdout)
#define rd freopen("C:\\Users\\Miller\\Desktop\\in.txt","r",stdin)
#define mod(n,m) ((n%m)+m)%m
#define pb(n) push_back(n)
#define mp(n,m) make_pair(n,m)
#define dbg(n) cout<<"Debug "<<n<<endl
#define debg(s,n) cout<<s<<" : "<<n<<endl
#define ln cout<<"\n"
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

int d2x[]={1,-1,0,0,1,-1,1,-1};
int d2y[]={0,0,1,-1,1,-1,-1,1};

int d3x[]={1,-1,0,0,0,0};
int d3y[]={0,0,1,-1,0,0};
int d3z[]={0,0,0,0,1,-1};

struct edge{
    int from , to , w ;
    edge( int from , int to , int w ):from(from),to(to),w(w){}
    bool operator < ( const edge & e ) const {
        return w > e.w ;
    }
};

typedef long long ll ;
typedef unsigned long long llu ;
typedef vector<int> vi ;
typedef vector<vi> vii ;
typedef vector<edge> ve ;
typedef vector<ve> vee ;
typedef bitset<15> MASK ;
typedef vector<int> row ;
typedef vector<row> mat ;
const double EPS = (1e-9);

string n , res ;
bool m ;
int cnt = 0 ;

int main()
{
    fast ;
    cin >> n ;

    if( n.size()%2 == 1 ){
        fa(i,sz(n)/2+1)cout<<"4";
        fa(i,sz(n)/2+1)cout<<"7";
    }
    else{
        fa(i,sz(n)/2){
            if( n[i] < '7' )break ;
            if( n[i] > '7' ){
                m = true ;
                break ;
            }
            else if( n[i] == '7' )++cnt ;
        }

        if(!m && cnt == sz(n)/2){
            fa(i,sz(n)/2){
                if( n[i+sz(n)/2] < '4' )break ;
                if( n[i+sz(n)/2] > '4' ){
                    m = true ;
                    break ;
                }
            }
        }

        if(m){
            fa(i,sz(n)/2+1)cout<<"4";
            fa(i,sz(n)/2+1)cout<<"7";
        }
        else{//can construct solution with same length
            int sCount = sz(n)/2 ;
            int fCount = sz(n)/2 ;
            fa(i,sz(n)){
                if( n[i] <= '4' && fCount ){
                    if( n[i] < '4' ){
                        while(res.size()<sz(n)){
                            if(fCount){
                                res+="4";
                                --fCount ;
                            }
                            else{
                                res+="7";
                            }
                        }
                        break ;
                    }
                    else{
                        res+="4" ;
                        --fCount ;
                    }
                }
                else if( n[i] <= '4' && fCount == 0 ){
                    while(res.size()<sz(n))res+="7";
                    break ;
                }
                else if( n[i] > '4' && n[i] <= '7' && sCount ){
                    res+="7";
                    --sCount ;
                    if(n[i]<'7'){
                        while(res.size()<sz(n)){
                            if(fCount){
                                res+="4";
                                --fCount ;
                            }
                            else{
                                res+="7";
                            }
                        }
                        break ;
                    }
                }
                else if( n[i] > '4' ){//n[i] > '7'
                    rap(j,res)if(res[j]=='4'){
                        res[j] = '7' ;
                        --sCount ; ++fCount ;
                        for(int k=j+1 ; k<i ; ++k){
                            if(fCount){
                                if( res[k] == '7' ){
                                    res[k] = '4' ;
                                    ++sCount ;
                                    --fCount ;
                                }
                            }
                            else{
                                break ;
                            }
                        }
                        break ;
                    }
                    while(res.size()<sz(n)){
                        if(fCount){
                            res+="4";
                            --fCount ;
                        }
                        else{
                            res+="7";
                        }
                    }
                    break ;
                }
            }
            cout<<res ;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define O4 10007
#define O5 100003
#define O6 1000003
#define O7 10000019
#define O8 100000007
#define O9 1000000007
#define all(v) ((v).begin()), ((v).end())
#define rall(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define setZero(a) memset(a,0,sizeof(a))
#define setNeg(a) memset(a,-1,sizeof(a))
#define fa(i,n) for(int i=0;i<n;i++)
#define fb(i,n) for(int i=1;i<=n;i++)
#define fz(i,n) for(int i=n-1;i>=0;--i)
#define rep(i,v) for(int i=0;i<sz(v);++i)
#define rap(i,v) for(int i=sz(v)-1;i>=0;--i)
#define tr(it,v) for(typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define print(arr,n,str) { cout<<str<<" : [ " ; fa(_i,n){ cout<<arr[_i]<<" "; } cout<<"]\n";}
#define pnt(v,s) print(v,sz(v),s)
#define pntAd(adj) rep(i,adj){ cout<<i+1<<" : " ; rep(j,adj[i])cout<<adj[i][j]+1<<" "; ln ; }
#define read(file) freopen(file,"r",stdin)
#define write(file) freopen(file,"w",stdout)
#define rd freopen("C:\\Users\\Miller\\Desktop\\in.txt","r",stdin)
#define mod(n,m) ((n%m)+m)%m
#define pb(n) push_back(n)
#define mp(n,m) make_pair(n,m)
#define dbg(n) cout<<"Debug "<<n<<endl
#define debg(s,n) cout<<s<<" : "<<n<<endl
#define ln cout<<"\n"
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

int d2x[]={1,-1,0,0,1,-1,1,-1};
int d2y[]={0,0,1,-1,1,-1,-1,1};

int d3x[]={1,-1,0,0,0,0};
int d3y[]={0,0,1,-1,0,0};
int d3z[]={0,0,0,0,1,-1};

struct edge{
    int from , to , w ;
    edge( int from , int to , int w ):from(from),to(to),w(w){}
    bool operator < ( const edge & e ) const {
        return w > e.w ;
    }
};

typedef long long ll ;
typedef unsigned long long llu ;
typedef vector<int> vi ;
typedef vector<vi> vii ;
typedef vector<edge> ve ;
typedef vector<ve> vee ;
typedef bitset<15> MASK ;
typedef vector<int> row ;
typedef vector<row> mat ;
const double EPS = (1e-9);

string n , res ;
bool m ;
int cnt = 0 ;

int main()
{
    fast ;
    cin >> n ;

    if( n.size()%2 == 1 ){
        fa(i,sz(n)/2+1)cout<<"4";
        fa(i,sz(n)/2+1)cout<<"7";
    }
    else{
        fa(i,sz(n)/2){
            if( n[i] < '7' )break ;
            if( n[i] > '7' ){
                m = true ;
                break ;
            }
            else if( n[i] == '7' )++cnt ;
        }

        if(!m && cnt == sz(n)/2){
            fa(i,sz(n)/2){
                if( n[i+sz(n)/2] < '4' )break ;
                if( n[i+sz(n)/2] > '4' ){
                    m = true ;
                    break ;
                }
            }
        }

        if(m){
            fa(i,sz(n)/2+1)cout<<"4";
            fa(i,sz(n)/2+1)cout<<"7";
        }
        else{//can construct solution with same length
            int sCount = sz(n)/2 ;
            int fCount = sz(n)/2 ;
            fa(i,sz(n)){
                if( n[i] <= '4' && fCount ){
                    if( n[i] < '4' ){
                        while(res.size()<sz(n)){
                            if(fCount){
                                res+="4";
                                --fCount ;
                            }
                            else{
                                res+="7";
                            }
                        }
                        break ;
                    }
                    else{
                        res+="4" ;
                        --fCount ;
                    }
                }
                else if( n[i] <= '4' && fCount == 0 ){
                    while(res.size()<sz(n))res+="7";
                    break ;
                }
                else if( n[i] > '4' && n[i] <= '7' && sCount ){
                    res+="7";
                    --sCount ;
                    if(n[i]<'7'){
                        while(res.size()<sz(n)){
                            if(fCount){
                                res+="4";
                                --fCount ;
                            }
                            else{
                                res+="7";
                            }
                        }
                        break ;
                    }
                }
                else if( n[i] > '4' ){//n[i] > '7'
                    rap(j,res)if(res[j]=='4'){
                        res[j] = '7' ;
                        //dbg(res[j]);
                        --sCount ; ++fCount ;
                        break ;
                    }
                    while(res.size()<sz(n)){
                        if(fCount){
                            res+="4";
                            --fCount ;
                        }
                        else{
                            res+="7";
                        }
                    }
                    break ;
                }
            }
            cout<<res ;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define O4 10007
#define O5 100003
#define O6 1000003
#define O7 10000019
#define O8 100000007
#define O9 1000000007
#define all(v) ((v).begin()), ((v).end())
#define rall(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define setZero(a) memset(a,0,sizeof(a))
#define setNeg(a) memset(a,-1,sizeof(a))
#define fa(i,n) for(int i=0;i<n;i++)
#define fb(i,n) for(int i=1;i<=n;i++)
#define fz(i,n) for(int i=n-1;i>=0;--i)
#define rep(i,v) for(int i=0;i<sz(v);++i)
#define rap(i,v) for(int i=sz(v)-1;i>=0;--i)
#define tr(it,v) for(typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define print(arr,n,str) { cout<<str<<" : [ " ; fa(_i,n){ cout<<arr[_i]<<" "; } cout<<"]\n";}
#define pnt(v,s) print(v,sz(v),s)
#define pntAd(adj) rep(i,adj){ cout<<i+1<<" : " ; rep(j,adj[i])cout<<adj[i][j]+1<<" "; ln ; }
#define read(file) freopen(file,"r",stdin)
#define write(file) freopen(file,"w",stdout)
#define rd freopen("C:\\Users\\Miller\\Desktop\\in.txt","r",stdin)
#define mod(n,m) ((n%m)+m)%m
#define pb(n) push_back(n)
#define mp(n,m) make_pair(n,m)
#define dbg(n) cout<<"Debug "<<n<<endl
#define debg(s,n) cout<<s<<" : "<<n<<endl
#define ln cout<<"\n"
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

int d2x[]={1,-1,0,0,1,-1,1,-1};
int d2y[]={0,0,1,-1,1,-1,-1,1};

int d3x[]={1,-1,0,0,0,0};
int d3y[]={0,0,1,-1,0,0};
int d3z[]={0,0,0,0,1,-1};

struct edge{
    int from , to , w ;
    edge( int from , int to , int w ):from(from),to(to),w(w){}
    bool operator < ( const edge & e ) const {
        return w > e.w ;
    }
};

typedef long long ll ;
typedef unsigned long long llu ;
typedef vector<int> vi ;
typedef vector<vi> vii ;
typedef vector<edge> ve ;
typedef vector<ve> vee ;
typedef bitset<15> MASK ;
typedef vector<int> row ;
typedef vector<row> mat ;
const double EPS = (1e-9);

string n , res ;
bool m = false ;
int cnt = 0 ;

int main()
{
    fast ;
    cin >> n ;

    if( n.size()%2 == 1 ){//right
        fa(i,sz(n)/2+1)cout<<"4";
        fa(i,sz(n)/2+1)cout<<"7";
    }
    else{
        fa(i,sz(n)/2){
            if( n[i] < '7' )break ;
            if( n[i] > '7' ){
                m = true ;
                break ;
            }
            else if( n[i] == '7' )++cnt ;
        }

        if(!m && cnt == sz(n)/2){
            fa(i,sz(n)/2){
                if( n[i+sz(n)/2] < '4' )break ;
                if( n[i+sz(n)/2] > '4' ){
                    m = true ;
                    break ;
                }
            }
        }

        if(m){
            fa(i,sz(n)/2+1)cout<<"4";
            fa(i,sz(n)/2+1)cout<<"7";
        }
        else{//can construct solution with same length
            int sCount = sz(n)/2 ;
            int fCount = sz(n)/2 ;
            fa(i,sz(n)){
                if( n[i] <= '4' && fCount ){
                    if( n[i] < '4' ){
                        while(res.size()<sz(n)){
                            if(fCount){
                                res+="4";
                                --fCount ;
                            }
                            else{
                                res+="7";
                            }
                        }
                        break ;
                    }
                    else{//=='4'
                        res+="4" ;
                        --fCount ;
                    }
                }
                else if( n[i] <= '4' && fCount == 0 ){
                    while(res.size()<sz(n))res+="7";
                    break ;
                }
                else if( n[i] > '4' && n[i] <= '7' && sCount ){//right
                    res+="7";
                    --sCount ;
                    if(n[i]<'7'){
                        while(res.size()<sz(n)){
                            if(fCount){
                                res+="4";
                                --fCount ;
                            }
                            else{
                                res+="7";
                            }
                        }
                        break ;
                    }
                }
                else if( n[i] > '4' ){//n[i] > '7'
                    bool flag = false ;
                    rap(j,res){
                        if(res[j]=='7')flag = true ;
                        else if(res[j]=='4' && sCount){
                            res[j] = '7' ;
                            --sCount ; ++fCount ;
                            for(int k=j+1 ; k<i ; ++k){
                                if(fCount){
                                    if( res[k] == '7' ){
                                        res[k] = '4' ;
                                        ++sCount ;
                                        --fCount ;
                                    }
                                }
                                else{
                                    break ;
                                }
                            }
                            break ;
                        }
                        else if( res[j]=='4' && flag ){

                            res[j] = '7' ;
                            --sCount ; ++fCount ;
                            for(int e=j+1 ; e<i ; ++e){
                                if(res[e]=='7' && fCount){
                                    --fCount ;
                                    res[e] = '4' ;
                                }
                            }
                            break ;
                        }
                        else if(res[j] == '4'){
                            ++fCount ;
                            res[j] = '7' ;
                            --sCount ;
                        }
                    }
                    while(res.size()<sz(n)){
                        if(fCount){
                            res+="4";
                            --fCount ;
                        }
                        else{
                            res+="7";
                        }
                    }
                    break ;
                }
            }
            cout<<res ;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define O4 10007
#define O5 100003
#define O6 1000003
#define O7 10000019
#define O8 100000007
#define O9 1000000007
#define all(v) ((v).begin()), ((v).end())
#define rall(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define setZero(a) memset(a,0,sizeof(a))
#define setNeg(a) memset(a,-1,sizeof(a))
#define fa(i,n) for(int i=0;i<n;i++)
#define fb(i,n) for(int i=1;i<=n;i++)
#define fz(i,n) for(int i=n-1;i>=0;--i)
#define rep(i,v) for(int i=0;i<sz(v);++i)
#define rap(i,v) for(int i=sz(v)-1;i>=0;--i)
#define tr(it,v) for(typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define print(arr,n,str) { cout<<str<<" : [ " ; fa(_i,n){ cout<<arr[_i]<<" "; } cout<<"]\n";}
#define pnt(v,s) print(v,sz(v),s)
#define pntAd(adj) rep(i,adj){ cout<<i+1<<" : " ; rep(j,adj[i])cout<<adj[i][j]+1<<" "; ln ; }
#define read(file) freopen(file,"r",stdin)
#define write(file) freopen(file,"w",stdout)
#define rd freopen("C:\\Users\\Miller\\Desktop\\in.txt","r",stdin)
#define mod(n,m) ((n%m)+m)%m
#define pb(n) push_back(n)
#define mp(n,m) make_pair(n,m)
#define dbg(n) cout<<"Debug "<<n<<endl
#define debg(s,n) cout<<s<<" : "<<n<<endl
#define ln cout<<"\n"
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

int d2x[]={1,-1,0,0,1,-1,1,-1};
int d2y[]={0,0,1,-1,1,-1,-1,1};

int d3x[]={1,-1,0,0,0,0};
int d3y[]={0,0,1,-1,0,0};
int d3z[]={0,0,0,0,1,-1};

struct edge{
    int from , to , w ;
    edge( int from , int to , int w ):from(from),to(to),w(w){}
    bool operator < ( const edge & e ) const {
        return w > e.w ;
    }
};

typedef long long ll ;
typedef unsigned long long llu ;
typedef vector<int> vi ;
typedef vector<vi> vii ;
typedef vector<edge> ve ;
typedef vector<ve> vee ;
typedef bitset<15> MASK ;
typedef vector<int> row ;
typedef vector<row> mat ;
const double EPS = (1e-9);

string n , res ;
bool m ;
int cnt = 0 ;

int main()
{
    fast ;
    cin >> n ;

    if( n.size()%2 == 1 ){
        fa(i,sz(n)/2+1)cout<<"4";
        fa(i,sz(n)/2+1)cout<<"7";
    }
    else{
        fa(i,sz(n)/2){
            if( n[i] > '7' ){
                m = true ;
                break ;
            }
            else if( n[i] == '7' )++cnt ;
        }

        if(!m && cnt == sz(n)/2){
            fa(i,sz(n)/2){
                if( n[i+sz(n)/2] > '4' ){
                    m = true ;
                    break ;
                }
            }
        }

        if(m){
            fa(i,sz(n)/2+1)cout<<"4";
            fa(i,sz(n)/2+1)cout<<"7";
        }
        else{//can construct solution with same length
            int sCount = sz(n)/2 ;
            int fCount = sz(n)/2 ;
            fa(i,sz(n)){
                if( n[i] <= '4' && fCount ){
                    res+="4" ;
                    --fCount ;
                }
                else if( n[i] <= '4' && fCount == 0 ){
                    res+="7" ;
                    --sCount ;
                }
                else if( n[i] > '4' && n[i] <= '7' && sCount ){
                    res+="7";
                    --sCount ;
                }
                else if( n[i] > '4' ){//make replace first left 4 with 7
                    rap(j,res)if(res[j]=='4'){
                        res[j] = '7' ;
                        break ;
                    }
                    while(res.size()<sz(n))res+="4";
                    break ;
                }
            }
            cout<<res ;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define O4 10007
#define O5 100003
#define O6 1000003
#define O7 10000019
#define O8 100000007
#define O9 1000000007
#define all(v) ((v).begin()), ((v).end())
#define rall(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define setZero(a) memset(a,0,sizeof(a))
#define setNeg(a) memset(a,-1,sizeof(a))
#define fa(i,n) for(int i=0;i<n;i++)
#define fb(i,n) for(int i=1;i<=n;i++)
#define fz(i,n) for(int i=n-1;i>=0;--i)
#define rep(i,v) for(int i=0;i<sz(v);++i)
#define rap(i,v) for(int i=sz(v)-1;i>=0;--i)
#define tr(it,v) for(typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define print(arr,n,str) { cout<<str<<" : [ " ; fa(_i,n){ cout<<arr[_i]<<" "; } cout<<"]\n";}
#define pnt(v,s) print(v,sz(v),s)
#define pntAd(adj) rep(i,adj){ cout<<i+1<<" : " ; rep(j,adj[i])cout<<adj[i][j]+1<<" "; ln ; }
#define read(file) freopen(file,"r",stdin)
#define write(file) freopen(file,"w",stdout)
#define rd freopen("C:\\Users\\Miller\\Desktop\\in.txt","r",stdin)
#define mod(n,m) ((n%m)+m)%m
#define pb(n) push_back(n)
#define mp(n,m) make_pair(n,m)
#define dbg(n) cout<<"Debug "<<n<<endl
#define debg(s,n) cout<<s<<" : "<<n<<endl
#define ln cout<<"\n"
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

int d2x[]={1,-1,0,0,1,-1,1,-1};
int d2y[]={0,0,1,-1,1,-1,-1,1};

int d3x[]={1,-1,0,0,0,0};
int d3y[]={0,0,1,-1,0,0};
int d3z[]={0,0,0,0,1,-1};

struct edge{
    int from , to , w ;
    edge( int from , int to , int w ):from(from),to(to),w(w){}
    bool operator < ( const edge & e ) const {
        return w > e.w ;
    }
};

typedef long long ll ;
typedef unsigned long long llu ;
typedef vector<int> vi ;
typedef vector<vi> vii ;
typedef vector<edge> ve ;
typedef vector<ve> vee ;
typedef bitset<15> MASK ;
typedef vector<int> row ;
typedef vector<row> mat ;
const double EPS = (1e-9);

string n , res ;
bool m ;
int cnt = 0 ;

int main()
{
    fast ;
    cin >> n ;

    if( n.size()%2 == 1 ){
        fa(i,sz(n)/2+1)cout<<"4";
        fa(i,sz(n)/2+1)cout<<"7";
    }
    else{
        fa(i,sz(n)/2){
            if( n[i] > '7' ){
                m = true ;
                break ;
            }
            else if( n[i] == '7' )++cnt ;
        }

        if(!m && cnt == sz(n)/2){
            fa(i,sz(n)/2){
                if( n[i+sz(n)/2] > '4' ){
                    m = true ;
                    break ;
                }
            }
        }

        if(m){
            fa(i,sz(n)/2+1)cout<<"4";
            fa(i,sz(n)/2+1)cout<<"7";
        }
        else{//can construct solution with same length
            int sCount = sz(n)/2 ;
            int fCount = sz(n)/2 ;
            fa(i,sz(n)){
                if( n[i] <= '4' && fCount ){
                    res+="4" ;
                    --fCount ;
                }
                else if( n[i] <= '4' && fCount == 0 ){
                    res+="7" ;
                    --sCount ;
                }
                else if( n[i] > '4' && sCount ){
                    res+="7";
                    --sCount ;
                }
                else if( n[i] > '4' && sCount == 0 ){//make replace first left 4 with 7
                    rap(j,res)if(res[j]=='4'){
                        res[j] = '7' ;
                        break ;
                    }
                    while(res.size()<sz(n))res+="4";
                    break ;
                }
            }
            cout<<res ;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define O4 10007
#define O5 100003
#define O6 1000003
#define O7 10000019
#define O8 100000007
#define O9 1000000007
#define all(v) ((v).begin()), ((v).end())
#define rall(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define setZero(a) memset(a,0,sizeof(a))
#define setNeg(a) memset(a,-1,sizeof(a))
#define fa(i,n) for(int i=0;i<n;i++)
#define fb(i,n) for(int i=1;i<=n;i++)
#define fz(i,n) for(int i=n-1;i>=0;--i)
#define rep(i,v) for(int i=0;i<sz(v);++i)
#define rap(i,v) for(int i=sz(v)-1;i>=0;--i)
#define tr(it,v) for(typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define print(arr,n,str) { cout<<str<<" : [ " ; fa(_i,n){ cout<<arr[_i]<<" "; } cout<<"]\n";}
#define pnt(v,s) print(v,sz(v),s)
#define pntAd(adj) rep(i,adj){ cout<<i+1<<" : " ; rep(j,adj[i])cout<<adj[i][j]+1<<" "; ln ; }
#define read(file) freopen(file,"r",stdin)
#define write(file) freopen(file,"w",stdout)
#define rd freopen("C:\\Users\\Miller\\Desktop\\in.txt","r",stdin)
#define mod(n,m) ((n%m)+m)%m
#define pb(n) push_back(n)
#define mp(n,m) make_pair(n,m)
#define dbg(n) cout<<"Debug "<<n<<endl
#define debg(s,n) cout<<s<<" : "<<n<<endl
#define ln cout<<"\n"
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

int d2x[]={1,-1,0,0,1,-1,1,-1};
int d2y[]={0,0,1,-1,1,-1,-1,1};

int d3x[]={1,-1,0,0,0,0};
int d3y[]={0,0,1,-1,0,0};
int d3z[]={0,0,0,0,1,-1};

struct edge{
    int from , to , w ;
    edge( int from , int to , int w ):from(from),to(to),w(w){}
    bool operator < ( const edge & e ) const {
        return w > e.w ;
    }
};

typedef long long ll ;
typedef unsigned long long llu ;
typedef vector<int> vi ;
typedef vector<vi> vii ;
typedef vector<edge> ve ;
typedef vector<ve> vee ;
typedef bitset<15> MASK ;
typedef vector<int> row ;
typedef vector<row> mat ;
const double EPS = (1e-9);

string n , res ;
bool m = false ;
int cnt = 0 ;

int main()
{
    fast ;
    cin >> n ;

    if( n.size()%2 == 1 ){//right
        fa(i,sz(n)/2+1)cout<<"4";
        fa(i,sz(n)/2+1)cout<<"7";
    }
    else{
        fa(i,sz(n)/2){
            if( n[i] < '7' )break ;
            if( n[i] > '7' ){
                m = true ;
                break ;
            }
            else if( n[i] == '7' )++cnt ;
        }

        if(!m && cnt == sz(n)/2){
            fa(i,sz(n)/2){
                if( n[i+sz(n)/2] < '4' )break ;
                if( n[i+sz(n)/2] > '4' ){
                    m = true ;
                    break ;
                }
            }
        }

        if(m){
            fa(i,sz(n)/2+1)cout<<"4";
            fa(i,sz(n)/2+1)cout<<"7";
        }
        else{//can construct solution with same length
            int sCount = sz(n)/2 ;
            int fCount = sz(n)/2 ;
            fa(i,sz(n)){
                if( n[i] <= '4' && fCount ){
                    if( n[i] < '4' ){
                        while(res.size()<sz(n)){
                            if(fCount){
                                res+="4";
                                --fCount ;
                            }
                            else{
                                res+="7";
                            }
                        }
                        break ;
                    }
                    else{//=='4'
                        res+="4" ;
                        --fCount ;
                    }
                }
                else if( n[i] <= '4' && fCount == 0 ){
                    while(res.size()<sz(n))res+="7";
                    break ;
                }
                else if( n[i] > '4' && n[i] <= '7' && sCount ){//right
                    res+="7";
                    --sCount ;
                    if(n[i]<'7'){
                        while(res.size()<sz(n)){
                            if(fCount){
                                res+="4";
                                --fCount ;
                            }
                            else{
                                res+="7";
                            }
                        }
                        break ;
                    }
                }
                else if( n[i] > '4' ){//n[i] > '7'
                    bool flag = false ;
                    rap(j,res){
                        if(res[j]=='7')flag = true ;
                        else if(res[j]=='4' && sCount){
                            res[j] = '7' ;
                            --sCount ; ++fCount ;
                            for(int k=j+1 ; k<i ; ++k){
                                if(fCount){
                                    if( res[k] == '7' ){
                                        res[k] = '4' ;
                                        ++sCount ;
                                        --fCount ;
                                    }
                                }
                                else{
                                    break ;
                                }
                            }
                            break ;
                        }
                        else if( res[j]=='4' && flag ){
                            res[j] = '7' ;
                            --sCount ; ++fCount ;
                            for(int e=j+1 ; e<i ; ++e){
                                if(res[e]=='7' && fCount){
                                    --fCount ;
                                    res[e] = '4' ;
                                }
                            }
                        }
                    }
                    while(res.size()<sz(n)){
                        if(fCount){
                            res+="4";
                            --fCount ;
                        }
                        else{
                            res+="7";
                        }
                    }
                    break ;
                }
            }
            cout<<res ;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define O4 10007
#define O5 100003
#define O6 1000003
#define O7 10000019
#define O8 100000007
#define O9 1000000007
#define all(v) ((v).begin()), ((v).end())
#define rall(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define setZero(a) memset(a,0,sizeof(a))
#define setNeg(a) memset(a,-1,sizeof(a))
#define fa(i,n) for(int i=0;i<n;i++)
#define fb(i,n) for(int i=1;i<=n;i++)
#define fz(i,n) for(int i=n-1;i>=0;--i)
#define rep(i,v) for(int i=0;i<sz(v);++i)
#define rap(i,v) for(int i=sz(v)-1;i>=0;--i)
#define tr(it,v) for(typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define print(arr,n,str) { cout<<str<<" : [ " ; fa(_i,n){ cout<<arr[_i]<<" "; } cout<<"]\n";}
#define pnt(v,s) print(v,sz(v),s)
#define pntAd(adj) rep(i,adj){ cout<<i+1<<" : " ; rep(j,adj[i])cout<<adj[i][j]+1<<" "; ln ; }
#define read(file) freopen(file,"r",stdin)
#define write(file) freopen(file,"w",stdout)
#define rd freopen("C:\\Users\\Miller\\Desktop\\in.txt","r",stdin)
#define mod(n,m) ((n%m)+m)%m
#define pb(n) push_back(n)
#define mp(n,m) make_pair(n,m)
#define dbg(n) cout<<"Debug "<<n<<endl
#define debg(s,n) cout<<s<<" : "<<n<<endl
#define ln cout<<"\n"
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

int d2x[]={1,-1,0,0,1,-1,1,-1};
int d2y[]={0,0,1,-1,1,-1,-1,1};

int d3x[]={1,-1,0,0,0,0};
int d3y[]={0,0,1,-1,0,0};
int d3z[]={0,0,0,0,1,-1};

struct edge{
    int from , to , w ;
    edge( int from , int to , int w ):from(from),to(to),w(w){}
    bool operator < ( const edge & e ) const {
        return w > e.w ;
    }
};

typedef long long ll ;
typedef unsigned long long llu ;
typedef vector<int> vi ;
typedef vector<vi> vii ;
typedef vector<edge> ve ;
typedef vector<ve> vee ;
typedef bitset<15> MASK ;
typedef vector<int> row ;
typedef vector<row> mat ;
const double EPS = (1e-9);

string n , res ;
bool m ;
int cnt = 0 ;

int main()
{
    fast ;
    cin >> n ;

    if( n.size()%2 == 1 ){
        fa(i,sz(n)/2+1)cout<<"4";
        fa(i,sz(n)/2+1)cout<<"7";
    }
    else{
        fa(i,sz(n)/2){
            if( n[i] > '7' ){
                m = true ;
                break ;
            }
            else if( n[i] == '7' )++cnt ;
        }

        if(!m && cnt == sz(n)/2){
            fa(i,sz(n)/2){
                if( n[i+sz(n)/2] > '4' ){
                    m = true ;
                    break ;
                }
            }
        }

        if(m){
            fa(i,sz(n)/2+1)cout<<"4";
            fa(i,sz(n)/2+1)cout<<"7";
        }
        else{//can construct solution with same length
            int sCount = sz(n)/2 ;
            int fCount = sz(n)/2 ;
            fa(i,sz(n)){
                if( n[i] <= '4' && fCount ){
                    if( n[i] < '4' ){
                        while(res.size()<sz(n)){
                            if(fCount){
                                res+="4";
                                --fCount ;
                            }
                            else{
                                res+="7";
                            }
                        }
                        break ;
                    }
                    else{
                        res+="4" ;
                        --fCount ;
                    }
                }
                else if( n[i] <= '4' && fCount == 0 ){
                    while(res.size()<sz(n))res+="7";
                    break ;
                }
                else if( n[i] > '4' && n[i] <= '7' && sCount ){
                    res+="7";
                    --sCount ;
                    if(n[i]<'7'){
                        while(res.size()<sz(n)){
                            if(fCount){
                                res+="4";
                                --fCount ;
                            }
                            else{
                                res+="7";
                            }
                        }
                        break ;
                    }
                }
                else if( n[i] > '4' ){//make replace first left 4 with 7
                    rap(j,res)if(res[j]=='4'){
                        res[j] = '7' ;
                        --sCount ; ++fCount ;
                        break ;
                    }
                    while(res.size()<sz(n)){
                        if(fCount){
                            res+="4";
                            --fCount ;
                        }
                        else{
                            res+="7";
                        }
                    }
                    break ;
                }
            }
            cout<<res ;
        }
    }

    return 0;
}


#include <bits/stdc++.h>

#define O4 10007
#define O5 100003
#define O6 1000003
#define O7 10000019
#define O8 100000007
#define O9 1000000007
#define all(v) ((v).begin()), ((v).end())
#define rall(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define setZero(a) memset(a,0,sizeof(a))
#define setNeg(a) memset(a,-1,sizeof(a))
#define fa(i,n) for(int i=0;i<n;i++)
#define fb(i,n) for(int i=1;i<=n;i++)
#define fz(i,n) for(int i=n-1;i>=0;--i)
#define rep(i,v) for(int i=0;i<sz(v);++i)
#define rap(i,v) for(int i=sz(v)-1;i>=0;--i)
#define tr(it,v) for(typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define print(arr,n,str) { cout<<str<<" : [ " ; fa(_i,n){ cout<<arr[_i]<<" "; } cout<<"]\n";}
#define pnt(v,s) print(v,sz(v),s)
#define pntAd(adj) rep(i,adj){ cout<<i+1<<" : " ; rep(j,adj[i])cout<<adj[i][j]+1<<" "; ln ; }
#define read(file) freopen(file,"r",stdin)
#define write(file) freopen(file,"w",stdout)
#define rd freopen("C:\\Users\\Miller\\Desktop\\in.txt","r",stdin)
#define mod(n,m) ((n%m)+m)%m
#define pb(n) push_back(n)
#define mp(n,m) make_pair(n,m)
#define dbg(n) cout<<"Debug "<<n<<endl
#define debg(s,n) cout<<s<<" : "<<n<<endl
#define ln cout<<"\n"
#define fast ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;

int d2x[]={1,-1,0,0,1,-1,1,-1};
int d2y[]={0,0,1,-1,1,-1,-1,1};

int d3x[]={1,-1,0,0,0,0};
int d3y[]={0,0,1,-1,0,0};
int d3z[]={0,0,0,0,1,-1};

struct edge{
    int from , to , w ;
    edge( int from , int to , int w ):from(from),to(to),w(w){}
    bool operator < ( const edge & e ) const {
        return w > e.w ;
    }
};

typedef long long ll ;
typedef unsigned long long llu ;
typedef vector<int> vi ;
typedef vector<vi> vii ;
typedef vector<edge> ve ;
typedef vector<ve> vee ;
typedef bitset<15> MASK ;
typedef vector<int> row ;
typedef vector<row> mat ;
const double EPS = (1e-9);

string n , res ;
bool m = false ;
int cnt = 0 ;

int main()
{
    fast ;
    cin >> n ;

    if( n.size()%2 == 1 ){//right
        fa(i,sz(n)/2+1)cout<<"4";
        fa(i,sz(n)/2+1)cout<<"7";
    }
    else{
        fa(i,sz(n)/2){
            if( n[i] < '7' )break ;
            if( n[i] > '7' ){
                m = true ;
                break ;
            }
            else if( n[i] == '7' )++cnt ;
        }

        if(!m && cnt == sz(n)/2){
            fa(i,sz(n)/2){
                if( n[i+sz(n)/2] < '4' )break ;
                if( n[i+sz(n)/2] > '4' ){
                    m = true ;
                    break ;
                }
            }
        }

        if(m){
            fa(i,sz(n)/2+1)cout<<"4";
            fa(i,sz(n)/2+1)cout<<"7";
        }
        else{//can construct solution with same length
            int sCount = sz(n)/2 ;
            int fCount = sz(n)/2 ;
            fa(i,sz(n)){
                if( n[i] <= '4' && fCount ){
                    if( n[i] < '4' ){
                        while(res.size()<sz(n)){
                            if(fCount){
                                res+="4";
                                --fCount ;
                            }
                            else{
                                res+="7";
                            }
                        }
                        break ;
                    }
                    else{//=='4'
                        res+="4" ;
                        --fCount ;
                    }
                }
                else if( n[i] <= '4' && fCount == 0 ){
                    while(res.size()<sz(n))res+="7";
                    break ;
                }
                else if( n[i] > '4' && n[i] <= '7' && sCount ){//right
                    res+="7";
                    --sCount ;
                    if(n[i]<'7'){
                        while(res.size()<sz(n)){
                            if(fCount){
                                res+="4";
                                --fCount ;
                            }
                            else{
                                res+="7";
                            }
                        }
                        break ;
                    }
                }
                else if( n[i] > '4' ){//n[i] > '7'
                    bool flag = false ;
                    rap(j,res){
                        if(res[j]=='7')flag = true ;
                        else if(res[j]=='4' && sCount){
                            res[j] = '7' ;
                            --sCount ; ++fCount ;
                            for(int k=j+1 ; k<i ; ++k){
                                if(fCount){
                                    if( res[k] == '7' ){
                                        res[k] = '4' ;
                                        ++sCount ;
                                        --fCount ;
                                    }
                                }
                                else{
                                    break ;
                                }
                            }
                            break ;
                        }
                        else if( res[j]=='4' && flag ){

                            res[j] = '7' ;
                            --sCount ; ++fCount ;
                            for(int e=j+1 ; e<i ; ++e){
                                if(res[e]=='7' && fCount){
                                    --fCount ;
                                    res[e] = '4' ;
                                }
                            }
                            break ;
                        }
                        else{
                            if(res[j]=='4')++fCount;
                            else ++sCount ;
                            res.resize( res.size() - 1 );
                        }
                    }
                    while(res.size()<sz(n)){
                        if(fCount){
                            res+="4";
                            --fCount ;
                        }
                        else{
                            res+="7";
                        }
                    }
                    break ;
                }
            }
            cout<<res ;
        }
    }

    return 0;
}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using LL = int64_t;
using pii = pair<int, int>;
const int SZ = 1000000 + 10;
string g_s;

void prete() {}
void input() {}
string output(int k) {
    string s;
    for(int i = 0; i < k; ++i) {
        if(i < k / 2) {
            s.push_back('4');
        } else {
            s.push_back('7');
        }
    }
    return s;
}
void solve() {
    string s;
    int len = g_s.length();
    if(len & 1) {
        s = output(len + 1);
        cout << s << endl;
        return;
    }
    s = output(len);
    reverse(s.begin(), s.end());
    if(s < g_s) {
        s = output(len + 2);
        cout << s << endl;
        return;
    }
    if(s == g_s) {
        cout << g_s << endl;
        return;
    }
    s.clear();
    int cnt7 = len / 2, cnt4 = len / 2;
    for(int i = 0; i < len; ++i) {
        char ch = g_s[i];
        if(ch == '7') {
            --cnt7;
            s.push_back('7');
        } else if(ch == '4') {
            --cnt4;
            s.push_back('4');
        } else if('4' < ch && ch < '7') {
            --cnt7;
            s.push_back('7');
            break;
        } else if(ch < '4') {
            --cnt4;
            s.push_back('4');
            break;
        }
    }
    for(int i = 0; i < cnt4; ++i) {
        s.push_back('4');
    }
    for(int i = 0; i < cnt7; ++i) {
        s.push_back('7');
    }
    cout << s << endl;
}

int main() {
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\in.txt", "r", stdin);
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\out.txt", "w", stdout);
    prete();
    //int t;
    //cin >> t;
    //for(int i = 1; i <= t; ++i) {
    //    cin >> g_n;
    while(cin >> g_s) {
        input();
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using LL = int64_t;
using pii = pair<int, int>;
const int SZ = 1000000 + 10;
string g_s;

void prete() {}
void input() {}
string change() {
    string s;
    for(int i = 0; i < g_s.size(); ++i) {
        char ch = g_s[i];
        if(ch > '7') {
            int cnt = s.size(), era = 0;
            while(!s.empty() && s.back() == '7') {
                s.pop_back();
                ++era;
            }
            if(s.empty()) {
                s = string(cnt + 1, '4');
            } else {
                s.pop_back();
                s.push_back('7');
                s += string(era, '7');
            }
            s += string(g_s.size() - i, '4');
            break;
        } else if(ch == '4' || ch == '7') {
            s.push_back(ch);
        } else {
            if(ch > '4') {
                s.push_back('7');
            } else {
                s.push_back('4');
            }
            s += string(g_s.size() - i - 1, '4');
            break;
        }
    }
    return s;
}
string get_new(int k) {
    string s;
    for(int i = 0; i < k; ++i) {
        if(i < k / 2) {
            s.push_back('4');
        } else {
            s.push_back('7');
        }
    }
    return s;
}
string judge(string s) {
    int cnt7 = 0;
    for(char &ch : s) {
        if(ch == '7') {
            ++cnt7;
            continue;
        }
        break;
    }
    bool is_7 = false;
    for(int i = cnt7; i < s.size(); ++i) {
        if(s[i] == '7') {
            is_7 = true;
            break;
        }
    }
    if(cnt7 > s.size() / 2 || (cnt7 == s.size() / 2 && is_7)) {
        s = string(s.size() + 1, '4');
    }
    if(s.size() & 1) {
        s = string(s.size() + 1, '4');
    }
    return s;
}
string get_ans(string s) {
    int len = s.size(), cnt4 = len / 2, cnt7 = len / 2;
    string ans;
    for(int i = 0; i < s.size(); ++i) {
        char ch = s[i];
        if(ch == '4') {
            --cnt4;
            ans.push_back('4');
            if(cnt4 == 0) {
                ans += string(cnt7, '7');
                return ans;
            }
        } else {
            --cnt7;
            ans.push_back('7');
            if(cnt7 < 0) {
                ans.pop_back();
                ++cnt7;
                while(ans.back() == '7') {
                    ans.pop_back();
                    ++cnt7;
                }
                ans.pop_back();
                ++cnt4;
                ans.push_back('7');
                --cnt7;
                ans += string(cnt4, '4');
                ans += string(cnt7, '7');
                return ans;
            }
        }
    }
    return ans;
}
void solve() {
    string s = change();
    s = judge(s);
    //cout << s << endl;
    cout << get_ans(s) << endl;
}

int main() {
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\in.txt", "r", stdin);
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\out.txt", "w", stdout);
    prete();
    //int t;
    //cin >> t;
    //for(int i = 1; i <= t; ++i) {
    //    cin >> g_n;
    while(cin >> g_s) {
        input();
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using LL = int64_t;
using pii = pair<int, int>;
const int SZ = 1000000 + 10;
string g_s;

void prete() {}
void input() {}
string output(int k) {
    string s;
    for(int i = 0; i < k; ++i) {
        if(i < k / 2) {
            s.push_back('4');
        } else {
            s.push_back('7');
        }
    }
    return s;
}
void solve() {
    string s;
    int len = g_s.length();
    if(len & 1) {
        s = output(len + 1);
        cout << s << endl;
        return;
    }
    s = output(len);
    reverse(s.begin(), s.end());
    if(s < g_s) {
        s = output(len + 2);
        cout << s << endl;
        return;
    }
    if(s == g_s) {
        cout << g_s << endl;
        return;
    }
    s.clear();
    int cnt7 = len / 2, cnt4 = len / 2;
    for(int i = 0; i < len; ++i) {
        char ch = g_s[i];
        if(ch > '7') {
            while(s.back() != '4') {
                s.pop_back();
                ++cnt7;
            }
            if(s.back() == '4') {
                s.pop_back();
                ++cnt4;
                --cnt7;
                s.push_back('7');
            }
            break;
        } else if(ch == '7') {
            --cnt7;
            s.push_back('7');
        } else if('4' < ch && ch < '7') {
            --cnt7;
            s.push_back('7');
            break;
        } else if(ch == '4') {
            --cnt4;
            s.push_back('4');
        } else if(ch < '4') {
            --cnt4;
            s.push_back('4');
            break;
        }
    }
    for(int i = 0; i < cnt4; ++i) {
        s.push_back('4');
    }
    for(int i = 0; i < cnt7; ++i) {
        s.push_back('7');
    }
    cout << s << endl;
}

int main() {
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\in.txt", "r", stdin);
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\out.txt", "w", stdout);
    prete();
    //int t;
    //cin >> t;
    //for(int i = 1; i <= t; ++i) {
    //    cin >> g_n;
    while(cin >> g_s) {
        input();
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using LL = int64_t;
using pii = pair<int, int>;
const int SZ = 1000000 + 10;
string g_s;

void prete() {}
void input() {}
string output(int k) {
    string s;
    for(int i = 0; i < k; ++i) {
        if(i < k / 2) {
            s.push_back('4');
        } else {
            s.push_back('7');
        }
    }
    return s;
}
void solve() {
    string s;
    int len = g_s.length();
    if(len & 1) {
        s = output(len + 1);
        cout << s << endl;
        return;
    }
    s = output(len);
    reverse(s.begin(), s.end());
    if(s < g_s) {
        s = output(len + 2);
        cout << s << endl;
        return;
    }
    if(s == g_s) {
        cout << s << endl;
        return;
    }
    s.clear();
    int cnt7 = len / 2, cnt4 = len / 2;
    for(int i = 0; i < len; ++i) {
        char ch = g_s[i];
        if(ch > '7') {
            while(s.back() == '7') {
                s.pop_back();
                ++cnt7;
            }
            s.pop_back();
            ++cnt4;
            --cnt7;
            s.push_back('7');
            break;
        } else if(ch == '7') {
            if(cnt7) {
                --cnt7;
                s.push_back('7');
            } else {
                while(s.back() == '7') {
                    s.pop_back();
                    ++cnt7;
                }
                s.pop_back();
                ++cnt4;
                --cnt7;
                s.push_back('7');
                break;
            }
        } else if('4' < ch && ch < '7') {
            if(cnt7) {
                --cnt7;
                s.push_back('7');
                break;
            } else {
                while(s.back() == '7') {
                    s.pop_back();
                    ++cnt7;
                }
                s.pop_back();
                ++cnt4;
                --cnt7;
                s.push_back('7');
                break;
            }
        } else if(ch == '4') {
            if(cnt4) {
                --cnt4;
                s.push_back('4');
            } else {
                --cnt7;
                s.push_back('7');
                break;
            }
        } else if(ch < '4') {
            if(cnt4) {
                --cnt4;
                s.push_back('4');
            } else {
                --cnt7;
                s.push_back('7');
            }
            break;
        }
    }
    //cout << cnt4 << endl << cnt7 << endl;
    for(int i = 0; i < cnt4; ++i) {
        s.push_back('4');
    }
    for(int i = 0; i < cnt7; ++i) {
        s.push_back('7');
    }
    cout << s << endl;
}

int main() {
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\in.txt", "r", stdin);
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\out.txt", "w", stdout);
    prete();
    //int t;
    //cin >> t;
    //for(int i = 1; i <= t; ++i) {
    //    cin >> g_n;
    while(cin >> g_s) {
        input();
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using LL = int64_t;
using pii = pair<int, int>;
const int SZ = 1000000 + 10;
string g_s;

void prete() {}
void input() {}
string output(int k) {
    string s;
    for(int i = 0; i < k; ++i) {
        if(i < k / 2) {
            s.push_back('4');
        } else {
            s.push_back('7');
        }
    }
    return s;
}
void solve() {
    string s;
    int len = g_s.length();
    if(len & 1) {
        s = output(len + 1);
        cout << s << endl;
        return;
    }
    s = output(len);
    reverse(s.begin(), s.end());
    if(s < g_s) {
        s = output(len + 2);
        cout << s << endl;
        return;
    }
    if(s == g_s) {
        cout << s << endl;
        return;
    }
    s.clear();
    int cnt7 = len / 2, cnt4 = len / 2;
    for(int i = 0; i < len; ++i) {
        char ch = g_s[i];
        if(ch > '7') {
            while(s.back() == '7') {
                s.pop_back();
                ++cnt7;
            }
            s.pop_back();
            ++cnt4;
            --cnt7;
            s.push_back('7');
            break;
        } else if(ch == '7') {
            if(cnt7>0) {
                --cnt7;
                s.push_back('7');
            } else {
                while(s.back() == '7') {
                    s.pop_back();
                    ++cnt7;
                }
                s.pop_back();
                ++cnt4;
                --cnt7;
                s.push_back('7');
                break;
            }
        } else if('4' < ch && ch < '7') {
            if(cnt7>0) {
                --cnt7;
                s.push_back('7');
                break;
            } else {
                while(s.back() == '7') {
                    s.pop_back();
                    ++cnt7;
                }
                s.pop_back();
                ++cnt4;
                --cnt7;
                s.push_back('7');
                break;
            }
        } else if(ch == '4') {
            if(cnt4>0) {
                --cnt4;
                s.push_back('4');
            } else {
                --cnt7;
                s.push_back('7');
                break;
            }
        } else if(ch < '4') {
            if(cnt4>0) {
                --cnt4;
                s.push_back('4');
            } else {
                --cnt7;
                s.push_back('7');
            }
            break;
        }
    }
    //cout << cnt4 << endl << cnt7 << endl;
    for(int i = 0; i < cnt4; ++i) {
        s.push_back('4');
    }
    for(int i = 0; i < cnt7; ++i) {
        s.push_back('7');
    }
    cout << s << endl;
}

int main() {
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\in.txt", "r", stdin);
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\out.txt", "w", stdout);
    prete();
    //int t;
    //cin >> t;
    //for(int i = 1; i <= t; ++i) {
    //    cin >> g_n;
    while(cin >> g_s) {
        input();
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using LL = int64_t;
using pii = pair<int, int>;
const int SZ = 1000000 + 10;
string g_s;

void prete() {}
void input() {}
string output(int k) {
    string s;
    for(int i = 0; i < k; ++i) {
        if(i < k / 2) {
            s.push_back('4');
        } else {
            s.push_back('7');
        }
    }
    return s;
}
void solve() {
    string s;
    int len = g_s.length();
    if(len & 1) {
        s = output(len + 1);
        cout << s << endl;
        return;
    }
    s = output(len);
    reverse(s.begin(), s.end());
    if(s < g_s) {
        s = output(len + 2);
        cout << s << endl;
        return;
    }
    if(s == g_s) {
        cout << g_s << endl;
        return;
    }
    s.clear();
    int cnt7 = len / 2, cnt4 = len / 2;
    for(int i = 0; i < len; ++i) {
        char ch = g_s[i];
        if(ch > '7') {
            while(s.back() == '7') {
                s.pop_back();
                ++cnt7;
            }
            s.pop_back();
            ++cnt4;
            --cnt7;
            s.push_back('7');
            break;
        } else if(ch == '7') {
            --cnt7;
            s.push_back('7');
        } else if('4' < ch && ch < '7') {
            --cnt7;
            s.push_back('7');
            break;
        } else if(ch == '4') {
            --cnt4;
            s.push_back('4');
        } else if(ch < '4') {
            --cnt4;
            s.push_back('4');
            break;
        }
    }
    for(int i = 0; i < cnt4; ++i) {
        s.push_back('4');
    }
    for(int i = 0; i < cnt7; ++i) {
        s.push_back('7');
    }
    cout << s << endl;
}

int main() {
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\in.txt", "r", stdin);
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\out.txt", "w", stdout);
    prete();
    //int t;
    //cin >> t;
    //for(int i = 1; i <= t; ++i) {
    //    cin >> g_n;
    while(cin >> g_s) {
        input();
        solve();
    }
    return 0;
}


#include <bits/stdc++.h>
#include <unordered_set>
#include <unordered_map>
using namespace std;
using LL = int64_t;
using pii = pair<int, int>;
const int SZ = 1000000 + 10;
string g_s;

void prete() {}
void input() {}
string output(int k) {
    string s;
    for(int i = 0; i < k; ++i) {
        if(i < k / 2) {
            s.push_back('4');
        } else {
            s.push_back('7');
        }
    }
    return s;
}
void solve() {
    string s;
    int len = g_s.length();
    if(len & 1) {
        s = output(len + 1);
        cout << s << endl;
        return;
    }
    s = output(len);
    if(s > g_s) {
        cout << s << endl;
        return;
    }
    reverse(s.begin(), s.end());
    if(s < g_s) {
        s = output(len + 2);
        cout << s << endl;
        return;
    }
    if(s == g_s) {
        cout << s << endl;
        return;
    }
    s.clear();
    int cnt7 = len / 2, cnt4 = len / 2;
    for(int i = 0; i < len; ++i) {
        char ch = g_s[i];
        if(ch > '7') {
            while(s.back() == '7') {
                s.pop_back();
                ++cnt7;
            }
            s.pop_back();
            ++cnt4;
            --cnt7;
            s.push_back('7');
            break;
        } else if(ch == '7') {
            if(cnt7 > 0) {
                --cnt7;
                s.push_back('7');
            } else {
                bool f = true;
                while(f) {
                    if(s.back() == '4') {
                        s.pop_back();
                        ++cnt4;
                    } else {
                        s.pop_back();
                        ++cnt7;
                        f = false;
                    }
                }
                while(s.back() == '7') {
                    s.pop_back();
                    ++cnt7;
                }
                s.pop_back();
                ++cnt4;
                s.push_back('7');
                --cnt7;
                break;
            }
        } else if('4' < ch && ch < '7') {
            if(cnt7 > 0) {
                --cnt7;
                s.push_back('7');
            } else {
                bool f = true;
                while(f) {
                    if(s.back() == '4') {
                        s.pop_back();
                        ++cnt4;
                    } else {
                        s.pop_back();
                        ++cnt7;
                        f = false;
                    }
                }
                while(s.back() == '7') {
                    s.pop_back();
                    ++cnt7;
                }
                s.pop_back();
                ++cnt4;
                s.push_back('7');
                --cnt7;
            }
            break;
        } else if(ch == '4') {
            if(cnt4 > 0) {
                --cnt4;
                s.push_back('4');
            } else {
                --cnt7;
                s.push_back('7');
                break;
            }
        } else if(ch < '4') {
            if(cnt4 > 0) {
                --cnt4;
                s.push_back('4');
            } else {
                --cnt7;
                s.push_back('7');
            }
            break;
        }
    }
    for(int i = 0; i < cnt4; ++i) {
        s.push_back('4');
    }
    for(int i = 0; i < cnt7; ++i) {
        s.push_back('7');
    }
    cout << s << endl;
}

int main() {
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\in.txt", "r", stdin);
    //freopen("C:\\Users\\Wanderflyle\\Desktop\\out.txt", "w", stdout);
    prete();
    //int t;
    //cin >> t;
    //for(int i = 1; i <= t; ++i) {
    //    cin >> g_n;
    while(cin >> g_s) {
        input();
        solve();
    }
    return 0;
}


#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <iostream>
#include <cmath>
#include <sstream>
#include <map>
#include <set>
#include <numeric>
#include <memory.h>
#include <cstdio>
#include <assert.h>

using namespace std;

#define pb push_back
#define INF 1011111111
#define FOR(i, a, b) for (int _n(b), i(a); i < _n; i++)
#define rep(i, n) FOR(i, 0, n)
#define CL(a, v) memset((a), (v), sizeof(a))
#define mp make_pair
#define X first
#define Y second
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))

typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;

/*** TEMPLATE CODE ENDS HERE */

string a, s;

bool go(int pos, int n4, int n7, bool bigger = false) {
  if (bigger || pos == (int)s.size()) {
    rep(i, n7) a.pb('7');
    rep(i, n4) a.pb('4');
    return true;
  }
  if (n4 && s[pos] <= '4' && go(pos + 1, n4 - 1, n7, s[pos] < '4')) {
    a.pb('4');
    return true;
  }
  if (n7 && s[pos] <= '7' && go(pos + 1, n4, n7 - 1, s[pos] < '7')) {
    a.pb('7');
    return true;
  }
  return false;
}

int main() {
#ifdef LOCAL_HOST
  freopen("input.txt", "r", stdin);
// freopen("output.txt","w",stdout);
#endif

  ios_base::sync_with_stdio(false);

  cin >> s;
  int n = (int)s.size();

  if (n & 1) {
    ++n;
    rep(i, n / 2) cout << '4';
    rep(i, n / 2) cout << '7';
  } else {
    if (!go(0, n / 2, n / 2)) {
      n += 2;
      rep(i, n / 2) cout << '4';
      rep(i, n / 2) cout << '7';
    } else {
      reverse(all(a));
      cout << a;
    }
  }

  cout << endl;

#ifdef LOCAL_HOST
  printf("TIME: %.3lf\n", double(clock()) / CLOCKS_PER_SEC);
#endif

  return 0;
}


#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <iostream>
#include <cmath>
#include <sstream>
#include <map>
#include <set>
#include <numeric>
#include <memory.h>
#include <cstdio>
#include <assert.h>

using namespace std;

#define pb push_back
#define INF 1011111111
#define FOR(i, a, b) for (int _n(b), i(a); i < _n; i++)
#define rep(i, n) FOR(i, 0, n)
#define CL(a, v) memset((a), (v), sizeof(a))
#define mp make_pair
#define X first
#define Y second
#define all(c) (c).begin(), (c).end()
#define SORT(c) sort(all(c))

typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> pii;

/*** TEMPLATE CODE ENDS HERE */

string a, s;

bool go(int pos, int n4, int n7, bool bigger = false) {
  if (bigger || pos == (int)s.size()) {
    rep(i, n7) a.pb('7');
    rep(i, n4) a.pb('4');
    return true;
  }
  if (n4 && s[pos] - '0' <= 4 && go(pos + 1, n4 - 1, n7, s[pos] - '0' < 4)) {
    a.pb('4');
    return true;
  }
  if (n7 && s[pos] - '0' <= 7 && go(pos + 1, n4, n7 - 1, s[pos] - '0' < 7)) {
    a.pb('7');
    return true;
  }
  return false;
}

int main() {
#ifdef LOCAL_HOST
  freopen("input.txt", "r", stdin);
// freopen("output.txt","w",stdout);
#endif

  ios_base::sync_with_stdio(false);

  cin >> s;
  int n = (int)s.size();

  if (n & 1) {
    ++n;
    rep(i, n / 2) cout << '4';
    rep(i, n / 2) cout << '7';
  } else {
    if (!go(0, n / 2, n / 2)) {
      n += 2;
      rep(i, n / 2) cout << '4';
      rep(i, n / 2) cout << '7';
    } else {
      reverse(all(a));
      cout << a;
    }
  }

  cout << endl;

#ifdef LOCAL_HOST
  printf("TIME: %.3lf\n", double(clock()) / CLOCKS_PER_SEC);
#endif

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int a[maxn], b[maxn];
int c[2];
string s;
int num_4,num_7;
int left1;
int cnt;
int flag;
bool panduan()
{
    //printf("!!!323!! %d\n", cnt);
    if(b[cnt] < a[cnt])
    {
        //printf("!!!!!%d\n", cnt);
        return false;
    }

    else
    {
        int dis = fabs(num_4 - num_7);
        //printf("%d %d\n", dis, cnt);
        if(dis > left1)
            return false;
    }

    return true;
}

void dfs()
{

    if(flag == 1)
        return;

    else if(left1 == 0 && num_4 == num_7)
    {
        flag = 1;
        //printf("----%d\n", cnt);
        return;
    }

    else
    {
        int i;
        for(i = 0; i <= 1; i++)
        {
            if(flag == 1)
                return;
            b[cnt] = c[i];

            if(c[i] == 4)
                num_4++;
            else
                num_7++;
            left1--;

            if(panduan())
            {

                cnt++;
                dfs();
                if(flag == 1)
                    return;
                if(c[i] == 4)
                    num_4--;
                else if(c[i] == 7)
                    num_7--;
                left1++;
                cnt--;
            }

            else
            {
                left1++;
                if(c[i] == 4)
                    num_4--;
                else
                    num_7--;
            }
        }
    }
}

int main()
{
    cin >> s;
    int len = s.size();
    left1 = len;
    cnt = 0;
    c[0] = 4;
    c[1] = 7;

    for(int j = 0; j < s.size(); j++)
        a[j] = s[j] - '0';

    if(len % 2 == 1)
    {
        int i;
        for(i = 0; i <= len / 2; i++)
            a[i] = 4;
        for(i = len / 2 + 1; i <= len; i++)
            a[i] = 7;

        for(i = 0; i <= len; i++)
            printf("%d", a[i]);
        return 0;
    }

    else if(len % 2 == 0)
    {
        flag = 1;
        int i;

        for(i = 0; i < len / 2; i++)
            if(a[i] < 7)
        {
            flag = 0;
            break;
        }

        if(flag == 1)
        {
            len = len + 2;

            for(i = 0; i< len / 2; i++)
                a[i] = 4;
            for(i = len / 2;i < len; i++)
                a[i] = 7;
            for(i = 0; i < len; i++)
                printf("%d", a[i]);
            return 0;
        }

        else if(flag == 0)
        {
            for(i = 0; i <= 1; i++)
            {
                if(flag == 1)
                    break;
                b[cnt] = c[i];

                if(c[i] == 4)
                    num_4++;
                else if(c[i]== 7)
                    num_7++;
                left1--;
                if(panduan())
                {
                    cnt++;
                    dfs();
                    if(flag== 1)
                        break;
                    if(c[i] == 4)
                       num_4--;
                    else if(c[i]==7)
                       num_7--;
                    left1++;
                    cnt--;
                }
                else
                {
                    left1++;
                    if(c[i] == 4)
                        num_4--;
                    else if(c[i] == 7)
                        num_7--;
                }
            }

            for(i = 0; i < len; i++)
                printf("%d", b[i]);
            return 0;
        }
    }
}


#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

const int maxn = 1e5 + 10;

char s[maxn];
int len, f = 0;

void dfs(char *s1, int loc, int cnt4, int cnt7)
{

    if(f || cnt4 > len / 2 || cnt7 > len / 2)//我这里是有个数限制的
        return;

    int c = s1[loc-1] - s[loc-1];
    //printf("%d %d\n", loc, c);
    //55555555555555555555555555555555555555555555555555printf("-----%c %c\n",s1[loc-1], s[loc-1]);

    if(c > 0)
    {
        int i;

        for(;cnt4 < len / 2; cnt4++)
            s1[loc++] = '4';

        for(; loc < len; loc++)
           s1[loc] = '7';

        s1[loc] = '\0';
        f = 1;
        return;
    }

    else if(loc == len && c >= 0)
    {
        f = 1;
        return;
    }

    else if(c == 0)
    {
        //printf("--------------\n");
        //printf("%d %d\n", loc, c);
        //printf("-----%c %c\n",s1[loc-1], s[loc-1]);
        s1[loc] = '4';
        //printf("%c ",s1[loc]);
        dfs(s1, loc+1, cnt4+1, cnt7);
        if(!f)
        {
            s1[loc] = '7';
            dfs(s1, loc+1, cnt4, cnt7+1);
        }
    }
}

int main()
{
    scanf("%s", s);
    len = strlen(s);
    char s1[maxn];
    int i;

    if(len % 2 == 1)
    {
        for(i = 0; i <= len / 2; i++)
            s1[i] = '4';
        for(i = len / 2 + 1; i <= len; i++)
            s1[i] = '7';
        for(i = 0; i <= len; i++)
            printf("%c", s1[i]);
        return 0;
    }

    else if(len % 2 == 0)
    {
        char ans[maxn];
        for(i = 0; i < len / 2; i++)
            ans[i] = '7';
        for(i = len / 2; i < len; i++)
            ans[i] = '4';

        if(strcmp(ans, s) < 0)
        {
            len = len + 2;
            for(i = 0; i < len / 2; i++)
                ans[i] = '4';
            for(i = len / 2; i < len; i++)
                ans[i] = '7';
            for(i = 0; i < len; i++)
                printf("%c", ans[i]);
            return 0;
        }

        else
        {
            s1[0] = '4';
            dfs(s1, 1, 1, 0);
            //printf("------%d\n", f);
            if(!f)
            {
                s1[0] = '7';
                dfs(s1, 1, 0, 1);
            }

            //printf("%d\n", f);
            for(i = 0; i < len; i++)
                printf("%c", s1[i]);
            return 0;
        }
    }
}


#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

const int maxn = 1e5 + 10;

char s[maxn];
int len, f = 0;

void dfs(char *s1, int loc, int cnt4, int cnt7)
{

    if(f || cnt4 > len / 2 || cnt7 > len / 2)//我这里是有个数限制的
        return;

    int c = s1[loc-1] - s[loc-1];
    //printf("%d %d\n", loc, c);
    //55555555555555555555555555555555555555555555555555printf("-----%c %c\n",s1[loc-1], s[loc-1]);

    if(c > 0)
    {
        int i;

        for(;cnt4 < len / 2; cnt4++)
            s1[loc++] = '4';

        for(; loc < len; loc++)
           s1[loc] = '7';

        s1[loc] = '\0';
        f = 1;
        return;
    }

    else if(loc == len && c >= 0)
    {
        f = 1;
        return;
    }

    else if(c == 0)
    {
        //printf("--------------\n");
        //printf("%d %d\n", loc, c);
        //printf("-----%c %c\n",s1[loc-1], s[loc-1]);
        s1[loc] = '4';
        //printf("%c ",s1[loc]);
        dfs(s1, loc+1, cnt4+1, cnt7);
        if(!f)
        {
            s1[loc] = '7';
            dfs(s1, loc+1, cnt4, cnt7+1);
        }
    }
}

int main()
{
    scanf("%s", s);
    len = strlen(s);
    char s1[maxn];
    int i;

    if(len % 2 == 1)
    {
        for(i = 0; i <= len / 2; i++)
            s1[i] = '4';
        for(i = len / 2 + 1; i <= len; i++)
            s1[i] = '7';
        for(i = 0; i <= len; i++)
            printf("%c", s1[i]);
        return 0;
    }

    else if(len % 2 == 0)
    {
        char ans[maxn];
        for(i = 0; i < len / 2; i++)
            ans[i] = '7';
        for(i = len / 2 + 1; i < len; i++)
            ans[i] = '4';

        if(strcmp(ans, s) < 0)
        {
            len = len + 2;
            for(i = 0; i < len / 2; i++)
                ans[i] = '4';
            for(i = len / 2; i < len; i++)
                ans[i] = '7';
            for(i = 0; i < len; i++)
                printf("%c", ans[i]);
            return 0;
        }

        else
        {
            s1[0] = '4';
            dfs(s1, 1, 1, 0);
            //printf("------%d\n", f);
            if(!f)
            {
                s1[0] = '7';
                dfs(s1, 1, 0, 1);
            }

            //printf("%d\n", f);
            for(i = 0; i < len; i++)
                printf("%c", s1[i]);
            return 0;
        }
    }
}


#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;

const int maxn = 1e5 + 10;

char s[maxn];
int len, f = 0;

void dfs(char *s1, int loc, int cnt4, int cnt7)
{

    if(f || cnt4 > len / 2 || cnt7 > len / 2)//我这里是有个数限制的
        return;

    int c = s1[loc-1] - s[loc-1];
    //printf("%d %d\n", loc, c);
    //55555555555555555555555555555555555555555555555555printf("-----%c %c\n",s1[loc-1], s[loc-1]);

    if(c > 0)
    {
        int i;

        for(;cnt4 < len / 2; cnt4++)
            s1[loc++] = '4';

        for(; loc < len; loc++)
           s1[loc] = '7';

        s1[loc] = '\0';
        f = 1;
        return;
    }

    else if(loc == len && c >= 0)
    {
        f = 1;
        return;
    }

    else if(c == 0)
    {
        //printf("--------------\n");
        //printf("%d %d\n", loc, c);
        //printf("-----%c %c\n",s1[loc-1], s[loc-1]);
        s1[loc] = '4';
        //printf("%c ",s1[loc]);
        dfs(s1, loc+1, cnt4+1, cnt7);
        if(!f)
        {
            s1[loc] = '7';
            dfs(s1, loc+1, cnt4, cnt7+1);
        }
    }
}

int main()
{
    scanf("%s", s);
    len = strlen(s);
    char s1[maxn];
    int i;

    if(len % 2 == 1)
    {
        for(i = 0; i <= len / 2; i++)
            s1[i] = '4';
        for(i = len / 2 + 1; i <= len; i++)
            s1[i] = '7';
        for(i = 0; i <= len; i++)
            printf("%c", s1[i]);
        return 0;
    }

    else if(len % 2 == 0)
    {
        char ans[maxn];
        for(i = 0; i < len / 2; i++)
            ans[i] = '7';
        for(i = len / 2 + 1; i < len; i++)
            ans[i] = '4';

        if(strcmp(ans, s) < 0)
        {
            len = len + 2;
            for(i = 0; i < len / 2; i++)
                ans[i] = '4';
            for(i = len / 2 + 1; i < len; i++)
                ans[i] = '7';
            for(i = 0; i < len; i++)
                printf("%c", ans[i]);
            return 0;
        }

        else
        {
            s1[0] = '4';
            dfs(s1, 1, 1, 0);
            //printf("------%d\n", f);
            if(!f)
            {
                s1[0] = '7';
                dfs(s1, 1, 0, 1);
            }

            //printf("%d\n", f);
            for(i = 0; i < len; i++)
                printf("%c", s1[i]);
            return 0;
        }
    }
}


#include <bits/stdc++.h>
using namespace std;

vector<long long> v;

void dfs(string s="",int c4=0, int c7=0){
    if(c4+c7>10){
        return;
    }
    if(c4||c7){
        if(c7==c4){
            v.push_back(stoll(s));
        }
    }
    dfs(s+'4',c4+1,c7);
    dfs(s+'7',c4,c7+1);
}

int main(){
    int n;
    cin>>n;
    dfs();
    sort(v.begin(),v.end());
    cout<<*lower_bound(v.begin(),v.end(),n);
}


//Luckey Numbers(easy) sol1 COPY;
//Q:Lower_bound
#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double ld;
typedef pair<int, int> pi;
typedef vector<int> vi;

const int maxn=10010;
ll a[maxn];
int ac=0;

void go(int k,ll num,int c4,int c7) {
	if(k==0) {
		if(c4==c7)
			a[ac++]=num;
	} else go(k-1,num*10+4,c4+1,c7),go(k-1,num*10+7,c4,c7+1);
}
int main() {
	int n;
	scanf("%d",&n);
	for(int i=2; i<=10; i++) {
		go(i,0,0,0);
	}
	sort(a,a+ac);
    int it=lower_bound(a,a+ac,n)-a;
	cout<<a[it]<<endl;
	return 0;
}


//Codeforces - Lucky Numbers(easy)1 - 96B -Prove on 2018/6/27
#include<bits/stdc++.h>
using namespace std;
int main()
{
	vector<long long> ans;
	string x="47";
	int y;
	cin>>y;
	while(x.size()<9)
	{
		do{
			ans.push_back(stoi(x));
		}
		while(next_permutation(x.begin(),x.end()));
		x+="47";
		sort(x.begin(),x.end());
	}
	ans.push_back(74444444444777777777);
	for(int i=0;i<ans.size();++i)
	{
		if(ans[i]>=y)
		{
			cout<<ans[i];
			break;
		}
	}
	return 0;
}


#include <bits/stdc++.h>

#define INF 1<<31
#define f(i,j,k) for(int (i)=(j);(i)<=(k);(i)++)

using namespace std;
const int maxn = 1e5 + 10;
string s, res;
int len;

bool solve(int pos, int r4, int r7, bool bigger) {
    if (pos == len) return true;
    if (bigger) {
        while (r7--) res += '7';
        while (r4--) res += '4';
        return true;
    }

    if(s[pos]<='4'&&r4>=1&&solve(pos+1,r4-1,r7,s[pos]<'4')){
        res+='4';
        return true;
    }
    else if(s[pos]<='7'&&r7>=1&&solve(pos+1,r4,r7-1,s[pos]<'7')){
        res+='7';
        return true;
    }
    return false;
}

int main() {
#ifdef CONTEST
    freopen("input.in", "r", stdin);
//    freopen("output.o", "w", stdout);
#endif
    cin >> s;
    len = s.length();

    if (len&1) {
        cout << string((len+1)/2, '4') << string((len+1)/2, '7') << endl;
    } else {
        if (string(len/2, '7') + string(len/2, '4') < s) {
            cout << string(len/2+1, '4') << string(len/2+1, '7') << endl;
        } else {
            solve(0, len/2, len/2, false);
            reverse(res.begin(), res.end());
            cout << res << endl;
        }
    }

}


//Codeforces - Lucky Numbers(easy)1 - 96B -Prove on 2018/6/27
#include<bits/stdc++.h>
using namespace std;
int main()
{
	vector<long long> ans;
	string x="47";
	int y;
	cin>>y;
	while(x.size()<9)
	{
		do{
			ans.push_back(stoi(x));
		}
		while(next_permutation(x.begin(),x.end()));
		x+="47";
		sort(x.begin(),x.end());
	}
	ans.push_back(7777777777777777777777777777777777777777777777777777777777777);
	for(int i=0;i<ans.size();++i)
	{
		if(ans[i]>=y)
		{
			cout<<ans[i];
			break;
		}
	}
	return 0;
}


//Codeforces - Lucky Numbers(easy)1 - 96B -Prove on 2018/6/27
#include<bits/stdc++.h>
using namespace std;
int main()
{
	vector<long long> ans;
	string x="47";
	int y;
	cin>>y;
	while(x.size()<9)
	{
		do{
			ans.push_back(stoi(x));
		}
		while(next_permutation(x.begin(),x.end()));
		x+="47";
		sort(x.begin(),x.end());
	}
	ans.push_back(4444477777);
	for(int i=0;i<ans.size();++i)
	{
		if(ans[i]>=y)
		{
			cout<<ans[i];
			break;
		}
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=1000005;
char s[maxn],c[maxn];
int n,flag;
void dfs(int pos,int cnt,int fp)
{
	if(flag||cnt>n/2||pos-1-cnt>n/2)return ;
	if(pos>n)
	{
		c[pos]='\0';
		if(cnt==n/2&&strcmp(c+1,s+1)>=0)
		{
			printf("%s\n",c+1);
			flag=1;
		}
		return ;
	}
	if(!fp)
	{
		for(int i=pos;i<=n;i++)
			if(cnt<n/2)c[i]='4',cnt++;
			else c[i]='7';
		c[n+1]='\0';
		printf("%s\n",c+1);
		flag=1;
		return ;
	}
	if(s[pos]<='4')
	{
		c[pos]='4';
		dfs(pos+1,cnt+1,s[pos]=='4');
	}
	if(s[pos]<='7')
	{
		c[pos]='7';
		dfs(pos+1,cnt,s[pos]=='7');
	}
}
int main()
{
	while(~scanf("%s",s+1))
	{
		n=strlen(s+1);
		if(n&1)
		{
			for(int i=1;i<=(n+1)/2;i++)printf("4");
			for(int i=1;i<=(n+1)/2;i++)printf("7");
			printf("\n");
			continue;
		}
		else
		{
			for(int i=1;i<=n;i++)
				if(i<=n/2)c[i]='7';
				else c[i]='4';
			c[n+1]='\0';
			if(strcmp(c+1,s+1)<0)
			{
				for(int i=1;i<=n/2+1;i++)printf("4");
				for(int i=1;i<=n/2+1;i++)printf("7");
				printf("\n");
			}
			else
			{
				flag=0;
				dfs(1,0,1);
			}
		}
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=1000005;
char s[maxn],c[maxn];
int n,flag;
void dfs(int pos,int cnt)
{
	if(flag)return ;
	if(pos>n)
	{
		c[pos]='\0';
		if(cnt==n/2&&strcmp(c+1,s+1)>=0)
		{
			printf("%s\n",c+1);
			flag=1;
		}
		return ;
	}
	c[pos]='4';
	dfs(pos+1,cnt+1);
	c[pos]='7';
	dfs(pos+1,cnt);
}
int main()
{
	while(~scanf("%s",s+1))
	{
		n=strlen(s+1);
		if(n&1)
		{
			for(int i=1;i<=(n+1)/2;i++)printf("4");
			for(int i=1;i<=(n+1)/2;i++)printf("7");
			printf("\n");
			continue;
		}
		else
		{
			for(int i=1;i<=n;i++)
				if(i<=n/2)c[i]='7';
				else c[i]='4';
			c[n+1]='\0';
			if(strcmp(c+1,s+1)<0)
			{
				for(int i=1;i<=n/2+1;i++)printf("4");
				for(int i=1;i<=n/2+1;i++)printf("7");
				printf("\n");
			}
			else
			{
				flag=0;
				dfs(1,0);
			}
		}
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=1000005;
char s[maxn],c[maxn];
int n,flag;
void dfs(int pos,int cnt,int fp)
{
	if(flag||cnt>n/2)return ;
	if(pos>n)
	{
		c[pos]='\0';
		if(cnt==n/2&&strcmp(c+1,s+1)>=0)
		{
			printf("%s\n",c+1);
			flag=1;
		}
		return ;
	}
	if(!fp)
	{
		for(int i=pos;i<=n;i++)
			if(cnt<n/2)c[i]='4',cnt++;
			else c[i]='7';
		c[n+1]='\0';
		printf("%s\n",c+1);
		flag=1;
		return ;
	}
	if(s[pos]<='4')
	{
		c[pos]='4';
		dfs(pos+1,cnt+1,fp&&s[pos]=='4');
	}
	if(s[pos]<='7')
	{
		c[pos]='7';
		dfs(pos+1,cnt,fp&&s[pos]=='7');
	}
}
int main()
{
	while(~scanf("%s",s+1))
	{
		n=strlen(s+1);
		if(n&1)
		{
			for(int i=1;i<=(n+1)/2;i++)printf("4");
			for(int i=1;i<=(n+1)/2;i++)printf("7");
			printf("\n");
			continue;
		}
		else
		{
			for(int i=1;i<=n;i++)
				if(i<=n/2)c[i]='7';
				else c[i]='4';
			c[n+1]='\0';
			if(strcmp(c+1,s+1)<0)
			{
				for(int i=1;i<=n/2+1;i++)printf("4");
				for(int i=1;i<=n/2+1;i++)printf("7");
				printf("\n");
			}
			else
			{
				flag=0;
				dfs(1,0,1);
			}
		}
	}
	return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=1000005;
char s[maxn],c[maxn];
int n,flag;
void dfs(int pos,int cnt)
{
	if(flag||cnt>n/2)return ;
	if(pos>n)
	{
		c[pos]='\0';
		if(cnt==n/2&&strcmp(c+1,s+1)>=0)
		{
			printf("%s\n",c+1);
			flag=1;
		}
		return ;
	}
	c[pos]='4';
	dfs(pos+1,cnt+1);
	c[pos]='7';
	dfs(pos+1,cnt);
}
int main()
{
	while(~scanf("%s",s+1))
	{
		n=strlen(s+1);
		if(n&1)
		{
			for(int i=1;i<=(n+1)/2;i++)printf("4");
			for(int i=1;i<=(n+1)/2;i++)printf("7");
			printf("\n");
			continue;
		}
		else
		{
			for(int i=1;i<=n;i++)
				if(i<=n/2)c[i]='7';
				else c[i]='4';
			c[n+1]='\0';
			if(strcmp(c+1,s+1)<0)
			{
				for(int i=1;i<=n/2+1;i++)printf("4");
				for(int i=1;i<=n/2+1;i++)printf("7");
				printf("\n");
			}
			else
			{
				flag=0;
				dfs(1,0);
			}
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 100100
char s[maxn];
char ans[maxn],ans1[maxn];
bool cmp(int len)
{
	for(int i=0;i<len/2;++i)
	{
		if(s[i]>'7') return false;
		if(s[i]<'7') return true;
	}
	for(int i=len/2;i<len;++i)
	{
		if(s[i]>'4') return false;
		if(s[i]<'4') return true;
	}
	return true;
}
int main()
{
	while(~scanf("%s",s))
	{
		int len=strlen(s);
		if(len&1||(!cmp(len)))
		{
			len/=2;
			for(int i=0;i<=len;++i) putchar('4');
			for(int i=0;i<=len;++i) putchar('7');
			puts("");
			continue;
		}
		bool z=true;
		int now=len/2;
		for(int i=0;i<len/2;++i) ans[i]='4';
		for(int i=len/2;i<len;++i) ans[i]='7';
		ans[len]=0;
		for(int i=0;i<len;++i)
		{
			if(!z) break;
			if(s[i]>ans[i])
			{
				swap(ans[i],ans[now]);
				now++;
			}
			if(s[i]<ans[i])	z=false;
		}
		if(strcmp(ans,s)>=0) puts(ans);
		else
		{
			for(int i=0;i<len/2;++i) ans[i]='4';
			for(int i=len/2;i<len;++i) ans[i]='7';
			z=true; now=len/2;
			for(int i=0;i<len;++i)
			{
				if(!z) break;
				if(s[i]>=ans[i])
				{
					swap(ans[i],ans[now]);
					now++;
				}
				if(s[i]<ans[i]) z=false;
			}
			puts(ans);
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 100100
char s[maxn];
char ans[maxn],ans1[maxn];
bool cmp(int len)
{
	for(int i=0;i<len/2;++i)
	{
		if(s[i]>'7') return false;
		if(s[i]<'7') return true;
	}
	for(int i=len/2;i<len;++i)
	{
		if(s[i]>'4') return false;
		if(s[i]<'4') return true;
	}
	return true;
}
int main()
{
	while(~scanf("%s",s))
	{
		int len=strlen(s);
		if(len&1||(!cmp(len)))
		{
			len/=2;
			for(int i=0;i<=len;++i) putchar('4');
			for(int i=0;i<=len;++i) putchar('7');
			puts("");
			continue;
		}
		bool z=true;
		int now=len/2;
		for(int i=0;i<len/2;++i) ans[i]='4';
		for(int i=len/2;i<len;++i) ans[i]='7';
		ans[len]=0;
		for(int i=0;i<len;++i)
		{
			if(!z) break;
			//now=max(now,i+1);
			if(s[i]>ans[i]&&now<len)
			{
				swap(ans[i],ans[now]);
				now++;
			}
			if(s[i]<ans[i])	z=false;
		}		//printf("cs4=%d,cs7=%d,ca4=%d,ca7=%d\n",cs4,cs7,ca4,ca7);

		//printf("strcmp(ans,s)=%d,len(s)=%d,len(ans)=%d\n",strcmp(ans,s),(int)strlen(s),(int)strlen(ans));
		if(strcmp(ans,s)>=0) puts(ans);
		else
		{
			int las4=-1,las7=-1;
			for(int i=len-1;i>=0;--i)
			{
				if(s[i]==ans[i]&&s[i]=='4')
				{
					las4=i;
					break;
				}
				if(ans[i]=='7') las7=i;
			}
			swap(ans[las4],ans[las7]);
			puts(ans);

		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 100100
char s[maxn];
char ans[maxn];
bool cmp(int len)
{
	for(int i=0;i<len/2;++i)
	{
		if(s[i]>'7') return false;
		if(s[i]<'7') return true;
	}
	for(int i=len/2;i<len;++i)
	{
		if(s[i]>'4') return false;
		if(s[i]<'4') return true;
	}
	return true;
}
int main()
{
	while(~scanf("%s",s))
	{
		int len=strlen(s);
		if(len&1||(!cmp(len)))
		{
			len/=2;
			for(int i=0;i<=len;++i) putchar('4');
			for(int i=0;i<=len;++i) putchar('7');
			puts("");
			continue;
		}
		bool z=true;
		int now=len/2;
		for(int i=0;i<len/2;++i) ans[i]='4';
		for(int i=len/2;i<len;++i) ans[i]='7';
		ans[len]=0;
		for(int i=0;i<len;++i)
		{
			if(!z) break;
			if(s[i]>ans[i])
			{
				swap(ans[i],ans[now]);
				now++;
			}
			if(s[i]<ans[i])	z=false;
		}
		puts(ans);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 100100
char s[maxn];
char ans[maxn],ans1[maxn];
bool cmp(int len)
{
	for(int i=0;i<len/2;++i)
	{
		if(s[i]>'7') return false;
		if(s[i]<'7') return true;
	}
	for(int i=len/2;i<len;++i)
	{
		if(s[i]>'4') return false;
		if(s[i]<'4') return true;
	}
	return true;
}
int main()
{
	while(~scanf("%s",s))
	{
		int len=strlen(s);
		if(len&1||(!cmp(len)))
		{
			len/=2;
			for(int i=0;i<=len;++i) putchar('4');
			for(int i=0;i<=len;++i) putchar('7');
			puts("");
			continue;
		}
		bool z=true;
		int now=len/2;
		for(int i=0;i<len/2;++i) ans[i]='4';
		for(int i=len/2;i<len;++i) ans[i]='7';
		ans[len]=0;
		for(int i=0;i<len;++i)
		{
			if(!z) break;
			//now=max(now,i+1);
			if(s[i]>ans[i]&&now<len)
			{
				swap(ans[i],ans[now]);
				now++;
			}
			if(s[i]<ans[i])	z=false;
		}		//printf("cs4=%d,cs7=%d,ca4=%d,ca7=%d\n",cs4,cs7,ca4,ca7);

		//printf("strcmp(ans,s)=%d,len(s)=%d,len(ans)=%d\n",strcmp(ans,s),(int)strlen(s),(int)strlen(ans));
		if(strcmp(ans,s)>=0) puts(ans);
		else
		{
			int las4=-1,las7=-1;
			for(int i=len-1;i>=0;--i)
			{
				if(s[i]==ans[i]&&s[i]=='4')
				{
					las4=i;
				}
				if(ans[i]=='7') las7=i;
			}
			swap(ans[las4],ans[las7]);
			puts(ans);

		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define maxn 100100
char s[maxn];
char ans[maxn],ans1[maxn];
bool cmp(int len)
{
	for(int i=0;i<len/2;++i)
	{
		if(s[i]>'7') return false;
		if(s[i]<'7') return true;
	}
	for(int i=len/2;i<len;++i)
	{
		if(s[i]>'4') return false;
		if(s[i]<'4') return true;
	}
	return true;
}
int main()
{
	while(~scanf("%s",s))
	{
		int len=strlen(s);
		if(len&1||(!cmp(len)))
		{
			len/=2;
			for(int i=0;i<=len;++i) putchar('4');
			for(int i=0;i<=len;++i) putchar('7');
			puts("");
			continue;
		}
		bool z=true;
		int now=len/2;
		for(int i=0;i<len/2;++i) ans[i]='4';
		for(int i=len/2;i<len;++i) ans[i]='7';
		ans[len]=0;
		for(int i=0;i<len;++i)
		{
			if(!z) break;
			//now=max(now,i+1);
			if(s[i]>ans[i]&&now<len)
			{
				swap(ans[i],ans[now]);
				now++;
			}
			if(s[i]<ans[i])	z=false;
		}		//printf("cs4=%d,cs7=%d,ca4=%d,ca7=%d\n",cs4,cs7,ca4,ca7);
		//puts(ans);
		//printf("strcmp(ans,s)=%d,len(s)=%d,len(ans)=%d\n",strcmp(ans,s),(int)strlen(s),(int)strlen(ans));
		if(strcmp(ans,s)>=0) puts(ans);
		else
		{
			int las4=-1,las7=-1;
			bool z=true;
			for(int i=0;i<len;++i)
			{
				if(z&&s[i]==ans[i]&&s[i]=='4')
				{
					las4=i;
				}
				if(s[i]>ans[i]) z=false;
			}
			for(int i=las4+1;i<len;++i)
				if(ans[i]=='7')
				{
					las7=i;
					break;
				}
			swap(ans[las4],ans[las7]);
			puts(ans);

		}
	}
	return 0;
}


#include<bits/stdc++.h>
#include<cstdint>
#define loop(i,r) for(ll i=0;i<r;i++)
typedef long long int ll;

using namespace std;

int main()
{  ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s;
    cin>>s;
    ll i,j,flag1=0,flag2=0,n=0;
    for(i=0;s[i]!='\0';i++)
    n++;

    for(i=0;s[i]!='\0';i++)
    {
     if(s[i]=='A' && s[i+1]=='B')
     {
        if(s[i+2]=='A')
        {j=i+3;flag2++;break;}
        else
        {j=i+2;flag1++;break;}}
     if(s[i]=='B' && s[i+1]=='A')
     {if(s[i+2]=='B')
     {j=i+3;flag1++;break;}
     else
     {j=i+2;flag2++;break;}}

    }

    if(flag1==0 && flag2==0)
    cout<<"NO";
    if(flag1)
    {for(i=j;s[i]!='\0';i++)
     if(s[i]=='B' && s[i+1]=='A')
     {j=i+2;flag2++;
     cout<<"YES";break;}
     if(i==n)
     cout<<"NO";
    }
     else if(flag2)
    {for(i=j;s[i]!='\0';i++)
     if(s[i]=='A' && s[i+1]=='B')
     {j=i+2;flag2++;
     cout<<"YES";break;}
     if(i==n)
     cout<<"NO";
    }

    }


#include<bits/stdc++.h>
#include<cstdint>
#define loop(i,r) for(ll i=0;i<r;i++)
typedef long long int ll;
using namespace std;
int main()
{  ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    char s[100005];
    cin>>s;
    char *p;
    if((p=strstr(s,"AB"))&& strstr(p+2,"BA"))
    cout<<"YES";
    else if((p=strstr(s,"BA"))&& strstr(p+2,"AB"))
    cout<<"YES";
    else cout<<"NO";

    return 0;
}


#include<bits/stdc++.h>
#include<cstdint>
#define loop(i,r) for(ll i=0;i<r;i++)
typedef long long int ll;

using namespace std;

int main()
{  ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s;
    cin>>s;
    ll i,j,flag1=0,flag2=0,n=0;
    for(i=0;s[i]!='\0';i++)
    n++;

    for(i=0;s[i]!='\0';i++)
    {
     if(s[i]=='A' && s[i+1]=='B')
     {
        if(s[i+2]=='A' && (n%2==0))
        {j=i+3;flag2++;break;}
        else
        {j=i+2;flag1++;break;}}
     if(s[i]=='B' && s[i+1]=='A')
     {if(s[i+2]=='B' && (n%2==0))
     {j=i+3;flag1++;break;}
     else
     {j=i+2;flag2++;break;}}

    }

    if(flag1==0 && flag2==0)
    cout<<"NO";
    if(flag1)
    {for(i=j;s[i]!='\0';i++)
     if(s[i]=='B' && s[i+1]=='A')
     {
     cout<<"YES";break;}
     if(i==n)
     cout<<"NO";
    }
     else if(flag2)
    {for(i=j;s[i]!='\0';i++)
     if(s[i]=='A' && s[i+1]=='B')
     {
     cout<<"YES";break;}
     if(i==n)
     cout<<"NO";
    }

    }


#include <iostream>
#include<unordered_map>
#include<vector>
using namespace std;
int main() {
    string a;
    cin>>a;
    int t1=0,t2=0;
    unordered_map<string,vector<int>> m;
    for(int i=0;i<a.size()-1;i++){
        if(a[i]=='A' && a[i+1]=='B'){
            m["AB"].push_back(i+1);
            t1=1;
        }
        if(a[i]=='B' && a[i+1]=='A'){
            m["BA"].push_back(i);
            t2=1;
        }
    }
    if(m["AB"].size()>1 || m["BA"].size()>1){
        cout<<"YES"<<endl;
    }
    else if(t1 && t2){
        if(m["AB"][0] != m["BA"][0])
        cout<<"YES"<<endl;
    }
    else{
        cout<<"NO"<<endl;
    }

}


#include <iostream>
#include<unordered_map>
#include<vector>
using namespace std;
int main() {
    string a;
    cin>>a;
    int t1=0,t2=0;
    unordered_map<string,vector<int>> m;
    for(int i=0;i<a.size()-1;i++){
        if(a[i]=='A' && a[i+1]=='B'){
            m["AB"].push_back(i+1);
            t1=1;
        }
        if(a[i]=='B' && a[i+1]=='A'){
            m["BA"].push_back(i);
            t2=1;
        }
    }
    if((m["AB"].size()>1 && m["BA"].size()) || (m["BA"].size()>1 && m["AB"].size())){
        cout<<"YES"<<endl;
    }
    else if(m.count("AB") && m.count("BA")){
        if(!m["AB"].size() || !m["BA"].size()){
            cout<<"NO";
        }
        else if((m["AB"][0] != m["BA"][0] ) && (m["BA"][0]+2 != m["AB"][0]))
        cout<<"YES"<<endl;
        else{
            cout<<"NO";
        }
    }
    else{
        cout<<"NO"<<endl;
    }

}


#include <iostream>
#include<unordered_map>
#include<vector>
using namespace std;
int main() {
    string a;
    cin>>a;
    int t1=0,t2=0;
    unordered_map<string,vector<int>> m;
    for(int i=0;i<a.size()-1;i++){
        if(a[i]=='A' && a[i+1]=='B'){
            m["AB"].push_back(i+1);
            t1=1;
        }
        if(a[i]=='B' && a[i+1]=='A'){
            m["BA"].push_back(i);
            t2=1;
        }
    }
    if(m["AB"].size()>1 || m["BA"].size()>1){
        cout<<"YES";
    }
    else if(t1 && t2){
        if(m["AB"][0] != m["BA"][0])
        cout<<"YES";
    }
    else{
        cout<<"NO";
    }

}


#include <iostream>
#include<unordered_map>
#include<vector>
using namespace std;
int main() {
    string a;
    cin>>a;
    int t1=0,t2=0;
    unordered_map<string,vector<int>> m;
    for(int i=0;i<a.size()-1;i++){
        if(a[i]=='A' && a[i+1]=='B'){
            m["AB"].push_back(i+1);
            t1=1;
        }
        if(a[i]=='B' && a[i+1]=='A'){
            m["BA"].push_back(i);
            t2=1;
        }
    }
    if(m["AB"].size()>1 || m["BA"].size()>1){
        cout<<"YES"<<endl;
    }
    else if(m.count("AB") && m.count("BA")){
        if(!m["AB"].size() && !m["BA"].size()){
            cout<<"NO";
        }
        else if(m["AB"][0] != m["BA"][0])
        cout<<"YES"<<endl;
        else{
            cout<<"NO";
        }
    }
    else{
        cout<<"NO"<<endl;
    }

}


#include <iostream>
using namespace std;
int main() {
    string a;
    cin>>a;
    bool ans1=false,ans2=false;
    for(int i=0;i<a.size();i++){
        if(a[i]=='A' && a[i+1]=='B' && a[i+2]!='A' && !ans1){
            ans1=true;
        }
        if(a[i+1]=='A' && a[i]=='B' && a[i-1]!='A' && !ans2){
            ans2=true;
        }
        if(ans1 && ans2){
            break;
        }
    }
    if(ans1 && ans2){
        cout<<"YES";
    }
    else{
        cout<<"NO";
    }
}


#include <iostream>
#include<unordered_map>
#include<vector>
using namespace std;
int main() {
    string a;
    cin>>a;
    int t1=0,t2=0;
    unordered_map<string,vector<int>> m;
    for(int i=0;i<a.size()-1;i++){
        if(a[i]=='A' && a[i+1]=='B'){
            m["AB"].push_back(i+1);
            t1=1;
        }
        if(a[i]=='B' && a[i+1]=='A'){
            m["BA"].push_back(i);
            t2=1;
        }
    }
    if(a=="ABAB"){
        cout<<"NO";
    }
    else if((m["AB"].size()>1 && m["BA"].size()) || (m["BA"].size()>1 && m["AB"].size())){
        cout<<"YES"<<endl;
    }
    else if(m.count("AB") && m.count("BA")){
        if(!m["AB"].size() || !m["BA"].size()){
            cout<<"NO";
        }
        else if((m["AB"][0] != m["BA"][0] ) && (m["BA"][0]+2 != m["AB"][0]))
        cout<<"YES"<<endl;
        else{
            cout<<"NO";
        }
    }
    else{
        cout<<"NO"<<endl;
    }

}


#include <iostream>
#include<unordered_map>
#include<vector>
using namespace std;
int main() {
    string a;
    cin>>a;
    int t1=0,t2=0;
    unordered_map<string,vector<int>> m;
    for(int i=0;i<a.size()-1;i++){
        if(a[i]=='A' && a[i+1]=='B'){
            m["AB"].push_back(i+1);
            t1=1;
        }
        if(a[i]=='B' && a[i+1]=='A'){
            m["BA"].push_back(i);
            t2=1;
        }
    }
    if((m["AB"].size()>1 && m["BA"].size()) || (m["BA"].size()>1 && m["AB"].size())){
        cout<<"YES"<<endl;
    }
    else if(m.count("AB") && m.count("BA")){
        if(!m["AB"].size() || !m["BA"].size()){
            cout<<"NO";
        }
        else if(m["AB"][0] != m["BA"][0])
        cout<<"YES"<<endl;
        else{
            cout<<"NO";
        }
    }
    else{
        cout<<"NO"<<endl;
    }

}


#include <iostream>
#include<unordered_map>
#include<vector>
using namespace std;
int main() {
    string a;
    cin>>a;
    int t1=0,t2=0;
    unordered_map<string,vector<int>> m;
    for(int i=0;i<a.size()-1;i++){
        if(a[i]=='A' && a[i+1]=='B'){
            m["AB"].push_back(i+1);
            t1=1;
        }
        if(a[i]=='B' && a[i+1]=='A'){
            m["BA"].push_back(i);
            t2=1;
        }
    }
    if(a=="ABAB" || a=="BABA"){
        cout<<"NO";
    }
    else if(m.count("AB") && m.count("BA")){
        if(!m["AB"].size() || !m["BA"].size()){
            cout<<"NO";
        }
        else if((m["AB"][0] != m["BA"][0] ) && (m["BA"][0]+2 != m["AB"][0]))
        cout<<"YES"<<endl;
        else if((m["AB"].size()>1 && m["BA"].size()) || (m["BA"].size()>1 && m["AB"].size())){
        cout<<"YES"<<endl;
    }
        else{
            cout<<"NO";
        }
    }
    else{
        cout<<"NO"<<endl;
    }

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
int main() {
    string a;
    cin>>a;
    bool ans=false;
    for(int i=0;i<a.size()-1;i++){
        if(a[i]=='A' && a[i+1]=='B'){
            i=i+2;
            for(int j=i;j<a.size()-1;j++){
                if(a[j]=='B' && a[j+1]=='A'){
                    ans=true;
                }
            }
        }
        if(ans){
            break;
        }
    }
    if(!ans){
        for(int i=0;i<a.size()-1;i++){
            if(a[i]=='B' && a[i+1]=='A'){
                i=i+2;
                for(int j=i;j<a.size()-1;j++){
                    if(a[j]=='A' && a[j+1]=='B'){
                        ans=true;
                    }
                }
            }
            if(ans){
                break;
            }
        }
    }
    if(ans){
        cout<<"YES";
    }
    else{
        cout<<"NO";
    }
}