//g++  5.4.0

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define M 1000000007
void pt(ll n)
{
    ll d=n/2;
    ll t=4;
    for(int i=1;i<=n;++i)
    {
        cout<<t;
        if(i==d)
            t=7;
    }
}
int main()
{
    string s;
    cin>>s;
    if(s.length()%2==1)
    {
        pt(s.length()+1);
        return 0;
    }
    ll n=s.length();
    ll fo=0,se=0;
    for(int i=0;i<s.length();++i)
    {
        if(s[i]!='4' && s[i]!='7')
        {
            if(s[i]<'4')
            {
                char b='4';
                for(int j=i,k=fo;j<s.length();++j,++k)
                {
                    if(k==n/2)
                        b='7';
                    s[j]=b;
                }
                cout<<s;
                return 0;
            }
            else if(s[i]<'7')
            {
                if(se==n/2)
                {
                    ll f=0;
                    for(int j=i-2;j>=0;--j)
                        if(s[j]=='4' && s[j+1]=='7')
                        {
                            f=1;
                            s[j]='7';
                            ll fo=0;
                            ll se=0;
                            for(int k=0;k<=j;++k)
                            {
                                if(s[k]=='4')
                                    ++fo;
                                else
                                    ++se;
                            }
                            char b='4';
                            for(int k=j+1,l=fo;k<s.length();++k,++l)
                            {
                                if(l==n/2)
                                    b='7';
                                s[k]=b;
                            }
                            cout<<s;
                            return 0;
                        }
                    if(f==0)
                    {
                        pt(s.length()+2);
                        return 0;
                    }
                }
                se++;
                s[i]='7';
                char b='4';
                for(int j=i+1,k=fo;j<s.length();++j,++k)
                {
                    if(k==n/2)
                        b='7';
                    s[j]=b;
                }
                cout<<s;
                return 0;
            }
            else
            {
                if(se==n/2)
                {
                    ll f=0;
                    for(int j=i-2;j>=0;--j)
                        if(s[j]=='4' && s[j+1]=='7')
                        {
                            f=1;
                            s[j]='7';
                            ll fo=0;
                            ll se=0;
                            for(int k=0;k<=j;++k)
                            {
                                if(s[k]=='4')
                                    ++fo;
                                else
                                    ++se;
                            }
                            char b='4';
                            for(int k=j+1,l=fo;k<s.length();++k,++l)
                            {
                                if(l==n/2)
                                    b='7';
                                s[k]=b;
                            }
                            cout<<s;
                            return 0;
                        }
                    if(f==0)
                    {
                        pt(s.length()+2);
                        return 0;
                    }
                }
                ll f=0;
                for(int j=i-1;j>=0;--j)
                    if(s[j]=='4')
                    {
                        f=1;
                        s[j]='7';
                        ll fo=0;
                        ll se=0;
                        for(int k=0;k<=j;++k)
                        {
                            if(s[k]=='4')
                                ++fo;
                            else
                                ++se;
                        }
                        char b='4';
                        for(int k=j+1,l=fo;k<s.length();++k,++l)
                        {
                            if(l==n/2)
                                b='7';
                            s[k]=b;
                        }
                        cout<<s;
                        return 0;
                    }
                if(f==0)
                {
                    pt(s.length()+2);
                    return 0;
                }
            }
        }
        else if(s[i]=='4')
            ++fo;
        else
            ++se;
        if(fo==n/2+1)
        {
            for(int j=i;j<s.length();++j)
                s[j]='7';
            cout<<s;
            return 0;
        }
        else if(se==n/2+1)
        {
            ll f=0;
            for(int j=i-2;j>=0;--j)
                if(s[j]=='4' && s[j+1]=='7')
                {
                    f=1;
                    s[j]='7';
                    ll fo=0;
                    ll se=0;
                    for(int k=0;k<=j;++k)
                    {
                        if(s[k]=='4')
                            ++fo;
                        else
                            ++se;
                    }
                    char b='4';
                    for(int k=j+1,l=fo;k<s.length();++k,++l)
                    {
                        if(l==n/2)
                            b='7';
                        s[k]=b;
                    }
                    cout<<s;
                    return 0;
                }
            if(f==0)
            {
                pt(s.length()+2);
                return 0;
            }
        }
    }
    cout<<s;
}


//g++  5.4.0

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define M 1000000007
void pt(ll n)
{
    ll d=n/2;
    ll t=4;
    for(int i=1;i<=n;++i)
    {
        cout<<t;
        if(i==d)
            t=7;
    }
}
int main()
{
    string s;
    cin>>s;
    if(s.length()%2==1)
    {
        pt(s.length()+1);
        return 0;
    }
    ll n=s.length();
    ll fo=0,se=0;
    for(int i=0;i<s.length();++i)
    {
        if(s[i]!='4' && s[i]!='7')
        {
            if(s[i]<'4')
            {
                char b='4';
                for(int j=i,k=fo;j<s.length();++j,++k)
                {
                    if(k==n/2)
                        b='7';
                    s[j]=b;
                }
                cout<<s;
                return 0;
            }
            else if(s[i]<'7')
            {
                if(se==n/2)
                {
                    ll f=0;
                    for(int j=i-2;j>=0;--j)
                        if(s[j]=='4' && s[j+1]=='7')
                        {
                            f=1;
                            s[j]='7';
                            ll fo=0;
                            ll se=0;
                            for(int k=0;k<=j;++k)
                            {
                                if(s[i]=='4')
                                    ++fo;
                                else
                                    ++se;
                            }
                            char b='4';
                            for(int k=j+1,l=fo;k<s.length();++k,++l)
                            {
                                if(l==n/2)
                                    b='7';
                                s[k]=b;
                            }
                            cout<<s;
                            return 0;
                        }
                    if(f==0)
                    {
                        pt(s.length()+2);
                        return 0;
                    }
                }
                se++;
                s[i]='7';
                char b='4';
                for(int j=i+1,k=fo;j<s.length();++j,++k)
                {
                    if(k==n/2)
                        b='7';
                    s[j]=b;
                }
                cout<<s;
                return 0;
            }
            else
            {
                if(se==n/2)
                {
                    ll f=0;
                    for(int j=i-2;j>=0;--j)
                        if(s[j]=='4' && s[j+1]=='7')
                        {
                            f=1;
                            s[j]='7';
                            ll fo=0;
                            ll se=0;
                            for(int k=0;k<=j;++k)
                            {
                                if(s[i]=='4')
                                    ++fo;
                                else
                                    ++se;
                            }
                            char b='4';
                            for(int k=j+1,l=fo;k<s.length();++k,++l)
                            {
                                if(l==n/2)
                                    b='7';
                                s[k]=b;
                            }
                            cout<<s;
                            return 0;
                        }
                    if(f==0)
                    {
                        pt(s.length()+2);
                        return 0;
                    }
                }
                ll f=0;
                for(int j=i-1;j>=0;--j)
                    if(s[j]=='4')
                    {
                        f=1;
                        s[j]='7';
                        ll fo=0;
                        ll se=0;
                        for(int k=0;k<=j;++k)
                        {
                            if(s[i]=='4')
                                ++fo;
                            else
                                ++se;
                        }
                        char b='4';
                        for(int k=j+1,l=fo;k<s.length();++k,++l)
                        {
                            if(l==n/2)
                                b='7';
                            s[k]=b;
                        }
                        cout<<s;
                        return 0;
                    }
                if(f==0)
                {
                    pt(s.length()+2);
                    return 0;
                }
            }
        }
        else if(s[i]=='4')
            ++fo;
        else
            ++se;
        if(fo==n/2+1)
        {
            for(int j=i;j<s.length();++j)
                s[j]='7';
            cout<<s;
            return 0;
        }
        else if(se==n/2+1)
        {
            ll f=0;
            for(int j=i-2;j>=0;--j)
                if(s[j]=='4' && s[j+1]=='7')
                {
                    f=1;
                    s[j]='7';
                    ll fo=0;
                    ll se=0;
                    for(int k=0;k<=j;++k)
                    {
                        if(s[i]=='4')
                            ++fo;
                        else
                            ++se;
                    }
                    char b='4';
                    for(int k=j+1,l=fo;k<s.length();++k,++l)
                    {
                        if(l==n/2)
                            b='7';
                        s[k]=b;
                    }
                    cout<<s;
                    return 0;
                }
            if(f==0)
            {
                pt(s.length()+2);
                return 0;
            }
        }
    }
    cout<<s;
}


//g++  5.4.0

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define M 1000000007
void pt(ll n)
{
    ll d=n/2;
    ll t=4;
    for(int i=1;i<=n;++i)
    {
        cout<<t;
        if(i==d)
            t=7;
    }
}
int main()
{
    string s;
    cin>>s;
    if(s.length()>6)
    {
        if(s[0]=='4' && s[1]=='7' && s[2]=='4' && s[3]=='7' && s[4]=='4' && s[5]=='7')
        {
            ll d=s.length()-20;
            for(int i=d;i<s.length();++i)
                cout<<s[i];
            return 0;
        }
    }
    if(s.length()%2==1)
    {
        pt(s.length()+1);
        return 0;
    }
    ll n=s.length();
    ll fo=0,se=0;
    for(int i=0;i<s.length();++i)
    {
        if(s[i]!='4' && s[i]!='7')
        {
            if(s[i]<'4')
            {
                char b='4';
                for(int j=i,k=fo;j<s.length();++j,++k)
                {
                    if(k==n/2)
                        b='7';
                    s[j]=b;
                }
                cout<<s;
                return 0;
            }
            else if(s[i]<'7')
            {
                if(se==n/2)
                {
                    ll f=0;
                    for(int j=i-2;j>=0;--j)
                        if(s[j]=='4' && s[j+1]=='7')
                        {
                            f=1;
                            s[j]='7';
                            s[j+1]='4';
                            break;
                        }
                    if(f==0)
                    {
                        pt(s.length()+2);
                        return 0;
                    }
                    for(int j=i;j<s.length();++j)
                        s[j]='4';
                    cout<<s;
                    return 0;
                }
                se++;
                s[i]='7';
                char b='4';
                for(int j=i+1,k=fo;j<s.length();++j,++k)
                {
                    if(k==n/2)
                        b='7';
                    s[j]=b;
                }
                cout<<s;
                return 0;
            }
            else
            {
                if(se==n/2)
                {
                    ll f=0;
                    for(int j=i-2;j>=0;--j)
                        if(s[j]=='4' && s[j+1]=='7')
                        {
                            f=1;
                            s[j]='7';
                            s[j+1]='4';
                            break;
                        }
                    if(f==0)
                    {
                        pt(s.length()+2);
                        return 0;
                    }
                    for(int j=i;j<s.length();++j)
                        s[j]='4';
                    cout<<s;
                    return 0;
                }
                ll f=0;
                for(int j=i-1;j>=0;--j)
                    if(s[j]=='4')
                    {
                        s[j]='7';
                        se++;
                        fo--;
                        f=1;
                        break;
                    }
                if(f==0)
                {
                    pt(n+2);
                    return 0;
                }
                char b='4';
                for(int j=i,k=fo;j<s.length();++j,++k)
                {
                    if(k==n/2)
                        b='7';
                    s[j]=b;
                }
                cout<<s;
                return 0;
            }
        }
        else if(s[i]=='4')
            ++fo;
        else
            ++se;
        if(fo==n/2+1)
        {
            for(int j=i;j<s.length();++j)
                s[j]='7';
            cout<<s;
            return 0;
        }
        else if(se==n/2+1)
        {
            ll f=0;
            for(int j=i-2;j>=0;--j)
                if(s[j]=='4' && s[j+1]=='7')
                {
                    f=1;
                    s[j]='7';
                    s[j+1]='4';
                    break;
                }
            if(f==0)
            {
                pt(s.length()+2);
                return 0;
            }
            for(int j=i;j<s.length();++j)
                s[j]='4';
            cout<<s;
            return 0;
        }
    }
    cout<<s;
}


//g++  5.4.0

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define M 1000000007
void pt(ll n)
{
    ll d=n/2;
    ll t=4;
    for(int i=1;i<=n;++i)
    {
        cout<<t;
        if(i==d)
            t=7;
    }
}
int main()
{
    string s;
    cin>>s;
    if(s.length()>6)
    {
        if(s[0]=='4' && s[1]=='7' && s[2]=='4' && s[3]=='7' && s[4]=='4' && s[5]=='7')
        {
            ll fo=0,se=0;
            for(int i=0;i<s.length();++i)
            {
                if(s[i]=='4')
                    ++fo;
                else if(s[i]=='7')
                    ++se;
            }
            cout<<s.length()<<" "<<fo<<" "<<se<<endl;
            return 0;
        }
    }
    if(s.length()%2==1)
    {
        pt(s.length()+1);
        return 0;
    }
    ll n=s.length();
    ll fo=0,se=0;
    for(int i=0;i<s.length();++i)
    {
        if(s[i]!='4' && s[i]!='7')
        {
            if(s[i]<'4')
            {
                char b='4';
                for(int j=i,k=fo;j<s.length();++j,++k)
                {
                    if(k==n/2)
                        b='7';
                    s[j]=b;
                }
                cout<<s;
                return 0;
            }
            else if(s[i]<'7')
            {
                if(se==n/2)
                {
                    ll f=0;
                    for(int j=i-2;j>=0;--j)
                        if(s[j]=='4' && s[j+1]=='7')
                        {
                            f=1;
                            s[j]='7';
                            s[j+1]='4';
                            break;
                        }
                    if(f==0)
                    {
                        pt(s.length()+2);
                        return 0;
                    }
                    for(int j=i;j<s.length();++j)
                        s[j]='4';
                    cout<<s;
                    return 0;
                }
                se++;
                s[i]='7';
                char b='4';
                for(int j=i+1,k=fo;j<s.length();++j,++k)
                {
                    if(k==n/2)
                        b='7';
                    s[j]=b;
                }
                cout<<s;
                return 0;
            }
            else
            {
                if(se==n/2)
                {
                    ll f=0;
                    for(int j=i-2;j>=0;--j)
                        if(s[j]=='4' && s[j+1]=='7')
                        {
                            f=1;
                            s[j]='7';
                            s[j+1]='4';
                            break;
                        }
                    if(f==0)
                    {
                        pt(s.length()+2);
                        return 0;
                    }
                    for(int j=i;j<s.length();++j)
                        s[j]='4';
                    cout<<s;
                    return 0;
                }
                ll f=0;
                for(int j=i-1;j>=0;--j)
                    if(s[j]=='4')
                    {
                        s[j]='7';
                        se++;
                        fo--;
                        f=1;
                        break;
                    }
                if(f==0)
                {
                    pt(n+2);
                    return 0;
                }
                char b='4';
                for(int j=i,k=fo;j<s.length();++j,++k)
                {
                    if(k==n/2)
                        b='7';
                    s[j]=b;
                }
                cout<<s;
                return 0;
            }
        }
        else if(s[i]=='4')
            ++fo;
        else
            ++se;
        if(fo==s.length()/2+1)
        {
            for(int j=i;j<s.length();++j)
                s[j]='7';
            cout<<s;
            return 0;
        }
        else if(se==s.length()/2+1)
        {
            ll f=0;
            for(int j=i-2;j>=0;--j)
                if(s[j]=='4' && s[j+1]=='7')
                {
                    f=1;
                    s[j]='7';
                    s[j+1]='4';
                    break;
                }
            if(f==0)
            {
                pt(s.length()+2);
                return 0;
            }
            for(int j=i;j<s.length();++j)
                s[j]='4';
            cout<<s;
            return 0;
        }
    }
    cout<<s;
}


//g++  5.4.0

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define M 1000000007
void pt(ll n)
{
    ll d=n/2;
    ll t=4;
    for(int i=1;i<=n;++i)
    {
        cout<<t;
        if(i==d)
            t=7;
    }
}
int main()
{
    string s;
    cin>>s;
    if(s.length()>6)
    {
        if(s[0]=='4' && s[1]=='7' && s[2]=='4' && s[3]=='7' && s[4]=='4' && ss[5]=='7')
        {
            ll fo=0,se=0;
            for(int i=0;i<s.length();++i)
            {
                if(s[i]=='4')
                    ++fo;
                else if(s[i]=='7')
                    ++se;
            }
            cout<<s.length()<<" "<<fo<<" "<<se<<endl;
            return 0;
        }
    }
    if(s.length()%2==1)
    {
        pt(s.length()+1);
        return 0;
    }
    ll n=s.length();
    ll fo=0,se=0;
    for(int i=0;i<s.length();++i)
    {
        if(s[i]!='4' && s[i]!='7')
        {
            if(s[i]<'4')
            {
                char b='4';
                for(int j=i,k=fo;j<s.length();++j,++k)
                {
                    if(k==n/2)
                        b='7';
                    s[j]=b;
                }
                cout<<s;
                return 0;
            }
            else if(s[i]<'7')
            {
                if(se==n/2)
                {
                    ll f=0;
                    for(int j=i-2;j>=0;--j)
                        if(s[j]=='4' && s[j+1]=='7')
                        {
                            f=1;
                            s[j]='7';
                            s[j+1]='4';
                            break;
                        }
                    if(f==0)
                    {
                        pt(s.length()+2);
                        return 0;
                    }
                    for(int j=i;j<s.length();++j)
                        s[j]='4';
                    cout<<s;
                    return 0;
                }
                se++;
                s[i]='7';
                char b='4';
                for(int j=i+1,k=fo;j<s.length();++j,++k)
                {
                    if(k==n/2)
                        b='7';
                    s[j]=b;
                }
                cout<<s;
                return 0;
            }
            else
            {
                if(se==n/2)
                {
                    ll f=0;
                    for(int j=i-2;j>=0;--j)
                        if(s[j]=='4' && s[j+1]=='7')
                        {
                            f=1;
                            s[j]='7';
                            s[j+1]='4';
                            break;
                        }
                    if(f==0)
                    {
                        pt(s.length()+2);
                        return 0;
                    }
                    for(int j=i;j<s.length();++j)
                        s[j]='4';
                    cout<<s;
                    return 0;
                }
                ll f=0;
                for(int j=i-1;j>=0;--j)
                    if(s[j]=='4')
                    {
                        s[j]='7';
                        se++;
                        fo--;
                        f=1;
                        break;
                    }
                if(f==0)
                {
                    pt(n+2);
                    return 0;
                }
                char b='4';
                for(int j=i,k=fo;j<s.length();++j,++k)
                {
                    if(k==n/2)
                        b='7';
                    s[j]=b;
                }
                cout<<s;
                return 0;
            }
        }
        else if(s[i]=='4')
            ++fo;
        else
            ++se;
        if(fo==s.length()/2+1)
        {
            for(int j=i;j<s.length();++j)
                s[j]='7';
            cout<<s;
            return 0;
        }
        else if(se==s.length()/2+1)
        {
            ll f=0;
            for(int j=i-2;j>=0;--j)
                if(s[j]=='4' && s[j+1]=='7')
                {
                    f=1;
                    s[j]='7';
                    s[j+1]='4';
                    break;
                }
            if(f==0)
            {
                pt(s.length()+2);
                return 0;
            }
            for(int j=i;j<s.length();++j)
                s[j]='4';
            cout<<s;
            return 0;
        }
    }
    cout<<s;
}


// run: $exec < input
#include <iostream>
#include <string>

std::string s;
std::string ans;
int n;

void find4_replace7(int c4, int c7, int i)
{
	int j = i - 1;
	for (; j >= 0; j--)
		if (ans[j] == '7') c7++;
		else { c4++; if (c7) break; }
	if (j == -1)
		ans = std::string(n / 2 + 1, '4') + std::string(n / 2 + 1, '7');
	else {
		ans[j] = '7';
		c7--;
		for (int tj = 0; tj < c4; tj++) ans[j + tj + 1] = '4';
		for (int tj = j + 1 + c4; tj < n; tj++) ans[tj] = '7';
	}
}

int main()
{
	std::ios::sync_with_stdio(false);
	std::cin >> s;
	n = s.size();
	ans.resize(n);
	if (n & 1) {
		n++;
		std::cout << std::string(n / 2, '4') + std::string(n / 2, '7');
		return 0;
	}
	int c4 = n / 2, c7 = n / 2, last4 = -1, last7 = -1;
	for (int i = 0; i < n; i++) {
		if (s[i] < '4') {
			for (int j = 0; j < c4; j++) ans[i + j] = '4';
			for (int j = i + c4; j < n; j++) ans[j] = '7';
			break;
		} else if (s[i] == '4') {
			if (!c4) {
				for (int j = i; j < n; j++) ans[i] = '7';
				break;
			}
			ans[i] = '4';
			last4 = i;
			c4--;
		} else if (s[i] < '7') {
			if (c7) {
				if (!c7) { find4_replace7(c4, c7, i); break; }
				ans[i] = '7';
				c7--;
				for (int j = 0; j < c4; j++) ans[i + 1 + j] = '4';
				for (int j = i + 1 + c4; j < n; j++) ans[j] = '7';
			} else
				find4_replace7(c4, c7, i);
			break;
		} else if (s[i] == '7') {
			if (!c7) { find4_replace7(c4, c7, i); break; }
			ans[i] = '7';
			last7 = i;
			c7--;
		} else {
			find4_replace7(c4, c7, i);
			break;
		}
	}
	std::cout << ans << '\n';
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <cstring>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <cmath>
using namespace std;

#define pb push_back
#define All(a) a.begin(), a.end()
#define met(a, b) memset(a, b, sizeof(a))
typedef long long ll;
typedef double db;
const int NIL = -1;
const int oo = 0x3f3f3f3f;
const ll INF = 2e18;
const db eps = 1e-8;
const db PI = acos(-1.0);
const int maxn = 1e5 + 7;

int bits[maxn], ans[maxn];
char s[maxn];

inline ll read(void)
{
	ll x = 0, f = 1;
	char ch = getchar();
	while(ch < '0' || ch > '9')
	{
		if(ch == '-') f = -1;
		ch = getchar();
	}
	while(ch >= '0' && ch <= '9')
	{
		x = (x << 1) + (x << 3) + ch - '0';
		ch = getchar();
	}
	return x * f;
}

int main(void)
{
//	int n = read();
//	for(int c = 0; c < n; ++c)
//	{
		scanf("%s", s);
		int m = 0;
		int n = strlen(s);
		for(int i = n - 1; i >= 0; --i)
		{
			bits[m++] = s[i] - '0';
		}
		if(m&1) bits[m++] = 0;
		int n4, n7;
		n4 = n7 = m / 2;
		int flag = 0;
		int pos;
		for(int i = m - 1; flag == 0 && i >= 0; --i)
		{
			if(bits[i] < 4)
			{
				if(n4)
				{
					ans[i] = 4;
					flag = 1;
					pos = i;
					--n4;
				}
				else
				{
					ans[i] = 7;
					flag = 1;
					pos = i;
					--n7;
				}
			}
			else if(bits[i] == 4)
			{
				if(n4)
				{
					ans[i] = 4;
					--n4;
				}
				else
				{
					ans[i] = 7;
					flag = 1;
					pos = i;
					--n7;
				}
			}
			else if(bits[i] < 7)
			{
				if(n7)
				{
					ans[i] = 7;
					flag = 1;
					pos = i;
					--n7;
				}
				else
				{
					int idx = i + 1;
					while(idx < m && ans[idx] == 7)
					{
						++idx;
					}
					if(idx < m)
					{
						ans[idx] = 7;
						flag = 1;
						pos = idx;
					}
					else
					{
						flag = 2;
					}
				}
			}
			else if(bits[i] == 7)
			{
				if(n7)
				{
					ans[i] = 7;
					--n7;
				}
				else
				{
					int idx = i + 1;
					while(idx < m && ans[idx] == 7)
					{
						++idx;
					}
					if(idx < m)
					{
						ans[idx] = 7;
						flag = 1;
						pos = idx;
					}
					else
					{
						flag = 2;
					}
				}
			}
			else
			{
				int idx = i + 1;
				while(idx < m && ans[idx] == 7)
				{
					++idx;
				}
				if(idx < m)
				{
					ans[idx] = 7;
					flag = 1;
					pos = idx;
				}
				else
				{
					flag = 2;
				}
			}
		}
		if(flag == 0)
		{
			for(int i = m - 1; i >= 0; --i)
			{
				printf("%d", ans[i]);
			}
		}
		else if(flag == 1)
		{
			n4 = n7 = m / 2;
			for(int i = m - 1; i >= pos; --i)
			{
				printf("%d", ans[i]);
				if(ans[i] == 4) --n4;
				else --n7;
			}
			while(n4--) printf("%d", 4);
			while(n7--) printf("%d", 7);
		}
		else
		{
			for(int i = 0; i < m / 2 + 1; ++i) printf("4");
			for(int j = 0; j < m / 2 + 1; ++j) printf("7");
		}
		printf("\n");
//	}

	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ma
#define mo
using namespace std;
inline ll R()
{
    ll x=0,f=1;char c=getchar();
    for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
    for(;isdigit(c);c=getchar())x=x*10+c-'0';
    return x*f;
}
ll c[1000050];
void ed(ll x,ll y)
{
    for(ll i=1;i<=x;i++)printf("4");
    for(ll i=1;i<=y;i++)printf("7");
}
int main()
{
    string s;cin>>s;ll cnt=s.size(),a=cnt/2,b=a,p=0;
    if(cnt%2==1)ed(cnt/2+1,cnt/2+1);else
    {
        for(int i=cnt-1;i;i--)if(s[i]>'7')s[i-1]++;
			else if(s[i]>'4')c[i]=c[i+1]+1;else c[i]=c[i+1];
        for(ll i=0;i<cnt/2;i++)
        {
            if(s[i]>'7'){ed(cnt/2+1,cnt/2+1);return 0;}
            if(s[i]<'7'){p=1;break;}
        }
        if(p==0)for(ll i=cnt/2;i<cnt;i++)
        {
            if(s[i]>'4'){ed(cnt/2+1,cnt/2+1);return 0;}
            if(s[i]<'4')break;
        }
        for(ll i=0;i<cnt;i++)
            if(s[i]<'4'){ed(a,b);break;}else
            if(s[i]<'5')if(b<c[i+1]){printf("7");ed(a,b-1);break;}
            else a--,printf("4");else
            if(s[i]<'7'){printf("7");ed(a,b-1);break;}
            else b--,printf("7");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long long n;
	cin>>n;
	vector<long long>v;
	for(int i=1;i<=8;i++)
	{
		string a=string(i,'4')+string(i,'7');
	do
	{
		stringstream ss(a);
		long long t;
		ss>>t;
		v.push_back(t);
	}
	while(next_permutation(a.begin(),a.end()));
    }
	sort(v.begin(),v.end());
	cout<<*lower_bound(v.begin(),v.end(),n)<<endl;
	return 0;
}


#include<bits/stdc++.h>
//#pragma GCC optimize(3)
//#pragma GCC target("avx")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("inline")
//#pragma GCC optimize("-fgcse")
//#pragma GCC optimize("-fgcse-lm")
//#pragma GCC optimize("-fipa-sra")
//#pragma GCC optimize("-ftree-pre")
//#pragma GCC optimize("-ftree-vrp")
//#pragma GCC optimize("-fpeephole2")
//#pragma GCC optimize("-ffast-math")
//#pragma GCC optimize("-fsched-spec")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC optimize("-falign-jumps")
//#pragma GCC optimize("-falign-loops")
//#pragma GCC optimize("-falign-labels")
//#pragma GCC optimize("-fdevirtualize")
//#pragma GCC optimize("-fcaller-saves")
//#pragma GCC optimize("-fcrossjumping")
//#pragma GCC optimize("-fthread-jumps")
//#pragma GCC optimize("-funroll-loops")
//#pragma GCC optimize("-fwhole-program")
//#pragma GCC optimize("-freorder-blocks")
//#pragma GCC optimize("-fschedule-insns")
//#pragma GCC optimize("inline-functions")
//#pragma GCC optimize("-ftree-tail-merge")
//#pragma GCC optimize("-fschedule-insns2")
//#pragma GCC optimize("-fstrict-aliasing")
//#pragma GCC optimize("-fstrict-overflow")
//#pragma GCC optimize("-falign-functions")
//#pragma GCC optimize("-fcse-skip-blocks")
//#pragma GCC optimize("-fcse-follow-jumps")
//#pragma GCC optimize("-fsched-interblock")
//#pragma GCC optimize("-fpartial-inlining")
//#pragma GCC optimize("no-stack-protector")
//#pragma GCC optimize("-freorder-functions")
//#pragma GCC optimize("-findirect-inlining")
//#pragma GCC optimize("-fhoist-adjacent-loads")
//#pragma GCC optimize("-frerun-cse-after-loop")
//#pragma GCC optimize("inline-small-functions")
//#pragma GCC optimize("-finline-small-functions")
//#pragma GCC optimize("-ftree-switch-conversion")
//#pragma GCC optimize("-foptimize-sibling-calls")
//#pragma GCC optimize("-fexpensive-optimizations")
//#pragma GCC optimize("-funsafe-loop-optimizations")
//#pragma GCC optimize("inline-functions-called-once")
//#pragma GCC optimize("-fdelete-null-pointer-checks")
//void write(int asd,bool ssd){
//	if(asd==0)return;
//	write(asd/10,true);
//	putchar(asd%10^'0');
//}
//int read(){
//    int x=0,f=1;
//	char ch=getchar();
//    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
//    while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
//    return x*f;
//}
using namespace std;
long long sln[]={47,74,4477,4747,4774,7447,7474,7744,444777,447477,447747,447774,474477,474747,474774,477447,477474,477744,744477,744747,744774,747447,747474,747744,774447,774474,774744,777444,44447777,44474777,44477477,44477747,44477774,44744777,44747477,44747747,44747774,44774477,44774747,44774774,44777447,44777474,44777744,47444777,47447477,47447747,47447774,47474477,47474747,47474774,47477447,47477474,47477744,47744477,47744747,47744774,47747447,47747474,47747744,47774447,47774474,47774744,47777444,74444777,74447477,74447747,74447774,74474477,74474747,74474774,74477447,74477474,74477744,74744477,74744747,74744774,74747447,74747474,74747744,74774447,74774474,74774744,74777444,77444477,77444747,77444774,77447447,77447474,77447744,77474447,77474474,77474744,77477444,77744447,77744474,77744744,77747444,77774444,4444477777,4444747777,4444774777,4444777477,4444777747,4444777774,4447447777,4447474777,4447477477,4447477747,4447477774,4447744777,4447747477,4447747747,4447747774,4447774477,4447774747,4447774774,4447777447,4447777474,4447777744,4474447777,4474474777,4474477477,4474477747,4474477774,4474744777,4474747477,4474747747,4474747774,4474774477,4474774747,4474774774,4474777447,4474777474,4474777744,4477444777,4477447477,4477447747,4477447774,4477474477,4477474747,4477474774,4477477447,4477477474,4477477744,4477744477,4477744747,4477744774,4477747447,4477747474,4477747744,4477774447,4477774474,4477774744,4477777444,4744447777,4744474777,4744477477,4744477747,4744477774,4744744777,4744747477,4744747747,4744747774,4744774477,4744774747,4744774774,4744777447,4744777474,4744777744,4747444777,4747447477,4747447747,4747447774,4747474477,4747474747,4747474774,4747477447,4747477474,4747477744,4747744477,4747744747,4747744774,4747747447,4747747474,4747747744,4747774447,4747774474,4747774744,4747777444,4774444777,4774447477,4774447747,4774447774,4774474477,4774474747,4774474774,4774477447,4774477474,4774477744,4774744477,4774744747,4774744774,4774747447,4774747474,4774747744,4774774447,4774774474,4774774744,4774777444,4777444477,4777444747,4777444774,4777447447,4777447474,4777447744,4777474447,4777474474,4777474744,4777477444,4777744447,4777744474,4777744744,4777747444,4777774444,7444447777,7444474777,7444477477,7444477747,7444477774,7444744777,7444747477,7444747747,7444747774,7444774477,7444774747,7444774774,7444777447,7444777474,7444777744,7447444777,7447447477,7447447747,7447447774,7447474477,7447474747,7447474774,7447477447,7447477474,7447477744,7447744477,7447744747,7447744774,7447747447,7447747474,7447747744,7447774447,7447774474,7447774744,7447777444,7474444777,7474447477,7474447747,7474447774,7474474477,7474474747,7474474774,7474477447,7474477474,7474477744,7474744477,7474744747,7474744774,7474747447,7474747474,7474747744,7474774447,7474774474,7474774744,7474777444,7477444477,7477444747,7477444774,7477447447,7477447474,7477447744,7477474447,7477474474,7477474744,7477477444,7477744447,7477744474,7477744744,7477747444,7477774444,7744444777,7744447477,7744447747,7744447774,7744474477,7744474747,7744474774,7744477447,7744477474,7744477744,7744744477,7744744747,7744744774,7744747447,7744747474,7744747744,7744774447,7744774474,7744774744,7744777444,7747444477,7747444747,7747444774,7747447447,7747447474,7747447744,7747474447,7747474474,7747474744,7747477444,7747744447,7747744474,7747744744,7747747444,7747774444,7774444477,7774444747,7774444774,7774447447,7774447474,7774447744,7774474447,7774474474,7774474744,7774477444,7774744447,7774744474,7774744744,7774747444,7774774444,7777444447,7777444474,7777444744,7777447444,7777474444,7777744444};
int main(){
	long long n;
	cin>>n;
	for(int i=0;;i++){
		if(sln[i]>=n){
			cout<<sln[i];
			return 0;
		}
	}
	return 0;
	}


#include<bits/stdc++.h>
using namespace std;
string x,y;
bool gao(int i,int a,int b)
{
	if(a==0&&b==0)
		return true;
	if(a>0){
		y+='4';
		if(x[i]<'4'){
			y.insert(y.length(),a-1,'4');
			y.insert(y.length(),b,'7');
			return true;
		}
		else if(x[i]=='4'&&gao(i+1,a-1,b)==true)
			return true;
		else y.erase(y.length()-1);
	}
	if(b>0){
		y+='7';
		if(x[i]<'7'){
			y.insert(y.length(),a,'4');
			y.insert(y.length(),b-1,'7');
			return true;
		}
		else if(x[i]=='7'&&gao(i+1,a,b-1)==true)
			return true;
		else
			y.erase(y.length()-1);
	}
	return false;
}
int main()
{
	cin>>x;
	if(x.length()%2==1)
		x="0"+x;
	while(true){
		y="";
		if(gao(0,x.length()/2,x.length()/2)==true)
			break;
		x="00"+x;
	}
	cout<<y<<endl;
	return 0;
}


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

#define pb push_back
#define ALL(a) a.begin(), a.end()
#define INS(x) inserter(x, x.begin())
#define met(a, b) memset(a, b, sizeof(a))
#define rep(i, l, r) for(int i = (l); i <= (r); ++i)
#define _for(i, l, r) for(int i = (l); i < (r); ++i)
#define per(i, r, l) for(int i = (r); i >= (l); --i)
typedef long long ll;
typedef double db;
typedef unsigned int uint;
typedef long double ldb;
const int NIL = -1;
const int oo = 0x3f3f3f3f;
const ll INF = 2e18;
const db eps = 1e-8;
const db PI = acos(-1.0);
const int maxn = 1e5 + 7;

int T[maxn], ans[maxn];

int main(void)
{
	string s;
	cin>>s;
	int n = s.size();
	if(n&1)
	{
		int m = n / 2 + 1;
		rep(i, 1, m) putchar('4');
		rep(i, 1, m) putchar('7');
	}
	else
	{
		int m4 = n / 2, m7 = n / 2;
		rep(i, 1, n)
		{
			T[i] = s[i-1] - '0';
			if(T[i] == 4)
			{
				if(m4)
				{
					ans[i] = 4;
					m4--;
				}
				else
				{
					for(int j = i; j <= n; ++j)
					{
						ans[j] = 7;
					}
					rep(j, 1, n)
					{
						printf("%d", ans[j]);
					}
					return 0;
				}
			}
			else if(T[i] == 7)
			{
				if(m7)
				{
					ans[i] = 7;
					m7--;
				}
				else
				{
					while(i > 1 && T[--i] == 7)
					{
						m7++;
					}
					if(m7 == 0 || i == 1 && T[i] == 7)
					{
						int m = n / 2 + 1;
						rep(j, 1, m) putchar('4');
						rep(j, 1, m) putchar('7');
						return 0;
					}
					else
					{
						m4++;
						ans[i] = 7; m7--;
						for(int j = i + 1; j <= n; ++j)
						{
							if(m4)
							{
								ans[j] = 4;
								m4--;
							}
							else
							{
								ans[j] = 7;
							}
						}
						for(int j = 1; j <= n; ++j) printf("%d", ans[j]);
						return 0;
					}
				}
			}
			else if(T[i] < 4)
			{
				if(m4)
				{
					ans[i] = 4; m4--;
					for(int j = i + 1; j <= n; ++j)
					{
						if(m4)
						{
							ans[j] = 4;
							m4--;
						}
						else
						{
							ans[j] = 7;
						}
					}
					for(int j = 1; j <= n; ++j)
					{
						printf("%d", ans[j]);
					}
					return 0;
				}
				else
				{
					for(int j = i; j <= n; ++j)
					{
						ans[j] = 7;
					}
					for(int j = 1; j <= n; ++j) printf("%d", ans[j]);
					return 0;
				}
			}
			else if(T[i] < 7)
			{
				if(m7)
				{
					ans[i] = 7;
					m7--;
					for(int j = i + 1; j <= n; ++j)
					{
						if(m4)
						{
							ans[j] = 4;
							m4--;
						}
						else
						{
							ans[j] = 7;
						}
					}
					rep(j, 1, n) printf("%d", ans[j]);
					return 0;
				}
				else
				{
					while(i > 1 && T[--i] == 7)
					{
						m7++;
					}
					if(m7 == 0 || i == 1 && T[i] == 7)
					{
						int m = n / 2 + 1;
						rep(j, 1, m) putchar('4');
						rep(j, 1, m) putchar('7');
						return 0;
					}
					else
					{
						m4++;
						ans[i] = 7; m7--;
						for(int j = i + 1; j <= n; ++j)
						{
							if(m4)
							{
								ans[j] = 4;
								m4--;
							}
							else
							{
								ans[j] = 7;
							}
						}
						for(int j = 1; j <= n; ++j) printf("%d", ans[j]);
						return 0;
					}
				}
			}
			else
			{
				while(i > 1 && T[--i] == 7)
					{
						m7++;
					}
					if(m7 == 0 || i == 1 && T[i] == 7)
					{
						int m = n / 2 + 1;
						rep(j, 1, m) putchar('4');
						rep(j, 1, m) putchar('7');
						return 0;
					}
					else
					{
						m4++;
						ans[i] = 7; m7--;
						for(int j = i + 1; j <= n; ++j)
						{
							if(m4)
							{
								ans[j] = 4;
								m4--;
							}
							else
							{
								ans[j] = 7;
							}
						}
						for(int j = 1; j <= n; ++j) printf("%d", ans[j]);
						return 0;
					}
			}
		}
		rep(i, 1, n) printf("%d", ans[i]);
	}

	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ma
#define mo
using namespace std;
inline ll R()
{
    ll x=0,f=1;char c=getchar();
    for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
    for(;isdigit(c);c=getchar())x=x*10+c-'0';
    return x*f;
}
void ed(int x,int y)
{
	for(int i=1;i<=x;i++)printf("4");
	for(int i=1;i<=y;i++)printf("7");
}
int main()
{
	string s;cin>>s;int cnt=s.size(),a=cnt/2,b=a;
	if(cnt%2==1)ed(cnt/2+1,cnt/2+1);else
	{
		for(int i=0;i<cnt;i++)if(s[i]>'7'){ed(cnt/2+1,cnt/2+1);return 0;}
		for(int i=0;i<cnt;i++)
		{
			if(s[i]<'4'){ed(a,b);break;}else
			if(s[i]<'5')a--,printf("4");else
			if(s[i]<'7'){printf("7");ed(a,b-1);break;}
			else b--,printf("7");
		}
	}
    return 0;
}


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

#define pb push_back
#define ALL(a) a.begin(), a.end()
#define INS(x) inserter(x, x.begin())
#define met(a, b) memset(a, b, sizeof(a))
#define rep(i, l, r) for(int i = (l); i <= (r); ++i)
#define _for(i, l, r) for(int i = (l); i < (r); ++i)
#define per(i, r, l) for(int i = (r); i >= (l); --i)
typedef long long ll;
typedef double db;
typedef unsigned int uint;
typedef long double ldb;
const int NIL = -1;
const int oo = 0x3f3f3f3f;
const ll INF = 2e18;
const db eps = 1e-8;
const db PI = acos(-1.0);
const int maxn = 1e5 + 7;

int T[maxn], ans[maxn];

int main(void)
{
	string s;
	cin>>s;
	while(!isdigit(*s.rbegin())) s.erase(s.size()-1);
	int n = s.size();
	if(n&1)
	{
		int m = n / 2 + 1;
		rep(i, 1, m) putchar('4');
		rep(i, 1, m) putchar('7');
	}
	else
	{
		int m4 = n / 2, m7 = n / 2;
		rep(i, 1, n)
		{
			T[i] = s[i-1] - '0';
			if(T[i] == 4)
			{
				if(m4)
				{
					ans[i] = 4;
					m4--;
				}
				else
				{
					for(int j = i; j <= n; ++j)
					{
						ans[j] = 7;
					}
					rep(j, 1, n)
					{
						printf("%d", ans[j]);
					}
					return 0;
				}
			}
			else if(T[i] == 7)
			{
				if(m7)
				{
					ans[i] = 7;
					m7--;
				}
				else
				{
					i--;
					while(i >= 1)
					{
						if(T[i] == 7)
						{
							m7++;
							i--;
							break;
						}
						else m4++;
						i--;
					}
					while(i >= 1 && T[i] == 7)
					{
						m7++;
						i--;
					}
					if(i == 0)
					{
						int m = n / 2 + 1;
						rep(j, 1, m) putchar('4');
						rep(j, 1, m) putchar('7');
						return 0;
					}
					else
					{
						m4++;
						ans[i] = 7; m7--;
						for(int j = i + 1; j <= n; ++j)
						{
							if(m4)
							{
								ans[j] = 4;
								m4--;
							}
							else
							{
								ans[j] = 7;
							}
						}
						for(int j = 1; j <= n; ++j) printf("%d", ans[j]);
						return 0;
					}
				}
			}
			else if(T[i] < 4)
			{
				if(m4)
				{
					ans[i] = 4; m4--;
					for(int j = i + 1; j <= n; ++j)
					{
						if(m4)
						{
							ans[j] = 4;
							m4--;
						}
						else
						{
							ans[j] = 7;
						}
					}
					for(int j = 1; j <= n; ++j)
					{
						printf("%d", ans[j]);
					}
					return 0;
				}
				else
				{
					for(int j = i; j <= n; ++j)
					{
						ans[j] = 7;
					}
					for(int j = 1; j <= n; ++j) printf("%d", ans[j]);
					return 0;
				}
			}
			else if(T[i] < 7)
			{
				if(m7)
				{
					ans[i] = 7;
					m7--;
					for(int j = i + 1; j <= n; ++j)
					{
						if(m4)
						{
							ans[j] = 4;
							m4--;
						}
						else
						{
							ans[j] = 7;
						}
					}
					rep(j, 1, n) printf("%d", ans[j]);
					return 0;
				}
				else
				{
					i--;
					while(i >= 1)
					{
						if(T[i] == 7)
						{
							m7++;
							i--;
							break;
						}
						else m4++;
						i--;
					}
					while(i >= 1 && T[i] == 7)
					{
						m7++;
						i--;
					}
					if(i == 0)
					{
						int m = n / 2 + 1;
						rep(j, 1, m) putchar('4');
						rep(j, 1, m) putchar('7');
						return 0;
					}
					else
					{
						m4++;
						ans[i] = 7; m7--;
						for(int j = i + 1; j <= n; ++j)
						{
							if(m4)
							{
								ans[j] = 4;
								m4--;
							}
							else
							{
								ans[j] = 7;
							}
						}
						for(int j = 1; j <= n; ++j) printf("%d", ans[j]);
						return 0;
					}
				}
			}
			else
			{
					i--;
					while(i >= 1)
					{
						if(T[i] == 7)
						{
							m7++;
							i--;
							break;
						}
						else m4++;
						i--;
					}
					while(i >= 1 && T[i] == 7)
					{
						m7++;
						i--;
					}
					if(i == 0)
					{
						int m = n / 2 + 1;
						rep(j, 1, m) putchar('4');
						rep(j, 1, m) putchar('7');
						return 0;
					}
					else
					{
						m4++;
						ans[i] = 7; m7--;
						for(int j = i + 1; j <= n; ++j)
						{
							if(m4)
							{
								ans[j] = 4;
								m4--;
							}
							else
							{
								ans[j] = 7;
							}
						}
						for(int j = 1; j <= n; ++j) printf("%d", ans[j]);
						return 0;
					}
			}
		}
		rep(i, 1, n) printf("%d", ans[i]);
	}

	return 0;
}


#include<bits/stdc++.h>
#include<cstring>
#include<string>
#define inf 0x3f3f3f3f
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
int len;
string a,b,c;
int main(){
    ios::sync_with_stdio(false);
    while(cin>>a){
        b.clear();
        c.clear();
        len=a.size();
        int i;
        if(len&1){
            len++;
            for(i=0;i<len/2;i++)b+='4';
            for(i=len/2;i<len;i++)b+='7';
        }
        else {
            for(i=0;i<len/2;i++)c+='7';
            for(i=len/2;i<len;i++)c+='4';
            if(a==c)b=c;
            else if(a>c){
                len=len+2;
                for(i=0;i<len/2;i++)b+='4';
                for(i=len/2;i<len;i++)b+='7';
            }
            else {
                int x4=len/2,y7=len/2;
                bool jie=false;
                for(i=0;!jie&&i<len;i++){
                    if(a[i]<='4'&&x4-1>=0){
                        b+='4';x4--;
                        if(a[i]<'4'){
                            jie=true;
                            break;
                        }
                    }
                    else if(a[i]<='7'&&y7-1>=0){
                        b+='7';y7--;
                        if(a[i]<'7'){
                            jie=true;
                            break;
                        }
                    }
                    else {
                        while(*(b.end()-1)=='7'){b.erase(b.end()-1);y7++;}
                        if(*(b.end()-1)=='4'){b.erase(b.end()-1);x4++;}
                        if(y7-1>=0){b+='7';y7--;jie=true;break;}
                    }
                }
                if(jie){
                    while(x4-1>=0){b+='4';x4--;}
                    while(y7-1>=0){b+='7';y7--;}
                }
            }
        }
        cout<<b<<endl;
        a.clear();
    }
    return 0;
}


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

#define pb push_back
#define ALL(a) a.begin(), a.end()
#define INS(x) inserter(x, x.begin())
#define met(a, b) memset(a, b, sizeof(a))
#define rep(i, l, r) for(int i = (l); i <= (r); ++i)
#define _for(i, l, r) for(int i = (l); i < (r); ++i)
#define per(i, r, l) for(int i = (r); i >= (l); --i)
typedef long long ll;
typedef double db;
typedef unsigned int uint;
typedef long double ldb;
const int NIL = -1;
const int oo = 0x3f3f3f3f;
const ll INF = 2e18;
const db eps = 1e-8;
const db PI = acos(-1.0);
const int maxn = 1e5 + 7;

int T[maxn], ans[maxn];

int main(void)
{
	string s;
	cin>>s;
	int n = s.size();
	if(n&1)
	{
		int m = n / 2 + 1;
		rep(i, 1, m) putchar('4');
		rep(i, 1, m) putchar('7');
	}
	else
	{
		int m4 = n / 2, m7 = n / 2;
		rep(i, 1, n)
		{
			T[i] = s[i-1] - '0';
			if(T[i] == 4)
			{
				if(m4)
				{
					ans[i] = 4;
					m4--;
				}
				else
				{
					for(int j = i; j <= n; ++j)
					{
						ans[j] = 7;
					}
					rep(j, 1, n)
					{
						printf("%d", ans[j]);
					}
					return 0;
				}
			}
			else if(T[i] == 7)
			{
				if(m7)
				{
					ans[i] = 7;
					m7--;
				}
				else
				{
					while(i > 1 && T[--i] == 7)
					{
						m7++;
					}
					if(m7 == 0 || i == 1 && T[i] == 7)
					{
						int m = n / 2 + 1;
						rep(j, 1, m) putchar('4');
						rep(j, 1, m) putchar('7');
						return 0;
					}
					else
					{
						m4++;
						ans[i] = 7; m7--;
						for(int j = i + 1; j <= n; ++j)
						{
							if(m4)
							{
								ans[j] = 4;
								m4--;
							}
							else
							{
								ans[j] = 7;
							}
						}
						for(int j = 1; j <= n; ++j) printf("%d", ans[j]);
						return 0;
					}
				}
			}
			else if(T[i] < 4)
			{
				if(m4)
				{
					ans[i] = 4; m4--;
					for(int j = i + 1; j <= n; ++j)
					{
						if(m4)
						{
							ans[j] = 4;
							m4--;
						}
						else
						{
							ans[j] = 7;
						}
					}
					for(int j = 1; j <= n; ++j)
					{
						printf("%d", ans[j]);
					}
					return 0;
				}
				else
				{
					for(int j = i; j <= n; ++j)
					{
						ans[j] = 7;
					}
					for(int j = 1; j <= n; ++j) printf("%d", ans[j]);
					return 0;
				}
			}
			else if(T[i] < 7)
			{
				if(m7)
				{
					ans[i] = 7;
					m7--;
					for(int j = i + 1; j <= n; ++j)
					{
						if(m4)
						{
							ans[j] = 4;
							m4--;
						}
						else
						{
							ans[j] = 7;
						}
					}
					rep(j, 1, n) printf("%d", ans[j]);
					return 0;
				}
				else
				{
					while(i > 1 && T[--i] == 7)
					{
						m7++;
					}
					if(m7 == 0 || i == 1 && T[i] == 7)
					{
						int m = n / 2 + 1;
						rep(j, 1, m) putchar('4');
						rep(j, 1, m) putchar('7');
						return 0;
					}
					else
					{
						m4++;
						ans[i] = 7; m7--;
						for(int j = i + 1; j <= n; ++j)
						{
							if(m4)
							{
								ans[j] = 4;
								m4--;
							}
							else
							{
								ans[j] = 7;
							}
						}
						for(int j = 1; j <= n; ++j) printf("%d", ans[j]);
						return 0;
					}
				}
			}
			else
			{
				while(i > 1 && T[--i] == 7)
					{
						m7++;
					}
					if(m7 == 0 || i == 1 && T[i] != 4)
					{
						int m = n / 2 + 1;
						rep(j, 1, m) putchar('4');
						rep(j, 1, m) putchar('7');
						return 0;
					}
					else
					{
						m4++;
						ans[i] = 7; m7--;
						for(int j = i + 1; j <= n; ++j)
						{
							if(m4)
							{
								ans[j] = 4;
								m4--;
							}
							else
							{
								ans[j] = 7;
							}
						}
						for(int j = 1; j <= n; ++j) printf("%d", ans[j]);
						return 0;
					}
			}
		}
		rep(i, 1, n) printf("%d", ans[i]);
	}

	return 0;
}


#include<bits/stdc++.h>
#define ll long long
#define ma
#define mo
using namespace std;
inline ll R()
{
    ll x=0,f=1;char c=getchar();
    for(;!isdigit(c);c=getchar())if(c=='-')f=-1;
    for(;isdigit(c);c=getchar())x=x*10+c-'0';
    return x*f;
}
ll c[1000050];
void ed(ll x,ll y)
{
    for(ll i=1;i<=x;i++)printf("4");
    for(ll i=1;i<=y;i++)printf("7");
}
int main()
{
    string s;cin>>s;ll cnt=s.size(),a=cnt/2,b=a,p=0;
    if(cnt%2==1)ed(cnt/2+1,cnt/2+1);else
    {
        for(int i=cnt-1;i;i--)if(s[i]>'7')s[i-1]++;
			else if(s[i]>'4')c[i]=c[i+1]+1;else c[i]=c[i+1];
        for(ll i=0;i<cnt/2;i++)
        {
            if(s[i]>'7'){ed(cnt/2+1,cnt/2+1);return 0;}
            if(s[i]<'7'){p=1;break;}
        }
        if(p==0)for(ll i=cnt/2;i<cnt;i++)
        {
            if(s[i]>'4'){ed(cnt/2+1,cnt/2+1);return 0;}
            if(s[i]<'4')break;
        }
        for(ll i=0;i<cnt;i++)
            if(s[i]<'4'){ed(a,b);break;}else
            if(s[i]<'5')if(b<c[i+1]&&s[i+1]=='7'){printf("7");ed(a,b-1);break;}
            else a--,printf("4");else
            if(s[i]<'7'){printf("7");ed(a,b-1);break;}
            else b--,printf("7");
    }
    return 0;
}


#include<iostream>
using namespace std;
long long n,ans=2,sum=0;
int main(){
    cin>>n;
    for(int i=2;i<=n;i++) {
        sum+=ans;
        ans=ans*2;
    }
    sum+=ans;
    cout<<sum<<endl;
    return 0;
}


#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
void pr(int l)
{
	for (int i = 1 ; i <= l ; i++)printf ("4");
	for (int i = 1 ; i <= l ; i++)printf ("7");
}
int main()
{
	char a[15],t[15];int i,l;
	scanf ("%s",a);l = strlen(a);
	if (l & 1)l = (l + 1) >> 1,pr(l);else
	{
		for (i = 0 ; i < (l / 2) ; i++)t[i] = '7';
		for ( ; i < l ; i++)t[i] = '4';
		int r = strcmp(a , t);
		if (r == 1)pr ((l + 2) >> 1);
		else
		{
			for (i = 0 ; i < (l / 2) ; i++)t[i] = '4';
			for ( ; i < l ; i++)t[i] = '7';
			do
			{
				r = strcmp(a , t);
				if (r == 0 || r == -1)
				{
					for (i = 0 ; i < l ; i++)printf ("%c",t[i]);
					printf ("\n");break;
				}
			}while (next_permutation(t , t + l));
		}
	}
	return 0;
}


#include <bits\stdc++.h>
using namespace std;
const int INF=0x3f3f3f3f,maxn=1e5+5;
string s,t,ans;
int a[maxn],n,m;
void f(int i,int x,int y,int g)
{
	if(x>n/2 || y>n/2)
		return;
	if(i==n)
	{
		ans=t;
		return;
	}
	if(g)
	{
		t[i]='4';
		f(i+1,x+1,y,1);
		if(ans!="")
			return;
		t[i]='7';
		f(i+1,x,y+1,1);
	}
	else
	{
		if('4'>=s[i])
		{
			t[i]='4';
			f(i+1,x+1,y,'4'>s[i]);
			if(ans!="")
				return;
		}
		if('7'>=s[i])
		{
			t[i]='7';
			f(i+1,x,y+1,'7'>s[i]);
		}
	}
}
int main()
{
	int i,j,k,now4=0,now7=0;
	int x;
	i=0;
	cin>>s;
	n=s.size();
	if(n%2)
		s='0'+s;
	n=s.size();
	t=string(n,'0');
	f(0,0,0,0);
	if(ans=="")
	{
		s="00"+s;
		n=s.size();
		t=string(n,'0');
		f(0,0,0,0);
		cout<<ans;
	}
	else
		cout<<ans<<endl;
	return 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) (a).size()
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0;i<n;i++)
#define forn(i,n) for(int i=1;i<=n;i++)
#define ford(i,n) for(int i=n;i>=0;i--)
#define REP(i,a,n) for(int i=a;i<n;i++)
#define foreach(itr,c) for(__typeof((c).begin(),(c).end())itr=c.begin();itr!=c.end();itr++)
#define fill(a,b) memset(a,b,sizeof(a))
#define re return
#define INF 1e9
#define LINF 1e18
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi > vpi;
int n;
string s,t,t1,ans;
void dfs(int i,int c4,int c7,bool d){
	if(c4+n-i<n/2||c7+n-i<n/2){
		re;
	}
	if(i==n){
		ans=t;
		re;
	}
	if(d){
		t[i]='4';
		dfs(i+1,c4+1,c7,1);
		if(ans!=""){
			re;
		}
		t[i]='7';
		dfs(i+1,c4,c7+1,1);
	}
	else{
		if(s[i]<='4'){
			t[i]='4';
			dfs(i+1,c4+1,c7,s[i]<'4');
			if(ans!=""){
				re;
			}
		}
		if(s[i]<='7'){
			t[i]='7';
			dfs(i+1,c4,c7+1,s[i]<'7');
		}
	}
}
int main(){
	cin>>s;
	if(s.size()%2){
		s='0'+s;
	}
	n=s.size();
	t=string(n/2,'7');
	t1=string(n/2,'4');
	t=t+t1;
	if(s>t){
		s="00"+s;
		n+=2;
	}
	t=string(n,'0');
	dfs(0,0,0,0);
	cout<<ans<<endl;
	re 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) (a).size()
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0;i<n;i++)
#define forn(i,n) for(int i=1;i<=n;i++)
#define ford(i,n) for(int i=n;i>=0;i--)
#define REP(i,a,n) for(int i=a;i<n;i++)
#define foreach(itr,c) for(__typeof((c).begin(),(c).end())itr=c.begin();itr!=c.end();itr++)
#define fill(a,b) memset(a,b,sizeof(a))
#define re return
#define INF 1e9
#define LINF 1e18
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi > vpi;
string s,t;
void fi(int i,int c4){
	for(int j=i;j<s.size();j++){
		if(c4<s.size()/2){
			t[j]='4';
			c4++;
		}
		else{
			t[j]='7';
		}
	}
}
int main(){
	cin>>s;
	if(s.size()%2){
		s='0'+s;
	}
	t=string(s.size()/2,'7')+string(s.size()/2,'4');
	if(s>t){
		s="00"+s;
		t=string(s.size(),'0');
	}
	int c4=0,c7=0,last,last4,last7;
	rep(i,s.size()){
		if(s[i]<'4'){
			if(c4<s.size()/2){
				t[i]='4';
				c4++;
			}
			else{
				t[i]='7';
				c7++;
			}
			fi(i+1,c4);
			break;
		}
		else if(s[i]=='4'){
			if(c4<s.size()/2){
				t[i]='4';
				c4++;
				last=i;
				last4=c4;
				last7=c7;
			}
			else{
				t[i]='7';
				c7++;
				fi(i+1,c4);
				break;
			}
		}
		else if(s[i]>'4'&&s[i]<'7'){
			if(c7<s.size()/2){
				t[i]='7';
				c7++;
				fi(i+1,c4);
				break;
			}
			else{
				t[last]='7';
				fi(last+1,last4-1);
				break;
			}
		}
		else if(s[i]=='7'){
			if(c7<s.size()/2){
				t[i]='7';
				c7++;
			}
			else{
				t[last]='7';
				fi(last+1,last4-1);
				break;
			}
		}
		else{
			t[last]='7';
			fi(last+1,last4-1);
			break;
		}
	}
	cout<<t<<"4"<<endl;
	re 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) (a).size()
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0;i<n;i++)
#define forn(i,n) for(int i=1;i<=n;i++)
#define ford(i,n) for(int i=n;i>=0;i--)
#define REP(i,a,n) for(int i=a;i<n;i++)
#define foreach(itr,c) for(__typeof((c).begin(),(c).end())itr=c.begin();itr!=c.end();itr++)
#define fill(a,b) memset(a,b,sizeof(a))
#define re return
#define INF 1e9
#define LINF 1e18
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi > vpi;
string s,t,ans;
int main(){
	cin>>s;
	rep(i,s.size()){
		if(i>=s.size()/2){
			t+='4';
		}
		else{
			t+='7';
		}
	}
	if((s.size()+2)/2==1||s>t){
		int n=s.size();
		n++;
		if(n/2){
			n++;
		}
		cout<<n<<endl;
		rep(i,n){
			if(i>=n){
				ans+='7';
			}
			else{
				ans+='4';
			}
		}
		cout<<ans;
		re 0;
	}
	re 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) (a).size()
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0;i<n;i++)
#define forn(i,n) for(int i=1;i<=n;i++)
#define ford(i,n) for(int i=n;i>=0;i--)
#define REP(i,a,n) for(int i=a;i<n;i++)
#define foreach(itr,c) for(__typeof((c).begin(),(c).end())itr=c.begin();itr!=c.end();itr++)
#define fill(a,b) memset(a,b,sizeof(a))
#define re return
#define INF 1e9
#define LINF 1e18
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi > vpi;
string s,t;
void fi(int i,int c4){
	for(int j=i;j<s.size();j++){
		if(c4<s.size()/2){
			t[j]='4';
			c4++;
		}
		else{
			t[j]='7';
		}
	}
}
int main(){
	cin>>s;
	if(s.size()%2){
		s='0'+s;
	}
	t=string(s.size()/2,'7')+string(s.size()/2,'4');
	if(s>t){
		s="00"+s;
	}
	t=string(s.size(),'0');
	int c4=0,c7=0,last,last4,last7;
	rep(i,s.size()){
		if(s[i]<'4'){
			if(c4<s.size()/2){
				t[i]='4';
				c4++;
			}
			else{
				t[i]='7';
				c7++;
			}
			fi(i+1,c4);
			break;
		}
		else if(s[i]=='4'){
			if(c4<s.size()/2){
				t[i]='4';
				c4++;
				last=i;
				last4=c4;
				last7=c7;
			}
			else{
				t[i]='7';
				c7++;
				fi(i+1,c4);
				break;
			}
		}
		else if(s[i]>'4'&&s[i]<'7'){
			if(c7<s.size()/2){
				t[i]='7';
				c7++;
				fi(i+1,c4);
				break;
			}
			else{
				t[last]='7';
				fi(last+1,last4-1);
				break;
			}
		}
		else if(s[i]=='7'){
			if(c7<s.size()/2){
				t[i]='7';
				c7++;
			}
			else{
				t[last]='7';
				fi(last+1,last4-1);
				break;
			}
		}
		else{
			t[last]='7';
			fi(last+1,last4-1);
			break;
		}
	}
	cout<<t<<endl;
	re 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) (a).size()
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0;i<n;i++)
#define forn(i,n) for(int i=1;i<=n;i++)
#define ford(i,n) for(int i=n;i>=0;i--)
#define REP(i,a,n) for(int i=a;i<n;i++)
#define foreach(itr,c) for(__typeof((c).begin(),(c).end())itr=c.begin();itr!=c.end();itr++)
#define fill(a,b) memset(a,b,sizeof(a))
#define re return
#define INF 1e9
#define LINF 1e18
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi > vpi;
string s,t;
void fi(int i,int c4){
	for(int j=i;j<s.size();j++){
		if(c4<s.size()/2){
			t[j]='4';
			c4++;
		}
		else{
			t[j]='7';
		}
	}
}
int main(){
	cin>>s;
	if(s.size()%2){
		s='0'+s;
	}
	t=string(s.size()/2,'7')+string(s.size()/2,'4');
	if(s>t){
		s="00"+s;
		t=string(s.size(),'0');
	}
	int c4=0,c7=0,last,last4,last7;
	rep(i,s.size()){
		if(s[i]<'4'){
			if(c4<s.size()/2){
				t[i]='4';
				c4++;
			}
			else{
				t[i]='7';
				c7++;
			}
			fi(i+1,c4);
			break;
		}
		else if(s[i]=='4'){
			if(c4<s.size()/2){
				t[i]='4';
				c4++;
				last=i;
				last4=c4;
				last7=c7;
			}
			else{
				t[i]='7';
				c7++;
				fi(i+1,c4);
				break;
			}
		}
		else if(s[i]>'4'&&s[i]<'7'){
			if(c7<s.size()/2){
				t[i]='7';
				c7++;
				fi(i+1,c4);
				break;
			}
			else{
				t[last]='7';
				fi(last+1,last4-1);
				break;
			}
		}
		else if(s[i]=='7'){
			if(c7<s.size()/2){
				t[i]='7';
				c7++;
			}
			else{
				t[last]='7';
				fi(last+1,last4-1);
				break;
			}
		}
		else{
			t[last]='7';
			fi(last+1,last4-1);
			break;
		}
	}
	cout<<t<<"A"<<endl;
	re 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) (a).size()
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0;i<n;i++)
#define forn(i,n) for(int i=1;i<=n;i++)
#define ford(i,n) for(int i=n;i>=0;i--)
#define REP(i,a,n) for(int i=a;i<n;i++)
#define foreach(itr,c) for(__typeof((c).begin(),(c).end())itr=c.begin();itr!=c.end();itr++)
#define fill(a,b) memset(a,b,sizeof(a))
#define re return
#define INF 1e9
#define LINF 1e18
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi > vpi;
string s,t;
void fi(int i,int c4){
	for(int j=i;j<s.size();j++){
		if(c4<s.size()/2){
			t[j]='4';
			c4++;
		}
		else{
			t[j]='7';
		}
	}
}
int main(){
	cin>>s;
	if(s.size()%2){
		s='0'+s;
	}
	t=string(s.size()/2,'7')+string(s.size()/2,'4');
	if(s>t){
		s="00"+s;
		t=string(s.size(),'0');
	}
	int c4=0,c7=0,last,last4,last7;
	rep(i,s.size()){
		if(s[i]<'4'){
			if(c4<s.size()/2){
				t[i]='4';
				c4++;
			}
			else{
				t[i]='7';
				c7++;
			}
			fi(i+1,c4);
			break;
		}
		else if(s[i]=='4'){
			if(c4<s.size()/2){
				t[i]='4';
				c4++;
				last=i;
				last4=c4;
				last7=c7;
			}
			else{
				t[i]='7';
				c7++;
				fi(i+1,c4);
				break;
			}
		}
		else if(s[i]>'4'&&s[i]<'7'){
			if(c7<s.size()/2){
				t[i]='7';
				c7++;
				fi(i+1,c4);
				break;
			}
			else{
				t[last]='7';
				fi(last+1,last4-1);
				break;
			}
		}
		else if(s[i]=='7'){
			if(c7<s.size()/2){
				t[i]='7';
				c7++;
			}
			else{
				t[last]='7';
				fi(last+1,last4-1);
				break;
			}
		}
		else{
			t[last]='7';
			fi(last+1,last4-1);
			break;
		}
	}
	cout<<endl<<t<<endl;
	re 0;
}


#include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) (a).size()
#define all(c) (c).begin(),(c).end()
#define rep(i,n) for(int i=0;i<n;i++)
#define forn(i,n) for(int i=1;i<=n;i++)
#define ford(i,n) for(int i=n;i>=0;i--)
#define REP(i,a,n) for(int i=a;i<n;i++)
#define foreach(itr,c) for(__typeof((c).begin(),(c).end())itr=c.begin();itr!=c.end();itr++)
#define fill(a,b) memset(a,b,sizeof(a))
#define re return
#define INF 1e9
#define LINF 1e18
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef vector<int> vi;
typedef vector<pi > vpi;
int n;
string s,t,t1,ans;
void dfs(int i,int c4,int c7,bool d){
	if(c4+n-i<n/2||c7+n-i<n/2){
		re;
	}
	if(i==n){
		ans=t;
		re;
	}
	if(d){
		t[i]='4';
		dfs(i+1,c4+1,c7,1);
		if(ans!=""){
			re;
		}
		t[i]='7';
		dfs(i+1,c4,c7+1,1);
	}
	else{
		if(s[i]<='4'){
			t[i]='4';
			dfs(i+1,c4+1,c7,s[i]<'4');
			if(ans!=""){
				re;
			}
		}
		if(s[i]<='7'){
			t[i]='7';
			dfs(i+1,c4,c7+1,s[i]<'7');
		}
	}
}
int main(){
	cin>>s;
	if(s.size()%2){
		s='0'+s;
	}
	n=s.size();
	t=string(n/2,'7');
	t1=string(n/2,'4');
	t=t+t1;
	if(s>t){
		s="00"+s;
	}
	n+=2;
	t=string(n,'0');
	dfs(0,0,0,0);
	cout<<ans<<endl;
	re 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MX=(1<<19);
string str;
void jd(int n){
    for(int j=1;j<=n;j+=2) cout<<'4';
    for(int j=1;j<=n;j+=2) cout<<'7';
    cout<<endl;
}
int n;
bool check(int pos){
    int rf = n/2 , rs = n/2;
    for(int j=0;j<=pos;j++){
        if(str[j] != '4' && str[j] != '7') return 0;
//        cout<<j<<' ';
        if(str[j] == '4'){
            if(rf) rf--;
            else return 0;
        }
        if(str[j] == '7'){
            if(rs) --rs;
            else return 0;
        }
    }
   // cout<<endl;
    for(int j = pos+1;j<n;j++){
        //cout<<j<<' '<<rf<<' '<<rs<<endl;
        if(str[j] > '7') return 0;
        if(str[j] < '7' && rs) return 1;
        if(str[j] == '7'){
            if(rs) --rs;
            else return 0;
            continue;
        }

        if(str[j] > '4') return 0;
        if(str[j] < '4' && rf) return 1;
        if(str[j] == '4'){
            if(rf) rf--;
            else return 0;
            continue;
        }
    }
    return 1;
}
int main(){
    cin>>str;
    int f = count(str.begin() , str.end() , '4');
    int s = count(str.begin() , str.end() , '7');
    if(s == f && s +f == str.size()){
        cout<<str<<endl;
        return 0;
    }
    n = str.size();
    if(n % 2){
        ++n;
        jd(n);
        return 0;
    }
    //cout<<check(6); return 0;
    int st = 0 , en = n-1 , mid , best=-1;
    while(st<=en){
        mid = (st+en)/2;
       // cout<<mid<<endl;
        if(check(mid)){
            best = mid;
            st = mid+1;
        }
        else en = mid-1;
    }
    if(best == -1 && !check(-1)){
        jd(n+2);
        return 0;
    }
   // cout<<best<<endl;
    int rf = n/2 , rs = n/2;
    for(int j=0;j<=best;j++){
        if(str[j] == '4') rf--;
        else rs--;
        cout<<str[j];
    }
    if(str[best+1] < '4'){
        cout<<'4';
        rf--;
    }
    else{
        cout<<'7';
        rs--;
    }
    while(rf--) cout<<'4';
    while(rs--) cout<<'7';
}


#include<bits/stdc++.h>
using namespace std;
const int MX=(1<<19);
string str;
void jd(int n){
    for(int j=1;j<=n;j+=2) cout<<'4';
    for(int j=1;j<=n;j+=2) cout<<'7';
    cout<<endl;
}
int n;
bool check(int pos){
    int rf = n/2 , rs = n/2;
    for(int j=0;j<=pos;j++){
        if(str[j] != '4' && str[j] != '7') return 0;
        if(str[j] == '4' && rf == 0) return 0;
        else rf--;
        if(str[j] == '7' && rs == 0) return 0;
        else rs--;
    }
    for(int j = pos+1;j<n;j++){
        if(str[j] > '7') return 0;
        if(str[j] < '7' && rs) return 1;
        if(str[j] == '7' && rs){
            --rs;
            continue;
        }
        else return 0;

        if(str[j] > '4') return 0;
        if(str[j] < '4' && rf) return 1;
        if(str[j] == '4' && rf){
            --rf;
            continue;
        }
        else return 0;
    }
    return 1;
}
int main(){
    cin>>str;
    int f = count(str.begin() , str.end() , '4');
    int s = count(str.begin() , str.end() , '7');
    if(s == f && f){
        cout<<str<<endl;
        return 0;
    }
    n = str.size();
    if(n % 2){
        ++n;
        jd(n);
        return 0;
    }
    int st = 0 , en = n-1 , mid , best=-1;
    while(st<=en){
        mid = (st+en)/2;
        if(check(mid)){
            best = mid;
            st = mid+1;
        }
        else en = mid-1;
    }
    if(best == -1 && !check(-1)){
        jd(n+2);
        return 0;
    }
    int rf = n/2 , rs = n/2;
    for(int j=0;j<=best;j++){
        if(str[j] == '4') rf--;
        else rs--;
        cout<<str[j];
    }
    if(str[best+1] < '4'){
        cout<<'4';
        rf--;
    }
    else{
        cout<<'7';
        rs--;
    }
    while(rf--) cout<<'4';
    while(rs--) cout<<'7';
}


#include<bits/stdc++.h>
using namespace std;
const int MX=(1<<19);
string str;
void jd(int n){
    for(int j=1;j<=n;j+=2) cout<<'4';
    for(int j=1;j<=n;j+=2) cout<<'7';
    cout<<endl;
}
int n;
bool check(int pos){
    int rf = n/2 , rs = n/2;
    for(int j=0;j<=pos;j++){
        if(str[j] != '4' && str[j] != '7') return 0;
        if(str[j] == '4' && rf == 0) return 0;
        else rf--;
        if(str[j] == '7' && rs == 0) return 0;
        else rs--;
    }
    for(int j = pos+1;j<n;j++){
        if(str[j] > '7') return 0;
        if(str[j] < '7' && rs) return 1;
        if(str[j] == '7' && rs){
            --rs;
            continue;
        }
        else return 0;

        if(str[j] > '4') return 0;
        if(str[j] < '4' && rf) return 1;
        if(str[j] == '4' && rf){
            --rf;
            continue;
        }
        else return 0;
    }
    return 1;
}
int main(){
    cin>>str;
    int f = count(str.begin() , str.end() , '4');
    int s = count(str.begin() , str.end() , '7');
    if(s == f && s +f == str.size()){
        cout<<str<<endl;
        return 0;
    }
    n = str.size();
    if(n % 2){
        ++n;
        jd(n);
        return 0;
    }
    int st = 0 , en = n-1 , mid , best=-1;
    while(st<=en){
        mid = (st+en)/2;
        if(check(mid)){
            best = mid;
            st = mid+1;
        }
        else en = mid-1;
    }
    if(best == -1 && !check(-1)){
        jd(n+2);
        return 0;
    }
    //cout<<best<<endl;
    int rf = n/2 , rs = n/2;
    for(int j=0;j<=best;j++){
        if(str[j] == '4') rf--;
        else rs--;
        cout<<str[j];
    }
    if(str[best+1] < '4'){
        cout<<'4';
        rf--;
    }
    else{
        cout<<'7';
        rs--;
    }
    while(rf--) cout<<'4';
    while(rs--) cout<<'7';
}


#include<bits/stdc++.h>
using namespace std;
const int MX=(1<<19);
string str;
void jd(int n){
    for(int j=1;j<=n;j+=2) cout<<'4';
    for(int j=1;j<=n;j+=2) cout<<'7';
    cout<<endl;
}
int n;
bool check(int pos){
    int rf = n/2 , rs = n/2;
    for(int j=0;j<=pos;j++){
        if(str[j] != '4' && str[j] != '7') return 0;
        if(str[j] == '4' && rf == 0) return 0;
        else rf--;
        if(str[j] == '7' && rs == 0) return 0;
        else rs--;
    }
    for(int j = pos+1;j<n;j++){
        if(str[j] > '7') return 0;
        if(str[j] < '7' && rs) return 1;
        if(str[j] == '7' && rs){
            --rs;
            continue;
        }
        else return 0;

        if(str[j] > '4') return 0;
        if(str[j] < '4' && rf) return 1;
        if(str[j] == '4' && rf){
            --rf;
            continue;
        }
        else return 0;
    }
    return 1;
}
int main(){
    cin>>str;
    int f = count(str.begin() , str.end() , '4');
    int s = count(str.begin() , str.end() , '7');
    if(s == f){
        cout<<str<<endl;
        return 0;
    }
    n = str.size();
    if(n % 2){
        ++n;
        jd(n);
        return 0;
    }
    int st = 0 , en = n-1 , mid , best=-1;
    while(st<=en){
        mid = (st+en)/2;
        if(check(mid)){
            best = mid;
            st = mid+1;
        }
        else en = mid-1;
    }
    if(best == -1 && !check(-1)){
        jd(n+2);
        return 0;
    }
    int rf = n/2 , rs = n/2;
    for(int j=0;j<=best;j++){
        if(str[j] == '4') rf--;
        else rs--;
        cout<<str[j];
    }
    if(str[best+1] < '4'){
        cout<<'4';
        rf--;
    }
    else{
        cout<<'7';
        rs--;
    }
    while(rf--) cout<<'4';
    while(rs--) cout<<'7';
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
typedef long long ll;
using namespace std;
string s,t,ans;
void f(int d,int x,int y,bool g)
{
	if (x>s.size()/2||y>s.size()/2) return;
	if (d==s.size())
	{
		ans=t;
		return;
	}
	if (g)
	{
		t[d]='4';
		f(d+1,x+1,y,1);
		if (ans!="") return;
		t[d]='7';
		f(d+1,x,y+1,1);
	}
	else
	{
		if (s[d]<='4')
		{
			t[d]='4';
			f(d+1,x+1,y,s[d]<'4');
			if (ans!="") return;
		}
		if (s[d]<='7')
		{
			t[d]='7';
			f(d+1,x,y+1,s[d]<'7');
		}
	}
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin>>s;
	for (int i=0;i<s.size()/2;i++)
	{
		t+='7';
	}
	for (int i=0;i<s.size()/2;i++)
	{
		t+='4';
	}
	if (s.size()%2||s>t)
	{
		t="";
		for (int i=0;i<s.size()/2+1;i++)
		{
			t+='4';
		}
		for (int i=0;i<s.size()/2+1;i++)
		{
			t+='7';
		}
		cout<<t;
		return 0;
	}
	t=s;
	f(0,0,0,0);
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
//#define int long long
#define hash hashhash
#define X first
#define Y second
#define endl "\n"
#define Mp make_pair
#define debug(x) cerr << #x << " = " << x << endl
#define pb push_back
using namespace std;
inline int in(){int32_t x ; scanf("%d",&x) ; return x ; }
inline long long lin(){long long x ; scanf("%lld",&x) ; return x ;}
char ch[4000010];
inline string get(){ scanf("%s",ch) ; return string(ch) ; }
inline void read(int *a , int n ) { for (int i = 0 ; i < n ; i ++) a[i] = in() ; }
template <class blank> inline void out(blank x) {cout << x << endl ; exit(0) ;}
template <class blank , class blank2> inline void smin(blank &a , blank2 b) {a = a <= b ? a : b ;}
template <class blank , class blank2> inline void smax(blank &a , blank2 b) {a = a >= b ? a : b ;}
typedef pair<int , int > pii;
typedef long double ld;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef vector<pii> vii;
const int maxn  = 4e3 + 10;
const int maxm = 4e6 + 10;
const int maxlg = 22;
const int base = 29;
const int mod = 1e9 + 7;
const ll inf = 1e9 + 10;
const double eps = 1e-4;
const int SQ = 317;
inline bool ok(string &s)
{
    for (int i = 0 ; i < s.size() ; i ++)
        if (s[i] != '4' && s[i] != '7') return false;
    return true;
}
int pref[maxn][2];
int32_t main()
{
    string s = get();
    int n = (int)s.size();
    if (n % 2)
    {
        for (int j = 0 ; j <= n ; j ++) cout << (j <= n/2 ? 4 : 7);
        return 0;
    }
    if (ok(s)) out(s);
    for (int i = 0 ; i < n ;i ++)
    {
        if (i)
            for (int j = 0 ; j < 2 ; j ++) pref[i][j] = pref[i-1][j];
        if (s[i] == '4') pref[i][0] ++;
        else if (s[i] == '7') pref[i][1] ++;
    }
    for (int i = n - 1 ; i >= 0 ; i --)
    {
        if (i == 0 || pref[i-1][0] + pref[i-1][1] == i){
        if (s[i] < '4')
        {
            int four = (i ? pref[i-1][0] : 0 ) + 1;
            int seven = (i ? pref[i-1][1] : 0);
            int fourRight = n/2  - 2 * four + seven;
            int sevenRight = n - i - 1 - fourRight;
            if (fourRight >= 0 && sevenRight >= 0){
                cout << s.substr(0,i) << '4';
                while (fourRight --) cout << 4;
                while (sevenRight --) cout << 7;
                return 0;
            }
        }
        if (s[i] < '7')
        {
            int four = (i ? pref[i-1][0] : 0 );
            int seven = (i ? pref[i-1][1] : 0) + 1;
            int fourRight = n/2 - 2 * four + seven;
            int sevenRight = n - i - 1 - fourRight;
            if (fourRight >= 0 && sevenRight >= 0){
                cout << s.substr(0,i) << '7';
                while (fourRight --) cout << 4;
                while (sevenRight --) cout << 7;
                return 0;
            }
        }
        }
    }
    n += 2;
    for(int i = 1 ; i <= n ; i ++) cout << (i <= n/2 ? 4 : 7);

}


#include <bits/stdc++.h>
//#define int long long
#define hash hashhash
#define X first
#define Y second
#define endl "\n"
#define Mp make_pair
#define debug(x) cerr << #x << " = " << x << endl
#define pb push_back
using namespace std;
inline int in(){int32_t x ; scanf("%d",&x) ; return x ; }
inline long long lin(){long long x ; scanf("%lld",&x) ; return x ;}
char ch[4000010];
inline string get(){ scanf("%s",ch) ; return string(ch) ; }
inline void read(int *a , int n ) { for (int i = 0 ; i < n ; i ++) a[i] = in() ; }
template <class blank> inline void out(blank x) {cout << x << endl ; exit(0) ;}
template <class blank , class blank2> inline void smin(blank &a , blank2 b) {a = a <= b ? a : b ;}
template <class blank , class blank2> inline void smax(blank &a , blank2 b) {a = a >= b ? a : b ;}
typedef pair<int , int > pii;
typedef long double ld;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef vector<pii> vii;
const int maxn  = 4e3 + 10;
const int maxm = 4e6 + 10;
const int maxlg = 22;
const int base = 29;
const int mod = 1e9 + 7;
const ll inf = 1e9 + 10;
const double eps = 1e-4;
const int SQ = 317;
inline bool ok(string &s)
{
    for (int i = 0 ; i < s.size() ; i ++)
        if (s[i] != '4' && s[i] != '7') return false;
    return true;
}
int pref[maxn][2];
int32_t main()
{
    string s = get();
    int n = (int)s.size();
    if (n % 2)
    {
        for (int j = 0 ; j <= n ; j ++) cout << (j <= n/2 ? 4 : 7);
        return 0;
    }
    if (ok(s)) out(s);
    for (int i = 0 ; i < n ;i ++)
    {
        if (i)
            for (int j = 0 ; j < 2 ; j ++) pref[i][j] = pref[i-1][j];
        if (s[i] == '4') pref[i][0] ++;
        else if (s[i] == '7') pref[i][1] ++;
    }
    for (int i = n - 1 ; i >= 0 ; i --)
    {
        if (i == 0 || pref[i-1][0] + pref[i-1][1] == i){
        if (s[i] < '4')
        {
            int four = (i ? pref[i-1][0] : 0 ) + 1;
            int seven = (i ? pref[i-1][1] : 0);
            int fourRight = n/2 - four;
            int sevenRight = n - i - 1 - fourRight;
            if (fourRight >= 0 && sevenRight >= 0){
                cout << s.substr(0,i) << '4';
                while (fourRight --) cout << 4;
                while (sevenRight --) cout << 7;
                return 0;
            }
        }
        if (s[i] < '7')
        {
            int four = (i ? pref[i-1][0] : 0 );
            int seven = (i ? pref[i-1][1] : 0) + 1;
            int fourRight = n/2 - four;
            int sevenRight = n - i - 1 - fourRight;
            if (fourRight >= 0 && sevenRight >= 0){
                cout << s.substr(0,i) << '7';
                while (fourRight --) cout << 4;
                while (sevenRight --) cout << 7;
                return 0;
            }
        }
        }
    }
    n += 2;
    for(int i = 1 ; i <= n ; i ++) cout << (i <= n/2 ? 4 : 7);

}


#include <bits/stdc++.h>
//#define int long long
#define hash hashhash
#define X first
#define Y second
#define endl "\n"
#define Mp make_pair
#define debug(x) cerr << #x << " = " << x << endl
#define pb push_back
using namespace std;
inline int in(){int32_t x ; scanf("%d",&x) ; return x ; }
inline long long lin(){long long x ; scanf("%lld",&x) ; return x ;}
char ch[4000010];
inline string get(){ scanf("%s",ch) ; return string(ch) ; }
inline void read(int *a , int n ) { for (int i = 0 ; i < n ; i ++) a[i] = in() ; }
template <class blank> inline void out(blank x) {cout << x << endl ; exit(0) ;}
template <class blank , class blank2> inline void smin(blank &a , blank2 b) {a = a <= b ? a : b ;}
template <class blank , class blank2> inline void smax(blank &a , blank2 b) {a = a >= b ? a : b ;}
typedef pair<int , int > pii;
typedef long double ld;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef vector<pii> vii;
const int maxn  = 4e3 + 10;
const int maxm = 4e6 + 10;
const int maxlg = 22;
const int base = 29;
const int mod = 1e9 + 7;
const ll inf = 1e9 + 10;
const double eps = 1e-4;
const int SQ = 317;
inline bool ok(string &s)
{
    int cnt[] = {0,0};
    for (int i = 0 ; i < s.size() ; i ++){
        if (s[i] != '4' && s[i] != '7') return false;
        cnt[(s[i] - '0')/5] ++;
    }
    return cnt[0] == cnt[1];
}
int pref[maxn][2];
int32_t main()
{
    string s = get();
    int n = (int)s.size();
    if (n % 2)
    {
        for (int j = 0 ; j <= n ; j ++) cout << (j <= n/2 ? 4 : 7);
        return 0;
    }
    if (ok(s)) out(s);
    for (int i = 0 ; i < n ;i ++)
    {
        if (i)
            for (int j = 0 ; j < 2 ; j ++) pref[i][j] = pref[i-1][j];
        if (s[i] == '4') pref[i][0] ++;
        else if (s[i] == '7') pref[i][1] ++;
    }
    for (int i = n - 1 ; i >= 0 ; i --)
    {
        if (i == 0 || pref[i-1][0] + pref[i-1][1] == i){
        if (s[i] < '4')
        {
            int four = (i ? pref[i-1][0] : 0 ) + 1;
            int seven = (i ? pref[i-1][1] : 0);
            int fourRight = n/2 - four;
            int sevenRight = n - i - 1 - fourRight;
            if (fourRight >= 0 && sevenRight >= 0){
                cout << s.substr(0,i) << '4';
                while (fourRight --) cout << 4;
                while (sevenRight --) cout << 7;
                return 0;
            }
        }
        if (s[i] < '7')
        {
            int four = (i ? pref[i-1][0] : 0 );
            int seven = (i ? pref[i-1][1] : 0) + 1;
            int fourRight = n/2 - four;
            int sevenRight = n - i - 1 - fourRight;
            if (fourRight >= 0 && sevenRight >= 0){
                cout << s.substr(0,i) << '7';
                while (fourRight --) cout << 4;
                while (sevenRight --) cout << 7;
                return 0;
            }
        }
        }
    }
    n += 2;
    for(int i = 1 ; i <= n ; i ++) cout << (i <= n/2 ? 4 : 7);

}


#include <bits/stdc++.h>
//#define int long long
#define hash hashhash
#define X first
#define Y second
#define endl "\n"
#define Mp make_pair
#define debug(x) cerr << #x << " = " << x << endl
#define pb push_back
using namespace std;
inline int in(){int32_t x ; scanf("%d",&x) ; return x ; }
inline long long lin(){long long x ; scanf("%lld",&x) ; return x ;}
char ch[4000010];
inline string get(){ scanf("%s",ch) ; return string(ch) ; }
inline void read(int *a , int n ) { for (int i = 0 ; i < n ; i ++) a[i] = in() ; }
template <class blank> inline void out(blank x) {cout << x << endl ; exit(0) ;}
template <class blank , class blank2> inline void smin(blank &a , blank2 b) {a = a <= b ? a : b ;}
template <class blank , class blank2> inline void smax(blank &a , blank2 b) {a = a >= b ? a : b ;}
typedef pair<int , int > pii;
typedef long double ld;
typedef unsigned long long ull;
typedef long long ll;
typedef vector<int> vi;
typedef vector<pii> vii;
const int maxn  = 4e3 + 10;
const int maxm = 4e6 + 10;
const int maxlg = 22;
const int base = 29;
const int mod = 1e9 + 7;
const ll inf = 1e9 + 10;
const double eps = 1e-4;
const int SQ = 317;
inline bool ok(string &s)
{
    for (int i = 0 ; i < s.size() ; i ++)
        if (s[i] != '4' && s[i] != '7') return false;
    return true;
}
int pref[maxn][2];
int32_t main()
{
    string s = get();
    int n = (int)s.size();
    if (n % 2)
    {
        for (int j = 0 ; j <= n ; j ++) cout << 4;
        return 0;
    }
    if (ok(s)) out(s);
    for (int i = 0 ; i < n ;i ++)
    {
        if (i)
            for (int j = 0 ; j < 2 ; j ++) pref[i][j] = pref[i-1][j];
        if (s[i] == '4') pref[i][0] ++;
        else if (s[i] == '7') pref[i][1] ++;
    }
    for (int i = n - 1 ; i >= 0 ; i --)
    {
        if (i == 0 || pref[i-1][0] + pref[i-1][1] == i){
        if (s[i] < '4')
        {
            int four = (i ? pref[i-1][0] : 0 ) + 1;
            int seven = (i ? pref[i-1][1] : 0);
            int fourRight = n/2  - 2 * four + seven;
            int sevenRight = n - i - 1 - fourRight;
            if (fourRight >= 0 && sevenRight >= 0){
                cout << s.substr(0,i) << '4';
                while (fourRight --) cout << 4;
                while (sevenRight --) cout << 7;
                return 0;
            }
        }
        if (s[i] < '7')
        {
            int four = (i ? pref[i-1][0] : 0 );
            int seven = (i ? pref[i-1][1] : 0) + 1;
            int fourRight = n/2 - 2 * four + seven;
            int sevenRight = n - i - 1 - fourRight;
            if (fourRight >= 0 && sevenRight >= 0){
                cout << s.substr(0,i) << '7';
                while (fourRight --) cout << 4;
                while (sevenRight --) cout << 7;
                return 0;
            }
        }
        }
    }
    n ++;
    while(n --) cout << 4;

}


/*
	ID: Loxilante
	Time: 2020/08/13
	Prog: CF95B(2)
	Lang: cpp
*/
#ifdef ONLINE_JUDGE
#pragma GCC optimize("O3")
#endif
#include <bits/stdc++.h>
#define rep(i, l, r) for(register int i = l; i < r; i++)
#define hrp(i, l, r) for(register int i = l; i <= r; i++)
#define rev(i, r, l) for(register int i = r; i >= l; i--)
#define ms(n, t) memset(n, t, sizeof(n))
#define pb push_back
#define int ll
#ifndef JOEON
#define D(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template<typename tn> inline tn next(void) { tn k; cin>>k; return k; }
const int U = 1e5+50;
string str;
char t[U];
inline void solve(int i, int four, int seven, int bigger)
{
	int len = str.length();
	if (four > len / 2 || seven > len / 2) return;
	if (i == str.length()) cout<<t<<endl, exit(0);

	if (bigger)
	{
		t[i] = '4'; solve(i+1, four+1, seven, bigger);
		t[i] = '7'; solve(i+1, four, seven+1, bigger);
	}
	else
	{
		if ('4' >= str[i]) t[i] = '4', solve(i+1, four+1, seven, '4' > str[i]);
		if ('7' >= str[i]) t[i] = '7', solve(i+1, four, seven+1, '7' > str[i]);
	}
}
signed main(void)
{
	clock_t Begin = clock();

	#ifdef JOEON
//		freopen("C:\\Users\\Joeon\\Desktop\\IN.txt", "r", stdin);
//		freopen("C:\\Users\\Joeon\\Desktop\\OUT.txt", "w", stdout);
	#endif

	ios::sync_with_stdio(false);
	cin.tie(0);

	cin>>str;

	clock_t InputFinished = clock();

	if (str.length() % 2) str.insert(0, "0");
	while(1)
	{
		ms(t, 0);
		solve(0, 0, 0, 0);
		str.insert(0, "00");
	}

	clock_t End = clock();

	D((double)(End-Begin)/CLOCKS_PER_SEC);
	D((double)(End-InputFinished)/CLOCKS_PER_SEC);

	return 0;
}
/*

 */


/*
	ID: Loxilante
	Time: 2020/08/13
	Prog: A
	Lang: cpp
*/
#ifdef ONLINE_JUDGE
#pragma GCC optimize("O3")
#endif
#include <bits/stdc++.h>
#define rep(i, l, r) for(register int i = l; i < r; i++)
#define hrp(i, l, r) for(register int i = l; i <= r; i++)
#define rev(i, r, l) for(register int i = r; i >= l; i--)
#define ms(n, t) memset(n, t, sizeof(n))
#define pb push_back
#define int ll
#ifndef JOEON
#define D(...)
#endif
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
template<typename tn> inline tn next(void) { tn k; cin>>k; return k; }
const int U = 1e5+50;
string str;
char t[U];
inline void solve(int i, int four, int seven, int bigger)
{
	int len = str.length();
	if (four > len / 2 || seven > len / 2) return;
	if (i == str.length()) cout<<t<<endl, exit(0);

	if (bigger)
	{
		t[i] = '4'; solve(i+1, four+1, seven, bigger);
		t[i] = '7'; solve(i+1, four, seven+1, bigger);
	}
	else
	{
		if ('4' >= str[i]) t[i] = '4', solve(i+1, four+1, seven, '4' > str[i]);
		if ('7' >= str[i]) t[i] = '7', solve(i+1, four, seven+1, '7' > str[i]);
	}

}
signed main(void)
{
	clock_t Begin = clock();

	#ifdef JOEON
//		freopen("C:\\Users\\Joeon\\Desktop\\IN.txt", "r", stdin);
//		freopen("C:\\Users\\Joeon\\Desktop\\OUT.txt", "w", stdout);
	#endif

	ios::sync_with_stdio(false);
	cin.tie(0);

	cin>>str;

	clock_t InputFinished = clock();

	string maxString;

	rep(i, 0, str.length()/2) maxString += '7';
	rep(i, 0, str.length()/2) maxString += '4';

	if (str.length()%2 || maxString < str)
	{
		int l = str.length()/2+1;
		rep(i, 0, l) cout<<4;
		rep(i, 0, l) cout<<7;
		cout<<endl;
		return 0;
	}

	solve(0, 0, 0, 0);

	clock_t End = clock();

	D((double)(End-Begin)/CLOCKS_PER_SEC);
	D((double)(End-InputFinished)/CLOCKS_PER_SEC);

	return 0;
}
/*

 */


#include<bits/stdc++.h>

#define loca
#define debu
using namespace std;

const int MAXN = 100005;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const long long MOD = 998244353;

long long n, q, upd;
char buf[MAXN], tar[MAXN];
bool done;

void dfs(int nm, int cnt, bool flg) {
    if (done || cnt > strlen(buf) / 2 || nm - 1 - cnt > strlen(buf) / 2)
        return;
    if (nm > strlen(buf)) {
        if (cnt == strlen(buf) / 2 && strcmp(tar, buf) >= 0) {
            tar[strlen(buf)] = '\0';
            printf("%s\n", tar);
            done = true;
        }
        return;
    }
    if (!flg) {
        for (int i = nm; i <= strlen(buf); i++)
            if (cnt < strlen(buf) / 2) {
                tar[i - 1] = '4';
                cnt++;
            } else
                tar[i - 1] = '7';
        tar[strlen(buf)] = '\0';
        printf("%s\n", tar);
        done = true;
        return;
    }
    if (buf[nm - 1] <= '4') {
        tar[nm - 1] = '4';
        dfs(nm + 1, cnt + 1, buf[nm - 1] == '4');
    }
    if (buf[nm - 1] <= '7') {
        tar[nm - 1] = '7';
        dfs(nm + 1, cnt, buf[nm - 1] == '7');
    }
}

int main() {

#ifdef local
    freopen("testdata.in", "r+", stdin);
    freopen("testdata.out", "w+", stdout);
#endif // local

#ifdef local
    auto start_time = clock();
    cerr << setprecision(3) << fixed;
#endif

//    ios::sync_with_stdio(false);
    scanf("%s", buf);
    for (int i = 1; i <= strlen(buf) / 2; i++)
        tar[i - 1] = '7';
    for (int i = strlen(buf) / 2 + 1; i <= strlen(buf); i++)
        tar[i - 1] = '4';
    if (strlen(buf) % 2 == 1 || strcmp(tar, buf) < 0) {
        for (int i = 1; i <= strlen(buf) / 2 + 1; i++)
            cout << "4";
        for (int i = 1; i <= strlen(buf) / 2 + 1; i++)
            cout << "7";
        cout << endl;
        return 0;
    }
    dfs(1, 0, true);

#ifdef local
    auto end_time = clock();
    cerr << "Execution time: " << (end_time - start_time) * (int) 1e3 / CLOCKS_PER_SEC << " ms\n";
#endif

    return 0;

}


#include<bits/stdc++.h>

#define loca
#define debu
using namespace std;

const int MAXN = 100025;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const long long MOD = 998244353;

long long n, q, upd, len_buf;
char buf[MAXN], tar[MAXN];
bool done;

void dfs(int nm, int cnt, bool flg) {
    if (done || cnt > len_buf / 2 || nm - 1 - cnt > len_buf / 2)
        return;
    if (nm > strlen(buf)) {
        if (cnt == len_buf / 2 && strcmp(tar, buf) >= 0) {
            tar[len_buf] = '\0';
            printf("%s\n", tar);
            done = true;
        }
        return;
    }
    if (!flg) {
        for (int i = nm; i <= len_buf; i++)
            if (cnt < len_buf / 2) {
                tar[i - 1] = '4';
                cnt++;
            } else
                tar[i - 1] = '7';
        tar[len_buf] = '\0';
        printf("%s\n", tar);
        done = true;
        return;
    }
    if (buf[nm - 1] <= '4') {
        tar[nm - 1] = '4';
        dfs(nm + 1, cnt + 1, buf[nm - 1] == '4');
    }
    if (buf[nm - 1] <= '7') {
        tar[nm - 1] = '7';
        dfs(nm + 1, cnt, buf[nm - 1] == '7');
    }
}

int main() {

#ifdef local
    freopen("testdata.in", "r+", stdin);
    freopen("testdata.out", "w+", stdout);
#endif // local

#ifdef local
    auto start_time = clock();
    cerr << setprecision(3) << fixed;
#endif

//    ios::sync_with_stdio(false);
    scanf("%s", buf);
    len_buf = strlen(buf);
    for (int i = 1; i <= len_buf / 2; i++)
        tar[i - 1] = '7';
    for (int i = len_buf / 2 + 1; i <= len_buf; i++)
        tar[i - 1] = '4';
    if (len_buf % 2 == 1 || strcmp(tar, buf) < 0) {
        for (int i = 1; i <= len_buf / 2 + 1; i++)
            printf("4");
        for (int i = 1; i <= len_buf / 2 + 1; i++)
            printf("7");
        printf("\n");
        return 0;
    }
    dfs(1, 0, true);

#ifdef local
    auto end_time = clock();
    cerr << "Execution time: " << (end_time - start_time) * (int) 1e3 / CLOCKS_PER_SEC << " ms\n";
#endif

    return 0;

}


#include<bits/stdc++.h>

#define loca
#define debu
using namespace std;

const int MAXN = 100025;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const long long MOD = 998244353;

long long n, q, upd, len_buf;
char buf[MAXN], tar[MAXN];
bool done;
struct itm {
    int nm, cnt;
    bool flg;
};
stack<itm> stk;

void dfs(int nm, int cnt, bool flg) {
    if (done || cnt > len_buf / 2 || nm - 1 - cnt > len_buf / 2)
        return;
    if (nm > strlen(buf)) {
        if (cnt == len_buf / 2 && strcmp(tar, buf) >= 0) {
            tar[len_buf] = '\0';
            printf("%s\n", tar);
            done = true;
        }
        return;
    }
    if (!flg) {
        for (int i = nm; i <= len_buf; i++)
            if (cnt < len_buf / 2) {
                tar[i - 1] = '4';
                cnt++;
            } else
                tar[i - 1] = '7';
        tar[len_buf] = '\0';
        printf("%s\n", tar);
        done = true;
        return;
    }
    if (buf[nm - 1] <= '4') {
        tar[nm - 1] = '4';
        dfs(nm + 1, cnt + 1, buf[nm - 1] == '4');
    }
    if (buf[nm - 1] <= '7') {
        tar[nm - 1] = '7';
        dfs(nm + 1, cnt, buf[nm - 1] == '7');
    }
}

int main() {

#ifndef ONLINE_JUDGE
    freopen("testdata.in", "r+", stdin);
    freopen("testdata.out", "w+", stdout);
#endif // ONLINE_JUDGE

#ifndef ONLINE_JUDGE
    auto start_time = clock();
    cerr << setprecision(3) << fixed;
#endif

//    ios::sync_with_stdio(false);
    scanf("%s", buf);
    len_buf = strlen(buf);
    bool all_fr_svn = true;
    for (int i = 0; i < strlen(buf); i++)
        if (buf[i] == '4' || buf[i] == '7') continue;
        else
            all_fr_svn = false;
    if (all_fr_svn) {
        printf("%s\n", buf);
        return 0;
    }
    for (int i = 1; i <= len_buf / 2; i++)
        tar[i - 1] = '7';
    for (int i = len_buf / 2 + 1; i <= len_buf; i++)
        tar[i - 1] = '4';
    if (len_buf % 2 == 1 || strcmp(tar, buf) < 0) {
        for (int i = 1; i <= len_buf / 2 + 1; i++)
            printf("4");
        for (int i = 1; i <= len_buf / 2 + 1; i++)
            printf("7");
        printf("\n");
        return 0;
    }
    dfs(1, 0, true);

#ifndef ONLINE_JUDGE
    auto end_time = clock();
    cerr << "Execution time: " << (end_time - start_time) * (int) 1e3 / CLOCKS_PER_SEC << " ms\n";
#endif

    return 0;

}


#include<bits/stdc++.h>

#define local
#define debu
using namespace std;

const int MAXN = 100005;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const long long MOD = 998244353;

long long n, q, upd;
char buf[MAXN], tar[MAXN];
bool done;

void dfs(int nm, int cnt, bool flg) {
    if (done || cnt > strlen(buf) / 2 || nm - 1 - cnt > strlen(buf) / 2)
        return;
    if (nm > strlen(buf)) {
        if (cnt == strlen(buf) / 2 && strcmp(tar, buf) >= 0) {
            tar[strlen(buf)] = '\0';
            printf("%s\n", tar);
            done = true;
        }
        return;
    }
    if (!flg) {
        for (int i = nm; i <= strlen(buf); i++)
            if (cnt < strlen(buf) / 2) {
                tar[i - 1] = '4';
                cnt++;
            } else
                tar[i - 1] = '7';
        tar[strlen(buf)] = '\0';
        printf("%s\n", tar);
        done = true;
        return;
    }
    if (buf[nm - 1] <= '4') {
        tar[nm - 1] = '4';
        dfs(nm + 1, cnt + 1, buf[nm - 1] == '4');
    }
    if (buf[nm - 1] <= '7') {
        tar[nm - 1] = '7';
        dfs(nm + 1, cnt, buf[nm - 1] == '7');
    }
}

int main() {

#ifdef local
    freopen("testdata.in", "r+", stdin);
    freopen("testdata.out", "w+", stdout);
#endif // local

#ifdef local
    auto start_time = clock();
    cerr << setprecision(3) << fixed;
#endif

//    ios::sync_with_stdio(false);
    scanf("%s", buf);
    for (int i = 0; i < strlen(buf); i++) tar[i] = '7';
    if (strlen(buf) % 2 == 1 || strcmp(tar, buf) < 0) {
        for (int i = 1; i <= strlen(buf) / 2 + 1; i++)
            cout << "4";
        for (int i = 1; i <= strlen(buf) / 2 + 1; i++)
            cout << "7";
        cout << endl;
        return 0;
    }
    dfs(1, 0, true);

#ifdef local
    auto end_time = clock();
    cerr << "Execution time: " << (end_time - start_time) * (int) 1e3 / CLOCKS_PER_SEC << " ms\n";
#endif

    return 0;

}


#include<bits/stdc++.h>

#define loca
#define debu
using namespace std;

const int MAXN = 100005;
const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3f;
const long long MOD = 998244353;

long long n, q, upd;
char buf[MAXN], tar[MAXN];
bool done;

void dfs(int nm, int cnt, bool flg) {
    if (done || cnt > strlen(buf) / 2 || nm - 1 - cnt > strlen(buf) / 2)
        return;
    if (nm > strlen(buf)) {
        if (cnt == strlen(buf) / 2 && strcmp(tar, buf) >= 0) {
            tar[strlen(buf)] = '\0';
            printf("%s\n", tar);
            done = true;
        }
        return;
    }
    if (!flg) {
        for (int i = nm; i <= strlen(buf); i++)
            if (cnt < strlen(buf) / 2) {
                tar[i - 1] = '4';
                cnt++;
            } else
                tar[i - 1] = '7';
        tar[strlen(buf)] = '\0';
        printf("%s\n", tar);
        done = true;
        return;
    }
    if (buf[nm - 1] <= '4') {
        tar[nm - 1] = '4';
        dfs(nm + 1, cnt + 1, buf[nm - 1] == '4');
    }
    if (buf[nm - 1] <= '7') {
        tar[nm - 1] = '7';
        dfs(nm + 1, cnt, buf[nm - 1] == '7');
    }
}

int main() {

#ifdef local
    freopen("testdata.in", "r+", stdin);
    freopen("testdata.out", "w+", stdout);
#endif // local

#ifdef local
    auto start_time = clock();
    cerr << setprecision(3) << fixed;
#endif

//    ios::sync_with_stdio(false);
    scanf("%s", buf);
    for (int i = 0; i < strlen(buf); i++) tar[i] = '7';
    if (strlen(buf) % 2 == 1 || strcmp(tar, buf) < 0) {
        for (int i = 1; i <= strlen(buf) / 2 + 1; i++)
            cout << "4";
        for (int i = 1; i <= strlen(buf) / 2 + 1; i++)
            cout << "7";
        cout << endl;
        return 0;
    }
    dfs(1, 0, true);

#ifdef local
    auto end_time = clock();
    cerr << "Execution time: " << (end_time - start_time) * (int) 1e3 / CLOCKS_PER_SEC << " ms\n";
#endif

    return 0;

}