#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define N 1000100
char str[N], ans[N];
bool half_seven() {
    int len = strlen(str) / 2;
    for (int i = 0; i < len; i++) {
        if (str[i] != '7')
            return false;
    }
    return true;
}
int main() {
    setvbuf(stdout,NULL,_IONBF,0);
    while(scanf(" %s",str)==1) {
        int len=strlen(str);
        if(len&1) {
            for(int i=0;i<len+1;i++) {
                if(i<(len+1)/2) putchar('4');
                else putchar('7');
            }
            puts("");
        }
        else if(half_seven()||str[0]>'7') {
            len+=2;
            for(int i=0;i<len/2;i++) {
                putchar('4');
            }
            for(int i=0;i<len/2;i++) {
                putchar('7');
            }
            puts("");
        }
        else {
            int cnt=0,fc=0,sc=0;
            bool out =false;
            for(int i=0;str[i];i++) {
                if(str[i] == '4') {
                    ans[cnt++]='4';
                    fc++;
                } else if(str[i]=='7') {
                    ans[cnt++]='7';
                    sc++;
                } else if(str[i]<'4') {
                    ans[cnt++]='4';
                    fc++;
                    if(fc>len/2) {
                        fc--;
                        sc++;
                        ans[cnt-1]='7';
                    }
                    ans[cnt]=0;
                    printf("%s",ans);
                    while(fc++<len/2) {
                        putchar('4');
                    }
                    while(sc++<len/2) {
                        putchar('7');
                    }
                    puts("");
                    out=true;
                    break;
                } else if(str[i]<'7') {
                    ans[cnt++]='7';
                    sc++;
                    if(sc>len/2) {
                        while(ans[cnt-1]=='7') {
                            --cnt;
                            sc--;
                        }
                        ans[cnt-1]='7';
                        fc--;
                        sc++;
                    }
                    ans[cnt]=0;
                    printf("%s",ans);
                    while(fc++<len/2) {
                        putchar('4');
                    }
                    while(sc++<len/2) {
                        putchar('7');
                    }
                    puts("");
                    out=true;
                    break;
                } else {
                    while(ans[cnt-1]=='7') {
                        --cnt;
                        sc--;
                        if(cnt==0) break;
                    }
                    if(cnt==0) {
                        len+=2;
                        for(int i=0;i<len/2;i++) {
                            putchar('4');
                        }
                        for(int i=0;i<len/2;i++) {
                            putchar('7');
                        }
                        puts("");
                        out=true;
                        break;
                    }
                    ans[cnt-1]='7';
                    fc--;
                    sc++;
                    ans[cnt]=0;
                    printf("%s",ans);
                    while(fc++<len/2) {
                        putchar('4');
                    }
                    while(sc++<len/2) {
                        putchar('7');
                    }
                    puts("");
                    out=true;
                    break;
                }
            }
            if(!out) {
                ans[cnt]=0;
                puts(ans);
            }
        }
    }
    return 0;
}


#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define N 1000100
char str[N], ans[N];
bool half_seven() {
    int len = strlen(str) / 2;
    for (int i = 0; i < len; i++) {
        if (str[i] != '7')
            return false;
    }
    return true;
}
int main() {
    setvbuf(stdout,NULL,_IONBF,0);
    while(scanf(" %s",str)==1) {
        int len=strlen(str);
        if(len&1) {
            for(int i=0;i<len+1;i++) {
                if(i<(len+1)/2) putchar('4');
                else putchar('7');
            }
            puts("");
        }
        else if(half_seven()) {
            len+=2;
            for(int i=0;i<len/2;i++) {
                putchar('4');
            }
            for(int i=0;i<len/2;i++) {
                putchar('7');
            }
            puts("");
        }
        else {
            int cnt=0,fc=0,sc=0;
            bool out =false;
            for(int i=0;str[i];i++) {
                if(str[i] == '4') {
                    ans[cnt++]='4';
                    fc++;
                } else if(str[i]=='7') {
                    ans[cnt++]='7';
                    sc++;
                } else if(str[i]<'4') {
                    ans[cnt++]='4';
                    fc++;
                    if(fc>len/2) {
                        fc--;
                        sc++;
                        ans[cnt-1]='7';
                    }
                    ans[cnt]=0;
                    printf("%s",ans);
                    while(fc++<len/2) {
                        putchar('4');
                    }
                    while(sc++<len/2) {
                        putchar('7');
                    }
                    puts("");
                    out=true;
                    break;
                } else if(str[i]<'7') {
                    ans[cnt++]='7';
                    sc++;
                    if(sc>len/2) {
                        while(ans[cnt-1]=='7') {
                            --cnt;
                            sc--;
                        }
                        ans[cnt-1]='7';
                        fc--;
                        sc++;
                    }
                    ans[cnt]=0;
                    printf("%s",ans);
                    while(fc++<len/2) {
                        putchar('4');
                    }
                    while(sc++<len/2) {
                        putchar('7');
                    }
                    puts("");
                    out=true;
                    break;
                } else {
                    while(ans[cnt-1]=='7') {
                        --cnt;
                        sc--;
                    }
                    ans[cnt-1]='7';
                    fc--;
                    sc++;
                    ans[cnt]=0;
                    printf("%s",ans);
                    while(fc++<len/2) {
                        putchar('4');
                    }
                    while(sc++<len/2) {
                        putchar('7');
                    }
                    puts("");
                    out=true;
                    break;
                }
            }
            if(!out){
                ans[cnt]=0;
                puts(ans);
            }
        }
    }
    return 0;
}


#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define N 1000100
char str[N], ans[N];
bool half_seven() {
    int len = strlen(str) / 2;
    for (int i = 0; i < len; i++) {
        if (str[i] != '7')
            return false;
    }
    return true;
}
void show(int len) {
    if (len & 1)
        len++;
    for (int i = 0; i < len; i += 2) {
        putchar('4');
    }
    for (int i = 0; i < len; i += 2) {
        putchar('7');
    }
    puts("");
}
int main() {
    setvbuf(stdout,NULL,_IONBF,0);
    while(scanf(" %s",str)==1) {
        int len=strlen(str);
        if(len&1) {
            show(len);
        }
        else if(half_seven()||str[0]>'7') {
            show(len+2);
        }
        else {
            int cnt=0,fc=0,sc=0;
            bool out =false;
            for(int i=0;str[i];i++) {
                if(str[i] == '4') {
                    ans[cnt++]='4';
                    fc++;
                } else if(str[i]=='7') {
                    ans[cnt++]='7';
                    sc++;
                } else if(str[i]<'4') {
                    ans[cnt++]='4';
                    fc++;
                    if(fc>len/2) {
                        fc--;
                        sc++;
                        ans[cnt-1]='7';
                    }
                    ans[cnt]=0;
                    printf("%s",ans);
                    while(fc++<len/2) {
                        putchar('4');
                    }
                    while(sc++<len/2) {
                        putchar('7');
                    }
                    puts("");
                    out=true;
                    break;
                } else if(str[i]<'7') {
                    ans[cnt++]='7';
                    sc++;
                    if(sc>len/2) {
                        while(ans[cnt-1]=='7'||sc>=len/2) {
                            if(ans[cnt]=='7') {
                                sc--;
                            } else fc--;

                            --cnt;
                            if(cnt==0) break;
                        }
                        if(cnt==0) {
                            show(len+2);
                            out=true;
                            break;
                        }
                        ans[cnt-1]='7';
                        fc--;
                        sc++;
                    }
                    ans[cnt]=0;
                    printf("%s",ans);
                    while(fc++<len/2) {
                        putchar('4');
                    }
                    while(sc++<len/2) {
                        putchar('7');
                    }
                    puts("");
                    out=true;
                    break;
                } else {
                    while(ans[cnt-1]=='7'||sc>=len/2) {
                        if(ans[cnt]=='7') {
                            sc--;
                        } else fc--;

                        --cnt;
                        if(cnt==0) break;
                    }
                    if(cnt==0) {
                        show(len+2);
                        out=true;
                        break;
                    }
                    ans[cnt-1]='7';
                    fc--;
                    sc++;
                    ans[cnt]=0;
                    printf("%s",ans);
                    while(fc++<len/2) {
                        putchar('4');
                    }
                    while(sc++<len/2) {
                        putchar('7');
                    }
                    puts("");
                    out=true;
                    break;
                }
                if(fc>len/2) {
                    ans[cnt-1]='7';
                    ans[cnt]=0;
                    fc--;
                    sc++;
                    printf("%s",ans);
                    while(sc++<len/2) {
                        putchar('7');
                    }puts("");
                    out=true;
                    break;
                }
                if(sc>len/2) {
                    while(ans[cnt-1]=='7'||sc>=len/2) {
                        if(ans[cnt]=='7') {
                            sc--;
                        } else fc--;

                        --cnt;
                        if(cnt==0) break;
                    }
                    if(cnt==0) {
                        show(len+2);
                        out=true;
                        break;
                    }
                    ans[cnt-1]='7';
                    fc--;
                    sc++;
                    ans[cnt]=0;
                    printf("%s",ans);
                    while(fc++<len/2) {
                        putchar('4');
                    }
                    while(sc++<len/2) {
                        putchar('7');
                    }
                    puts("");
                    out=true;
                    break;
                }
            }
            if(!out) {
                ans[cnt]=0;
                puts(ans);
            }
        }
    }
    return 0;
}


#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
#define N 1000100
char str[N], ans[N];
bool half_seven() {
    int len = strlen(str) / 2;
    for (int i = 0; i < len; i++) {
        if (str[i] != '7')
            return false;
    }
    return true;
}
int main() {
    setvbuf(stdout,NULL,_IONBF,0);
    while(scanf(" %s",str)==1) {
        int len=strlen(str);
        if(len&1) {
            for(int i=0;i<len+1;i++) {
                if(i<(len+1)/2) putchar('4');
                else putchar('7');
            }
            puts("");
        }
        else if(half_seven()||str[0]>'7') {
            len+=2;
            for(int i=0;i<len/2;i++) {
                putchar('4');
            }
            for(int i=0;i<len/2;i++) {
                putchar('7');
            }
            puts("");
        }
        else {
            int cnt=0,fc=0,sc=0;
            bool out =false;
            for(int i=0;str[i];i++) {
                if(str[i] == '4') {
                    ans[cnt++]='4';
                    fc++;
                } else if(str[i]=='7') {
                    ans[cnt++]='7';
                    sc++;
                } else if(str[i]<'4') {
                    ans[cnt++]='4';
                    fc++;
                    if(fc>len/2) {
                        fc--;
                        sc++;
                        ans[cnt-1]='7';
                    }
                    ans[cnt]=0;
                    printf("%s",ans);
                    while(fc++<len/2) {
                        putchar('4');
                    }
                    while(sc++<len/2) {
                        putchar('7');
                    }
                    puts("");
                    out=true;
                    break;
                } else if(str[i]<'7') {
                    ans[cnt++]='7';
                    sc++;
                    if(sc>len/2) {
                        while(ans[cnt-1]=='7') {
                            --cnt;
                            sc--;
                        }
                        ans[cnt-1]='7';
                        fc--;
                        sc++;
                    }
                    ans[cnt]=0;
                    printf("%s",ans);
                    while(fc++<len/2) {
                        putchar('4');
                    }
                    while(sc++<len/2) {
                        putchar('7');
                    }
                    puts("");
                    out=true;
                    break;
                } else {
                    while(ans[cnt-1]=='7') {
                        --cnt;
                        sc--;
                    }
                    ans[cnt-1]='7';
                    fc--;
                    sc++;
                    ans[cnt]=0;
                    printf("%s",ans);
                    while(fc++<len/2) {
                        putchar('4');
                    }
                    while(sc++<len/2) {
                        putchar('7');
                    }
                    puts("");
                    out=true;
                    break;
                }
            }
            if(!out){
                ans[cnt]=0;
                puts(ans);
            }
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
char ch[100000 + 2];
int len;
vector<int> ans;
int cnt[10];
bool flag;
void push(int c, int x)
{
    while(x--)
    {
        ans.push_back(c);
    }
}
bool dfs(int x)
{
    if(x == len) return false;
    if(ch[x] < '4')
    {
        push(4 ,len - x);
    }
    else if(ch[x] == '4')
    {
        push(4 , 1);
        if(dfs(x + 1))
            ans[x] = 7;
    }
    else if(ch[x] < '7')
    {
        push(7 , 1);
        push(4 , len - x - 1);
    }
    else if(ch[x] == '7')
    {
        push(7 , 1);
        if(dfs(x + 1))
        {
            while(~x)
            {
                if(ans[x] == 7)
                    ans[x] = 4;
                else
                    break;
                --x;
            }
            if(x == -1)
                flag = true;
            else
                ans[x - 1] = 7;
        }
    }
    else
        return true;
    return false;
}
void print(int t, int x)
{
    while(t--)
    {
        printf("%d",x);
    }
}
int main()
{
    scanf("%s",ch);
    len = strlen(ch);
    if(dfs(0)) flag = true;
    if(flag || (len & 1))
    {
        if(flag) ++len;
        print((len + 1) / 2, 4);
        print((len + 1) / 2, 7);
    }
    else
    {
        int i;
        for(i = 0; i < len; ++i)
            ++cnt[ans[i]];
        cnt[0] = (cnt[4] - cnt[7]) / 2;
        if(cnt[0] >= 0)
        {
            i = len - 1;
            while(cnt[0]--)
            {
                while(ans[i] == 7) --i;
                ans[i] = 7;
            }
            for(i = 0; i < ans.size(); ++i)
                printf("%d",ans[i]);
        }
        else
        {
            for(i = len - 1; ~i; --i)
            {
                if(ans[i] == 7)
                    ++cnt[0];
                else
                {
                    if(cnt[0] > 0)
                        break;
                }
            }
            if(i == -1)
            {
                print(len / 2 + 1, 4);
                print(len / 2 + 1, 7);
            }
            else
            {
                for(int j = 0; j < i; ++j)
                    printf("%d",ans[i]);
                print(1 , 7);
                print(len - i - cnt[0], 4);
                print(cnt[0] - 1, 7);
            }
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
char ch[100000 + 2];
int len;
vector<int> ans;
int cnt[10];
bool flag;
void push(int c, int x)
{
    while(x--)
    {
        ans.push_back(c);
    }
}
bool dfs(int x)
{
    if(x == len) return false;
    if(ch[x] < '4')
    {
        push(4 ,len - x);
    }
    else if(ch[x] == '4')
    {
        push(4 , 1);
        if(dfs(x + 1))
        {
            ans[x] = 7;
        }
    }
    else if(ch[x] < '7')
    {
        push(7 , 1);
        push(4 , len - x - 1);
    }
    else if(ch[x] == '7')
    {
        push(7 , 1);
        if(dfs(x + 1))
        {
            while(~x)
            {
                if(ans[x] == 7)
                    ans[x] = 4;
                else
                    break;
                --x;
            }
            if(x == -1)
                flag = true;
            else
                ans[x - 1] = 7;
        }
    }
    else
    {
        push(4 , len - x);
        return true;
    }
    return false;
}
void print(int t, int x)
{
    while(t--)
    {
        printf("%d",x);
    }
}
int main()
{
    scanf("%s",ch);
    len = strlen(ch);
    if(dfs(0)) flag = true;
    if(flag || (len & 1))
    {
        if(flag) ++len;
        print((len + 1) / 2, 4);
        print((len + 1) / 2, 7);
    }
    else
    {
        int i;
        for(i = 0; i < len; ++i)
            ++cnt[ans[i]];
        cnt[0] = (cnt[4] - cnt[7]) / 2;
        if(cnt[0] >= 0)
        {
            i = len - 1;
            while(cnt[0]--)
            {
                while(ans[i] == 7) --i;
                ans[i] = 7;
            }
            for(i = 0; i < ans.size(); ++i)
                printf("%d",ans[i]);
        }
        else
        {
            for(i = len - 1; ~i; --i)
            {
                if(ans[i] == 7)
                    ++cnt[0];
                else
                {
                    if(cnt[0] > 0)
                        break;
                }
            }
            if(i == -1)
            {
                print(len / 2 + 1, 4);
                print(len / 2 + 1, 7);
            }
            else
            {
                for(int j = 0; j < i; ++j)
                    printf("%d",ans[i]);
                print(1 , 7);
                print(len - i - cnt[0], 4);
                print(cnt[0] - 1, 7);
            }
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
char ch[100000 + 2];
int len;
vector<int> ans;
int cnt[10];
bool flag;
void push(int c, int x)
{
    while(x--)
    {
        ans.push_back(c);
    }
}
bool dfs(int x)
{
    if(x == len) return false;
    if(ch[x] < '4')
    {
        push(4 ,len - x);
    }
    else if(ch[x] == '4')
    {
        push(4 , 1);
        if(dfs(x + 1))
        {
            ans[x] = 7;
        }
    }
    else if(ch[x] < '7')
    {
        push(7 , 1);
        push(4 , len - x - 1);
    }
    else if(ch[x] == '7')
    {
        push(7 , 1);
        if(dfs(x + 1))
        {
            while(~x)
            {
                if(ans[x] == 7)
                    ans[x] = 4;
                else
                    break;
                --x;
            }
            if(x == -1)
                flag = true;
            else
                ans[x - 1] = 7;
        }
    }
    else
    {
        push(4 , len - x);
        return true;
    }
    return false;
}
void print(int t, int x)
{
    while(t--)
    {
        printf("%d",x);
    }
}
int main()
{
    scanf("%s",ch);
    len = strlen(ch);
    if(dfs(0)) flag = true;
    if(flag || (len & 1))
    {
        if(flag) ++len;
        print((len + 1) / 2, 4);
        print((len + 1) / 2, 7);
    }
    else
    {
        int i;
        for(i = 0; i < len; ++i)
            ++cnt[ans[i]];
        cnt[0] = (cnt[4] - cnt[7]) / 2;
        if(cnt[0] >= 0)
        {
            i = len - 1;
            while(cnt[0]--)
            {
                while(ans[i] == 7) --i;
                ans[i] = 7;
            }
            for(i = 0; i < ans.size(); ++i)
                printf("%d",ans[i]);
        }
        else
        {
            for(i = len - 1; ~i; --i)
            {
                if(ans[i] == 7)
                    ++cnt[0];
                else
                {
                    if(cnt[0] > 0)
                        break;
                }
            }
            if(i == -1)
            {
                print(len / 2 + 1, 4);
                print(len / 2 + 1, 7);
            }
            else
            {
                for(int j = 0; j < i; ++j)
                    printf("%d",ans[j]);
                print(1 , 7);
                print(len - i - cnt[0], 4);
                print(cnt[0] - 1, 7);
            }
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
char ch[100000 + 2];
int len;
vector<int> ans;
int cnt[10];
bool flag;
void push(int c, int x)
{
    while(x--)
    {
        ans.push_back(c);
    }
}
bool dfs(int x)
{
    if(x == len) return false;
    if(ch[x] < '4')
    {
        push(4 ,len - x);
    }
    else if(ch[x] == '4')
    {
        push(4 , 1);
        if(dfs(x + 1))
            ans[x] = 7;
    }
    else if(ch[x] < '7')
    {
        push(7 , 1);
        push(4 , len - x - 1);
    }
    else if(ch[x] == '7')
    {
        push(7 , 1);
        if(dfs(x + 1))
        {
            while(~x)
            {
                if(ans[x] == 7)
                    ans[x] = 4;
                else
                    break;
                --x;
            }
            if(x == -1)
                flag = true;
            else
                ans[x - 1] = 7;
        }
    }
    else
        return true;
    return false;
}
void print(int t, int x)
{
    while(t--)
    {
        printf("%d",x);
    }
}
int main()
{
    scanf("%s",ch);
    len = strlen(ch);
    dfs(0);
    if(flag || (len & 1))
    {
        print((len + 1) / 2, 4);
        print((len + 1) / 2, 7);
    }
    else
    {
        int i;
        for(i = 0; i < len; ++i)
            ++cnt[ans[i]];
        cnt[0] = (cnt[4] - cnt[7]) / 2;
        if(cnt[0] >= 0)
        {
            i = 0;
            while(cnt[0]--)
            {
                while(ans[i] == 7) ++i;
                ans[i] = 7;
            }
            for(i = 0; i < ans.size(); ++i)
                printf("%d",ans[i]);
        }
        else
        {
            for(i = len - 1; ~i; --i)
            {
                if(ans[i] == 7)
                    ++cnt[0];
                else
                {
                    if(cnt[0] > 0)
                        break;
                }
            }
            if(i == -1)
            {
                print(len / 2 + 1, 4);
                print(len / 2 + 1, 7);
            }
            else
            {
                for(int j = 0; j < i; ++j)
                    printf("%d",ans[i]);
                print(1 , 7);
                print(len - i - cnt[0], 4);
                print(cnt[0] - 1, 7);
            }
        }
    }
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;
char ch[100000 + 2];
int len;
vector<int> ans;
int cnt[10];
bool flag;
void push(int c, int x)
{
    while(x--)
    {
        ans.push_back(c);
    }
}
bool dfs(int x)
{
    if(x == len) return false;
    if(ch[x] < '4')
    {
        push(4 ,len - x);
    }
    else if(ch[x] == '4')
    {
        push(4 , 1);
        if(dfs(x + 1))
            ans[x] = 7;
    }
    else if(ch[x] < '7')
    {
        push(7 , 1);
        push(4 , len - x - 1);
    }
    else if(ch[x] == '7')
    {
        push(7 , 1);
        if(dfs(x + 1))
        {
            while(~x)
            {
                if(ans[x] == 7)
                    ans[x] = 4;
                else
                    break;
                --x;
            }
            if(x == -1)
                flag = true;
            else
                ans[x - 1] = 7;
        }
    }
    else
        return true;
    return false;
}
void print(int t, int x)
{
    while(t--)
    {
        printf("%d",x);
    }
}
int main()
{
    scanf("%s",ch);
    len = strlen(ch);
    dfs(0);
    if(flag || (len & 1))
    {
        print((len + 1) / 2, 4);
        print((len + 1) / 2, 7);
    }
    else
    {
        int i;
        for(i = 0; i < len; ++i)
            ++cnt[ans[i]];
        cnt[0] = (cnt[4] - cnt[7]) / 2;
        if(cnt[0] >= 0)
        {
            i = len - 1;
            while(cnt[0]--)
            {
                while(ans[i] == 7) --i;
                ans[i] = 7;
            }
            for(i = 0; i < ans.size(); ++i)
                printf("%d",ans[i]);
        }
        else
        {
            for(i = len - 1; ~i; --i)
            {
                if(ans[i] == 7)
                    ++cnt[0];
                else
                {
                    if(cnt[0] > 0)
                        break;
                }
            }
            if(i == -1)
            {
                print(len / 2 + 1, 4);
                print(len / 2 + 1, 7);
            }
            else
            {
                for(int j = 0; j < i; ++j)
                    printf("%d",ans[i]);
                print(1 , 7);
                print(len - i - cnt[0], 4);
                print(cnt[0] - 1, 7);
            }
        }
    }
    return 0;
}


#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<cstring>

const int maxn = 1e5 + 10;
char num[maxn];
char tmp[maxn];

int main()
{
    while(scanf("%s", num) != EOF)
    {
        int len = strlen(num);
        if(len % 2)	//奇数位的话，+1位，前一半是4，后一半是7
        {
            for(int i = 0; i < (len + 1) / 2; i++)
            {
                printf("4");
            }
            for(int i = (len + 1) / 2; i < len + 1; i++)
            {
                printf("7");
            }
            printf("\n");
        }
        else
        {
            for(int i = 0; i < (len / 2); i++)
            {
                tmp[i] = '7';
            }
            for(int i = (len / 2); i < len; i++)
            {
                tmp[i] = '4';
            }
            //如果比当前位数情况下最大的超级完美数还要大，就要加两位
            if(strcmp(num, tmp) == 1)
            {
                //加位前一半4，后一半7
                for(int i = 0; i < ((len + 2) / 2); i++)
                {
                    printf("4");
                }
                for(int i = ((len + 2) / 2); i < len + 2; i++)
                {
                    printf("7");
                }
                printf("\n");
            }
            else
            {
                //位数固定，4和7的数量固定，字典序全排列列举比较,找最小的不低于的，就从最小的开始
                for(int i = 0; i < (len / 2); i++)
                {
                    tmp[i] = '4';
                }
                for(int i = (len / 2); i < len; i++)
                {
                    tmp[i] = '7';
                }
                if(strcmp(num, tmp) != 1)
                {
                    for(int i = 0; i < len; i++)
                        printf("%c", tmp[i]);
                    printf("\n");
                }
                else
                {
                    while(std::next_permutation(tmp, tmp + len))
                    {
                        if(strcmp(num, tmp) != 1)
                        {
                            for(int i = 0; i < len; i++)
                                printf("%c", tmp[i]);
                            printf("\n");
                            break;
                        }
                    }
                }
            }
        }
    }
    return 0;
}


/*————————————————————AC代码————————————————————*/
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1e5 + 10;
int len;
char str[maxn];
char ans[maxn];
bool dfs(int pos, int sum1, int sum2, bool flag)	//pos指当前位数，sum1指4的总个数，sum2指7的总个数，flag标识符
{
    if(pos >= len)
        return true;
    if(flag)	//前面有位数变大了，那么后面的数就按一半4一半7排列
    {
        for(int i = 0; i < sum1; i++)
            ans[pos++] = '4';
        for(int i = 0; i < sum2; i++)
            ans[pos++] = '7';
        return true;
    }
    if(sum1 && str[pos] <= '4')
    {
        if(dfs(pos + 1, sum1 - 1, sum2, str[pos] != '4'))	//遍历到一个比4小的数，并且将这个位数的数变成4是可行的
        {
            ans[pos] = '4';
            return true;
        }
    }
    if(sum2 && str[pos] <= '7')
    {
        if(dfs(pos + 1, sum1, sum2 - 1, str[pos] != '7'))	//遍历到一个比7小的数，并且将这个位数的数变成7是可行的
        {
            ans[pos] = '7';
            return true;
        }
    }
    return false;	//没有比4和7小的数，说明当前位数不够，要加两位
}

int main()
{
    cin >> str;
    len = strlen(str);
    if(len & 1 || !dfs(0, len / 2, len / 2, 0))
    {
        if(len & 1)		//奇数位加一位
            len++;
        else	//dfs返回false加两位
            len += 2;
        //加位的话一半4一半7就行
        int i;
        for(i = 0; i < len / 2; i++)
            ans[i] = '4';
        for(i ; i < len; i++)
            ans[i] = '7';
    }
    cout << ans << endl;
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1e5 + 10;
int len;
char str[maxn];
char ans[maxn];
bool dfs(int pos, int sum1, int sum2, bool limit)	//pos指当前位数，sum1指4的总个数，sum2指7的总个数，limit表示限制
{
    if(pos >= len)
        return true;
    if(limit)	//没有限制了，后面的数就按一半4一半7排列
    {
        for(int i = 0; i < sum1; i++)
            ans[pos++] = '4';
        for(int i = 0; i < sum2; i++)
            ans[pos++] = '7';
        return true;
    }
    if(sum1 && str[pos] <= '4')
    {
        if(dfs(pos + 1, sum1 - 1, sum2, str[pos] != '4'))
        {
            ans[pos] = '4';
            return true;
        }
    }
    if(sum2 && str[pos] <= '7')
    {
        if(dfs(pos + 1, sum1, sum2 - 1, str[pos] != '7'))
        {
            ans[pos] = '7';
            return true;
        }
    }
    return false;
}

int main()
{
    cin >> str;
    len = strlen(str);
    if(len & 1 || !dfs(0, len / 2, len / 2, 0))
    {
        if(len & 1)
            len++;
        else
            len += 2;
        for(int i = 0; i < len / 2; i++)
            ans[i] = '4';
        for(int i = 0; i < len / 2; i++)
            ans[i] = '7';
    }
    cout << ans << endl;
    return 0;
}


#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1e5 + 10;
int len;
char str[maxn];
char ans[maxn];
bool dfs(int pos, int sum1, int sum2, bool flag)	//pos指当前位数，sum1指4的总个数，sum2指7的总个数，limit表示限制
{
    if(pos >= len)
        return true;
    if(flag)	//没有限制了，后面的数就按一半4一半7排列
    {
        for(int i = 0; i < sum1; i++)
            ans[pos++] = '4';
        for(int i = 0; i < sum2; i++)
            ans[pos++] = '7';
        return true;
    }
    if(sum1 && str[pos] <= '4')
    {
        if(dfs(pos + 1, sum1 - 1, sum2, str[pos] != '4'))
        {
            ans[pos] = '4';
            return true;
        }
    }
    if(sum2 && str[pos] <= '7')
    {
        if(dfs(pos + 1, sum1, sum2 - 1, str[pos] != '7'))
        {
            ans[pos] = '7';
            return true;
        }
    }
    return false;
}

int main()
{
    cin >> str;
    len = strlen(str);
    if(len & 1 || !dfs(0, len / 2, len / 2, 0))
    {
        if(len & 1)
            len++;
        else
            len += 2;
        int i;
        for(i = 0; i < len / 2; i++)
            ans[i] = '4';
        for(i ; i < len; i++)
            ans[i] = '7';
    }
    cout << ans << endl;
    return 0;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <cctype>

#define ll long long
#define ull unsigned long long
#define all(v)  v.begin() , v.end()
#define f(i,x,y) for (int i = x; i < y; i++)
#define FOR(it,A) for(typeof A.begin() it = A.begin(); it!=A.end(); it++)
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define pii pair< int , int >
#define pll pair< ll , ll >
#define mp make_pair
#define fi first
#define se second
#define sc(x) scanf("%d",&x)
#define me(t,val) memset( t , val , sizeof(t) )

#define MOD 1000000009
#define INF (1<<30)
#define EPS (1e-5)
#define MAX 100005
#define ERR (1e-9)
#define SIZE 100005

using namespace std ;

int main()
{
    string s ; cin >> s ;
    int n = s.size() ;

    int v , f ;
    v = f = 0 ;

    for ( int i = 0 ; i < n ; i++ )
    {
        if ( s[i] == '4' ) f++;
        if ( s[i] == '7' ) v++;
    }

    if ( f + v == n && f == v )
    {
        cout << s << endl;
    }
    else
    {
        if ( n % 2 == 1 )
        {
            cout << string ( (n+1)/2 , '4') << string ( (n+1)/2 , '7') << endl;
        }
        else
        {
            if ( s[0] - '0' > 7 )
            {
                cout << string ( (n)/2 + 1 , '4') << string ( (n)/2 + 1 , '7') << endl;
            }
            else
            {
                string ans = string ( (n)/2 , '4') + string ( (n)/2 , '7') ;

                bool flag = false ;

                for ( int i = n/2 - 1 ; i >= 0 ; i-- )
                {
                     for ( int j = 0 ; j <= n/2 ; j++ )
                     {
                          swap ( ans[i] , ans[i+j] ) ;
                          if ( ans > s )
                          {
                            flag = true ;
                            break;
                          }
                     }
                }

                if ( flag )
                {
                    cout << ans << endl;
                }
                else
                {
                    cout << string ( (n)/2 + 1 , '4') << string ( (n)/2 + 1 , '7') << endl;
                }
            }

        }
    }

    return 0 ;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <cctype>

#define ll long long
#define ull unsigned long long
#define all(v)  v.begin() , v.end()
#define f(i,x,y) for (int i = x; i < y; i++)
#define FOR(it,A) for(typeof A.begin() it = A.begin(); it!=A.end(); it++)
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define pii pair< int , int >
#define pll pair< ll , ll >
#define mp make_pair
#define fi first
#define se second
#define sc(x) scanf("%d",&x)
#define me(t,val) memset( t , val , sizeof(t) )

#define MOD 1000000009
#define INF (1<<30)
#define EPS (1e-5)
#define MAX 100005
#define ERR (1e-9)
#define SIZE 100005

using namespace std ;

int main()
{
    string s,t ; cin >> s ;
    int n = s.size() ;

    if ( n % 2 == 0 )
    {
        if (string(n/2, '7') + string(n/2, '4') < s)
        {
            cout<<string(n/2+1, '4')+string(n/2+1, '7')<<endl;
        }
        else
        {
            int n4 = n/2, n7 = n/2;
            bool f = 0;
            for (int i=0; i<n; ++i) {
                bool ff = 0;
                if (n4 > 0) {
                    if (f || s[i] < '4') ff = 1;
                    else if (s[i] == '4') {
                        for (int j=i+1; j<=n; ++j){
                            if (j == n) {ff = 1; break;}
                            char ch = (j <= i+n7?'7':'4');
                            if (ch > s[j]) ff = 1;
                            if (ch != s[j]) break;
                        }
                    }
                }
                if (ff) {t += "4"; n4--;} else {t += "7"; n7--;}
                if (t[i] > s[i]) f = 1;
            }
            cout<<t<<endl;
        }

    }
    else
    {
        cout<<string(n/2+1,'4')<<string(n/2+1,'7')<<endl;
    }

    return 0 ;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <cctype>

#define ll long long
#define ull unsigned long long
#define all(v)  v.begin() , v.end()
#define f(i,x,y) for (int i = x; i < y; i++)
#define FOR(it,A) for(typeof A.begin() it = A.begin(); it!=A.end(); it++)
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define pii pair< int , int >
#define pll pair< ll , ll >
#define mp make_pair
#define fi first
#define se second
#define sc(x) scanf("%d",&x)
#define me(t,val) memset( t , val , sizeof(t) )

#define MOD 1000000009
#define INF (1<<30)
#define EPS (1e-5)
#define MAX 100005
#define ERR (1e-9)
#define SIZE 100005

using namespace std ;

int main()
{
    string s ; cin >> s ;
    int n = s.size() ;

    int v , f ;
    v = f = 0 ;

    for ( int i = 0 ; i < n ; i++ )
    {
        if ( s[i] == '4' ) f++;
        if ( s[i] == '7' ) v++;
    }

    if ( f + v == n && f == v )
    {
        cout << s << endl;
    }
    else
    {
        if ( n % 2 == 1 )
        {
            cout << string ( (n+1)/2 , '4') << string ( (n+1)/2 , '7') << endl;
        }
        else
        {
            if ( s[0] - '0' > 7 )
            {
                cout << string ( (n)/2 + 1 , '4') << string ( (n)/2 + 1 , '7') << endl;
            }
            else
            {
                string ans = string ( (n)/2 , '4') + string ( (n)/2 , '7') ;

                bool flag = ( ans > s ) ;
                if ( !flag )
                {
                    int k = 0 ;
                    for ( int i = n/2 - 1 ; i >= 0 ; i-- )
                    {
                         int j = i ;
                         while ( j < n-1 - k )
                         {
                            swap ( ans[j] , ans[j+1] ) ;

                            j++;
                            if ( ans > s )
                              {
                                flag = true ;
                                break;
                              }
                         }
                         k++;

                         if ( flag ) break;

                    }
                }

                if ( flag )
                {
                    cout << ans << endl;
                }
                else
                {
                    cout << string ( (n)/2 + 1 , '4') << string ( (n)/2 + 1 , '7') << endl;
                }
            }

        }
    }

    return 0 ;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <cctype>

#define ll long long
#define ull unsigned long long
#define all(v)  v.begin() , v.end()
#define f(i,x,y) for (int i = x; i < y; i++)
#define FOR(it,A) for(typeof A.begin() it = A.begin(); it!=A.end(); it++)
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define pii pair< int , int >
#define pll pair< ll , ll >
#define mp make_pair
#define fi first
#define se second
#define sc(x) scanf("%d",&x)
#define me(t,val) memset( t , val , sizeof(t) )

#define MOD 1000000009
#define INF (1<<30)
#define EPS (1e-5)
#define MAX 100005
#define ERR (1e-9)
#define SIZE 100005

using namespace std ;

int main()
{
    string s ; cin >> s ;
    int n = s.size() ;

    int v , f ;
    v = f = 0 ;

    for ( int i = 0 ; i < n ; i++ )
    {
        if ( s[i] == '4' ) f++;
        if ( s[i] == '7' ) v++;
    }

    if ( f + v == n && f == v )
    {
        cout << s << endl;
    }
    else
    {
        if ( n % 2 == 1 )
        {
            cout << string ( (n+1)/2 , '4') << string ( (n+1)/2 , '7') << endl;
        }
        else
        {
            if ( s[0] - '0' > 7 )
            {
                cout << string ( (n)/2 + 1 , '4') << string ( (n)/2 + 1 , '7') << endl;
            }
            else
            {
                string ans = string ( (n)/2 , '4') + string ( (n)/2 , '7') ;

                bool flag = false ;
                int k = 0 ;
                for ( int i = n/2 - 1 ; i >= 0 ; i-- )
                {
                     int j = i ;
                     while ( j < n-1 - k )
                     {
                        swap ( ans[j] , ans[j+1] ) ;

                        j++;
                        if ( ans > s )
                          {
                            flag = true ;
                            break;
                          }
                     }
                     k++;

                     if ( flag ) break;

                }

                if ( flag )
                {
                    cout << ans << endl;
                }
                else
                {
                    cout << string ( (n)/2 + 1 , '4') << string ( (n)/2 + 1 , '7') << endl;
                }
            }

        }
    }

    return 0 ;
}


#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <cctype>

#define ll long long
#define ull unsigned long long
#define all(v)  v.begin() , v.end()
#define f(i,x,y) for (int i = x; i < y; i++)
#define FOR(it,A) for(typeof A.begin() it = A.begin(); it!=A.end(); it++)
#define pb push_back
#define vi vector<int>
#define vll vector<ll>
#define vull vector<ull>
#define pii pair< int , int >
#define pll pair< ll , ll >
#define mp make_pair
#define fi first
#define se second
#define sc(x) scanf("%d",&x)
#define me(t,val) memset( t , val , sizeof(t) )

#define MOD 1000000009
#define INF (1<<30)
#define EPS (1e-5)
#define MAX 100005
#define ERR (1e-9)
#define SIZE 100005

using namespace std ;

int main()
{
    string s ; cin >> s ;
    int n = s.size() ;

    int v , f ;
    v = f = 0 ;

    for ( int i = 0 ; i < n ; i++ )
    {
        if ( s[i] == '4' ) f++;
        if ( s[i] == '7' ) v++;
    }

    if ( f + v == n && f == v )
    {
        cout << s << endl;
    }
    else
    {
        if ( n % 2 == 1 )
        {
            cout << string ( (n+1)/2 , '4') << string ( (n+1)/2 , '7') << endl;
        }
        else
        {
            if ( s[0] - '0' > 7 )
            {
                cout << string ( (n)/2 + 1 , '4') << string ( (n)/2 + 1 , '7') << endl;
            }
            else
            {
                string ans = string ( (n)/2 , '4') + string ( (n)/2 , '7') ;

                bool flag = false ;
                int k = 0 ;
                for ( int i = n/2 - 1 ; i >= 0 ; i-- )
                {
                     int j = i ;
                     while ( j < n-1 - k )
                     {
                        swap ( ans[j] , ans[j+1] ) ;
                        j++;
                        if ( ans > s )
                          {
                            flag = true ;
                            break;
                          }
                     }
                     k++;

                }

                if ( flag )
                {
                    cout << ans << endl;
                }
                else
                {
                    cout << string ( (n)/2 + 1 , '4') << string ( (n)/2 + 1 , '7') << endl;
                }
            }

        }
    }

    return 0 ;
}


#pragma comment(linker, "/STACK:100000000000000")
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string.h>
#include <cctype>
#include <cassert>
#include <ctime>
#define mp make_pair
#define pb push_back
#define lo int
#define li long long int
#define db double
#define pi 3.14159265358979323
#define eps 1e-6
#define MN (1<<24) + 201001
using namespace std;
const double INF= 2e50;

string can(string &s)
{
    lo n = (lo) s.size();
    bool yes = false;
    lo a = n / 2, b = n / 2;
    string out = "";
    for(lo i = 0; i < n; i++)
    {
        if(yes)
        {
            if(a)
            {
                out += "4";
                a--;
            }
            else
            {
                out += "7";
                b--;
            }
        }
        else
        {
            if(s[i] <= '4')
            {
                if(a)
                {
                    a--;
                    out += '4';
                }
                else
                {
                    if(!b)
                    {
                        return "";
                    }
                    b--;
                    out += "7";
                }

            }
            else
            {

                if(s[i] <= '7' && b)
                {
                    b--;
                    out += '7';
                }
                else
                {
                    i--;
                    while(i >= 0 && ((*out.rbegin()) == '7' || !b))
                    {
                        if((*out.rbegin()) == '7')
                            b++;
                        else
                            a++;
                        out.erase((--out.end()));
                        i--;
                    }
                    if(i == -1)
                        return "";
                    out[out.size() - 1] = '7';
                    b--;
                    a++;
                    yes = true;
                    continue;
                }
            }
            if(*out.rbegin() > s[i])
                    yes = true;
        }
    }
    return out;
}
int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    string s;
    cin >> s;
    lo n = (lo) s.size();
    if(n & 1)
    {
        for(lo i = 0; i < n + 1; i++)
        {
            if(i < (n + 1) / 2)
                cout << 4;
            else
                cout << 7;
        }
        return 0;
    }
    string out = can(s);
    if(out.size() == 0)
    {
        for(lo i = 0; i < n + 2; i++)
        {
            if(i < (n + 2) / 2)
                cout << 4;
            else
                cout << 7;
        }
        return 0;
    }
    cout << out;
}


#pragma comment(linker, "/STACK:100000000000000")
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string.h>
#include <cctype>
#include <cassert>
#include <ctime>
#define mp make_pair
#define pb push_back
#define lo int
#define li long long int
#define db double
#define pi 3.14159265358979323
#define eps 1e-6
#define MN (1<<24) + 201001
using namespace std;
const double INF= 2e50;

string can(string &s)
{
    lo n = (lo) s.size();
    bool yes = false;
    lo a = n / 2, b = n / 2;
    string out = "";
    for(lo i = 0; i < n; i++)
    {
        if(yes)
        {
            if(a)
            {
                out += "4";
                a--;
            }
            else
            {
                out += "7";
                b--;
            }
        }
        else
        {
            if(s[i] <= '4')
            {
                if(a)
                {
                    a--;
                    out += '4';
                }
                else
                {
                    if(!b)
                    {
                        return "";
                    }
                    b--;
                    out += "7";
                }

            }
            else
            {

                if(s[i] <= '7' && b)
                {
                    b--;
                    out += '7';
                }
                else
                {
                    i--;
                    while(i >= 0 && ((*out.rbegin()) == '7' || !b))
                    {
                        b++;
                        out.erase((--out.end()));
                        i--;
                    }
                    if(i == -1)
                        return "";
                    out[out.size() - 1] = '7';
                    b--;
                    a++;
                    yes = true;
                    continue;
                }
            }
            if(*out.rbegin() > s[i])
                    yes = true;
        }
    }
    return out;
}
int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    string s;
    cin >> s;
    lo n = (lo) s.size();
    if(n & 1)
    {
        for(lo i = 0; i < n + 1; i++)
        {
            if(i < (n + 1) / 2)
                cout << 4;
            else
                cout << 7;
        }
        return 0;
    }
    string out = can(s);
    if(out.size() == 0)
    {
        for(lo i = 0; i < n + 2; i++)
        {
            if(i < (n + 2) / 2)
                cout << 4;
            else
                cout << 7;
        }
        return 0;
    }
    cout << out;
}


#pragma comment(linker, "/STACK:100000000000000")
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string.h>
#include <cctype>
#include <cassert>
#include <ctime>
#define mp make_pair
#define pb push_back
#define lo int
#define li long long int
#define db double
#define pi 3.14159265358979323
#define eps 1e-6
#define MN (1<<24) + 201001
using namespace std;
const double INF= 2e50;

string can(string &s)
{
    lo n = (lo) s.size();
    bool yes = false;
    lo a = n / 2, b = n / 2;
    string out = "";
    for(lo i = 0; i < n; i++)
    {
        if(yes)
        {
            if(a)
            {
                out += "4";
                a--;
            }
            else
            {
                out += "7";
                b--;
            }
        }
        else
        {
            if(s[i] <= '4')
            {
                if(a)
                {
                    a--;
                    out += '4';
                }
                else
                {
                    if(!b)
                    {
                        return "";
                    }
                    b--;
                    out += "7";
                }

            }
            else
            {
                if(!b)
                    return "";
                if(s[i] <= '7')
                {
                    b--;
                    out += '7';
                }
                else
                {
                    i--;
                    while(i > 0 && *out.rbegin() == '7')
                    {
                        b++;
                        out.erase((--out.end()));
                        i--;
                    }
                    if(i == -1)
                        return "";
                    out[out.size() - 1] = '7';
                    b--;
                    a++;
                    yes = true;
                    continue;
                }
            }
            if(*out.rbegin() > s[i])
                    yes = true;
        }
    }
    return out;
}
int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    string s;
    cin >> s;
    lo n = (lo) s.size();
    if(n & 1)
    {
        for(lo i = 0; i < n + 1; i++)
        {
            if(i < (n + 1) / 2)
                cout << 4;
            else
                cout << 7;
        }
        return 0;
    }
    string out = can(s);
    if(out.size() == 0)
    {
        for(lo i = 0; i < n + 1; i++)
        {
            if(i < (n + 2) / 2)
                cout << 4;
            else
                cout << 7;
        }
        return 0;
    }
    cout << out;
}


#pragma comment(linker, "/STACK:100000000000000")
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string.h>
#include <cctype>
#include <cassert>
#include <ctime>
#define mp make_pair
#define pb push_back
#define lo int
#define li long long int
#define db double
#define pi 3.14159265358979323
#define eps 1e-6
#define MN (1<<24) + 201001
using namespace std;
const double INF= 2e50;

string can(string &s)
{
    lo n = (lo) s.size();
    bool yes = false;
    lo a = n / 2, b = n / 2;
    string out = "";
    for(lo i = 0; i < n; i++)
    {
        if(yes)
        {
            if(a)
            {
                out += "4";
                a--;
            }
            else
            {
                out += "7";
                b--;
            }
        }
        else
        {
            if(s[i] <= '4')
            {
                if(a)
                {
                    a--;
                    out += '4';
                }
                else
                {
                    if(!b)
                    {
                        return "";
                    }
                    b--;
                    out += "7";
                }

            }
            else
            {
                if(!b)
                    return "";
                if(s[i] <= '7')
                {
                    b--;
                    out += '7';
                }
                else
                {
                    i--;
                    while(i >= 0 && (*out.rbegin()) == '7')
                    {
                        b++;
                        out.erase((--out.end()));
                        i--;
                    }
                    if(i == -1)
                        return "";
                    out[out.size() - 1] = '7';
                    b--;
                    a++;
                    yes = true;
                    continue;
                }
            }
            if(*out.rbegin() > s[i])
                    yes = true;
        }
    }
    return out;
}
int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    string s;
    cin >> s;
    lo n = (lo) s.size();
    if(n & 1)
    {
        for(lo i = 0; i < n + 1; i++)
        {
            if(i < (n + 1) / 2)
                cout << 4;
            else
                cout << 7;
        }
        return 0;
    }
    string out = can(s);
    if(out.size() == 0)
    {
        for(lo i = 0; i < n + 2; i++)
        {
            if(i < (n + 2) / 2)
                cout << 4;
            else
                cout << 7;
        }
        return 0;
    }
    cout << out;
}


#pragma comment(linker, "/STACK:100000000000000")
#include <stdio.h>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <set>
#include <stack>
#include <queue>
#include <math.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string.h>
#include <cctype>
#include <cassert>
#include <ctime>
#define mp make_pair
#define pb push_back
#define lo int
#define li long long int
#define db double
#define pi 3.14159265358979323
#define eps 1e-6
#define MN (1<<24) + 201001
using namespace std;
const double INF= 2e50;

string can(string &s)
{
    lo n = (lo) s.size();
    bool yes = false;
    lo a = n / 2, b = n / 2;
    string out = "";
    for(lo i = 0; i < n; i++)
    {
        if(yes)
        {
            if(a)
            {
                out += "4";
                a--;
            }
            else
            {
                out += "7";
                b--;
            }
        }
        else
        {
            if(s[i] <= '4')
            {
                if(a)
                {
                    a--;
                    out += '4';
                }
                else
                {
                    if(!b)
                    {
                        return "";
                    }
                    b--;
                    out += "7";
                }

            }
            else
            {

                if(s[i] <= '7' && b)
                {
                    b--;
                    out += '7';
                }
                else
                {
                    i--;
                    while(i >= 0 && (*out.rbegin()) == '7')
                    {
                        b++;
                        out.erase((--out.end()));
                        i--;
                    }
                    if(i == -1)
                        return "";
                    out[out.size() - 1] = '7';
                    b--;
                    a++;
                    yes = true;
                    continue;
                }
            }
            if(*out.rbegin() > s[i])
                    yes = true;
        }
    }
    return out;
}
int main()
{
#ifdef _DEBUG
    freopen("input.txt", "r", stdin);
#endif
    string s;
    cin >> s;
    lo n = (lo) s.size();
    if(n & 1)
    {
        for(lo i = 0; i < n + 1; i++)
        {
            if(i < (n + 1) / 2)
                cout << 4;
            else
                cout << 7;
        }
        return 0;
    }
    string out = can(s);
    if(out.size() == 0)
    {
        for(lo i = 0; i < n + 2; i++)
        {
            if(i < (n + 2) / 2)
                cout << 4;
            else
                cout << 7;
        }
        return 0;
    }
    cout << out;
}


#include <iostream>
#include <string>
using namespace std;

// input
string input;
// metadata
int len, half_len;
// output
string output;

void dfs(int step, int n4, int n7, bool large)
{
    if (step == input.size())
        return;
    if (large) {
        if (n4 > 0) output[step] = '4' , dfs(step+1, n4-1, n7,   large);
        else        output[step] = '7' , dfs(step+1, n4,   n7-1, large);
    }
    if (input[step] <= '4' && n4 > 0)
        output[step] = '4', dfs(step+1, n4-1, n7,   input[step]<='4');
    else if (input[step] <= '7' && n7 > 0)
        output[step] = '7', dfs(step+1, n4,   n7-1, input[step]<='7');
}

int main()
{
    // input
    cin >> input;

    // solve
    len      = input.size();
    half_len = (input.size() + 1) / 2;
    if (len % 2 == 1)
        output = string(half_len, '4') + string(half_len, '7');
    else {
        if ( input > string(half_len, '7') + string(half_len, '4') )
            output = string(half_len + 1, '4') + string(half_len + 1, '7');
        else
            output = input, dfs(0, half_len, half_len, false);
    }

    // output
    cout << output << endl;

    return 0;
}


#include <iostream>
#include <string>
using namespace std;

// input
string input;
// metadata
int len, half_len;
// output
string output;

bool dfs(int step, int n4, int n7, bool large)
{
    if (step == len)
        return true;
    if (large) {
        if (n4 > 0) return output[step] = '4' ,dfs(step+1, n4-1, n7,   large);
        else        return output[step] = '7' ,dfs(step+1, n4,   n7-1, large);
    }
    if (n4 > 0 && input[step] <= '4') {
        output[step] = '4';
        if ( dfs(step+1, n4-1, n7, input[step]<'4') ) return true;
    }
    if (n7 > 0 && input[step] <= '7') {
        output[step] = '7';
        if ( dfs(step+1, n4, n7-1, input[step]<'7') ) return true;
    }
    return false;
}

int main()
{
    // input
    while (cin >> input) {
    //cin >> input;

    // solve
    len      = input.size();
    half_len = (input.size() + 1) / 2;
    if (len % 2 == 1)
        output = string(half_len, '4') + string(half_len, '7');
    else {
        if ( input > string(half_len, '7') + string(half_len, '4') )
            output = string(half_len + 1, '4') + string(half_len + 1, '7');
        else
            output = input, dfs(0, half_len, half_len, false);
    }

    // output
    cout << output << endl;
    }

    return 0;
}


#include <iostream>
#include <string>
using namespace std;

// input
string input;
// metadata
int len, half_len;
// output
string output;

void dfs(int step, int n4, int n7, bool large)
{
    if (step == input.size() && output > input)
        return;
    if (large) {
        if (n4 > 0) output[step] = '4' , dfs(step+1, n4-1, n7,   large);
        else        output[step] = '7' , dfs(step+1, n4,   n7-1, large);
    }
    if (input[step] <= '4' && n4 > 0)
        output[step] = '4', dfs(step+1, n4-1, n7,   input[step]<'4');
    else if (input[step] <= '7' && n7 > 0)
        output[step] = '7', dfs(step+1, n4,   n7-1, input[step]<'7');
}

int main()
{
    // input
    cin >> input;

    // solve
    len      = input.size();
    half_len = (input.size() + 1) / 2;
    if (len % 2 == 1)
        output = string(half_len, '4') + string(half_len, '7');
    else {
        if ( input > string(half_len, '7') + string(half_len, '4') )
            output = string(half_len + 1, '4') + string(half_len + 1, '7');
        else
            output = input, dfs(0, half_len, half_len, false);
    }

    // output
    cout << output << endl;

    return 0;
}


#include <iostream>
#include <string>
using namespace std;

// input
string input;
// metadata
int len, half_len;
// output
string output;

bool dfs(int step, int n4, int n7, bool large)
{
    if (step == input.size())
        return true;
    if (large) {
        if (n4 > 0) return output[step] = '4' ,dfs(step+1, n4-1, n7,   large);
        else        return output[step] = '7' ,dfs(step+1, n4,   n7-1, large);
    }
    if (n4 > 0 && input[step] <= '4') {
        output[step] = '4';
        if ( dfs(step+1, n4-1, n7, input[step]<'4') ) return true;
    }
    if (n7 > 0 && input[step] <= '7') {
        output[step] = '7';
        if ( dfs(step+1, n4, n7-1, input[step]<'7') ) return true;
    }
    return false;
}

int main()
{
    // input
    while (cin >> input) {
    //cin >> input;

    // solve
    len      = input.size();
    half_len = (input.size() + 1) / 2;
    if (len % 2 == 1)
        output = string(half_len, '4') + string(half_len, '7');
    else {
        if ( input > string(half_len, '7') + string(half_len, '4') )
            output = string(half_len + 1, '4') + string(half_len + 1, '7');
        else
            output = input, dfs(0, half_len, half_len, false);
    }

    // output
    cout << output << endl;
    }

    return 0;
}


#include <iostream>
#include <string>
using namespace std;

// input
string input;
// metadata
int len, half_len;
bool found = false;
// output
string output;

void dfs(int step, int n4, int n7, bool large)
{

    if (step == len) found = true;
    if (found) return;

    if (large) {
        if (n4) output[step]='4', dfs(step+1, n4-1, n7,   true);
        else    output[step]='7', dfs(step+1, n4,   n7-1, true);
    }
    else
    {
        if (input[step] <= '4' && n4) output[step]='4', dfs(step+1, n4-1, n7, input[step]<'4');
        if (found) return;
        if (input[step] <= '7' && n7) output[step]='7', dfs(step+1, n4, n7-1, input[step]<'7');
    }
}

int main()
{
    // input
    cin >> input;

    // solve
    len      = input.size();
    half_len = (input.size() + 1) / 2;

    if (len % 2 == 1)
        output = string(half_len, '4') + string(half_len, '7');
    else {
        if ( input > string(half_len, '7') + string(half_len, '4') )
            output = string(half_len + 1, '4') + string(half_len + 1, '7');
        else
            output = input, dfs(0, half_len, half_len, false);
    }

    // output
    cout << output << endl;

    return 0;
}


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

using namespace std;

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

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

int nums[200000];

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

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

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

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

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

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


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

using namespace std;

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

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

bool check(string s){
    int n = s.size();
    rep(i, n / 2) {
        if(s[i] > '7') return false;
        if(s[i] < '7') return true;
    }
    rep(i, n / 2){
        if(s[i+n/2] > '4') return false;
        if(s[i+n/2] < '4') return true;
    }
    return true;
}

int plu[200000];

int main(){
    string number;
    cin >> number;
    int n = number.size();
    if(n % 2 == 1 || !check(number)){
        rep(i, n / 2 + 1) cout << 4;
        rep(i, n / 2 + 1) cout << 7;
    }else{
        memset(plu, 0, sizeof(plu));
        repD(i, n-1){
            if(number[i] > '7' || number[i] == '7' && plu[i]){
                number[i] = '0';
                plu[i-1]++;
            }
        }

        bool f = false;
        int S = n/2, F = n/2;
        rep(i, n){
            if(f){
                if(F > 0) F--, number[i] = '4';
                else S++, number[i] = '7';
            }else if(F > 0 && number[i] + plu[i] <= '4'){
                if(number[i] + plu[i] < '4') f = true;
                number[i] = '4';
                F--;
            }else if(S > 0 && number[i] + plu[i] <= '7'){
                if(number[i] + plu[i] < '7') f = true;
                number[i] ='7';
                S--;
            }else{
                if(S == 0 && number[i] + plu[i] > '4'){
                    i--;
                    while(number[i] != '4'){
                        S++;
                        i--;
                    }
                    number[i] = '7';
                    S--;
                    F++;
                    f = true;
                }else if(S == 0){
                    number[i] = '4';
                    F--;
                }
            }
        }
        cout << number;
    }
    cout << endl;
    return 0;
}


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

using namespace std;

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

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

int nums[200000];

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

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

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

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

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


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

using namespace std;

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

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

string ans;
string number;
int n;

void solve(int cnt, int f, int s, bool ok){
    if(cnt == 0){
        rep(i, n) cout << ans[i];
        cout << endl;
        exit(0);
    }
    if(f > 0 && (number[n-cnt] <= '4' || ok)){
        ans[n-cnt] = '4';
        solve(cnt-1, f-1, s, ok || (number[n-cnt] < '4'));
    }
    if(s > 0 && (number[n-cnt] <= '7' || ok)){
        ans[n-cnt] = '7';
        solve(cnt - 1, f, s - 1, ok || (number[n-cnt] < '7'));
    }
}

void doit(int l){
    if(l % 2 == 1) return ;
    if(l > n) {
        rep(i, l /2) cout << 4;
        rep(i, l / 2) cout << 7;
        cout << endl;
        exit(0);
    }
    solve(n, n / 2, n / 2, false);
}

int main(){

    cin >> number;
    n = number.size();
    ans = string(' ', n + 2);

    doit(n);
    doit(n+1);
    doit(n+2);
    return 0;
}


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

using namespace std;

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

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

string ans;
string number;
int n;

void solve(int cnt, int f, int s, bool ok){
    if(cnt == 0){
        rep(i, n) cout << ans[i];
        cout << endl;
        exit(0);
    }
    if(f > 0 && (number[n-cnt] <= '4' || ok)){
        ans[n-cnt] = '4';
        solve(cnt-1, f-1, s, ok || (number[n-cnt] < '4'));
    }
    if(s > 0 && (number[n-cnt] <= '7' || ok)){
        ans[n-cnt] = '7';
        solve(cnt-1, f, s - 1, ok || (number[n-cnt] < '7'));
    }
}

void doit(int l){
    if(l % 2 == 1) return ;
    if(l > n) {
        rep(i, l /2) cout << 4;
        rep(i, l / 2) cout << 7;
        cout << endl;
        exit(0);
    }
    solve(n, n / 2, n / 2, false);
}

int main(){

    cin >> number;
    n = number.size();
    ans = string(n+2, ' ');

    doit(n);
    doit(n+1);
    doit(n+2);
    return 0;
}


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

using namespace std;

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

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

bool check(string s){
    int n = s.size();
    rep(i, n / 2) {
        if(s[i] > '7') return false;
        if(s[i] < '7') return true;
    }
    rep(i, n / 2){
        if(s[i+n/2] > '4') return false;
        if(s[i+n/2] < '4') return true;
    }
    return true;
}

int plu[200000];

int main(){
    string number;
    cin >> number;
    int n = number.size();
    if(n % 2 == 1 || !check(number)){
        rep(i, n / 2 + 1) cout << 4;
        rep(i, n / 2 + 1) cout << 7;
    }else{
        memset(plu, 0, sizeof(plu));
        repD(i, n-1){
            if(number[i] > '7' || number[i] == '7' && plu[i]){
                plu[i-1]++;
            }
        }

        bool f = false;
        int S = n/2, F = n/2;
        rep(i, n){
            if(f){
                if(F > 0) F--, number[i] = '4';
                else S++, number[i] = '7';
            }else if(F > 0 && number[i] + plu[i] <= '4'){
                if(number[i] + plu[i] < '4') f = true;
                number[i] = '4';
                F--;
            }else if(S > 0 && number[i] + plu[i] <= '7'){
                if(number[i] + plu[i] < '7') f = true;
                number[i] ='7';
                S--;
            }else{
                if(S == 0 && number[i] + plu[i] > '4'){
                    i--;
                    while(number[i] != '4'){
                        S++;
                        i--;
                    }
                    number[i] = '7';
                    S--;
                    F++;
                    f = true;
                }
            }
        }
        cout << number;
    }
    cout << endl;
    return 0;
}


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

using namespace std;

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

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

int nums[200000];

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

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

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

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

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

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


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

using namespace std;

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

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

int nums[200000];

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

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

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

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

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

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


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

using namespace std;

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

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

int nums[200000];

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

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

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

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

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

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

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


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

using namespace std;

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

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

int nums[200000];

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

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

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

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

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

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

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


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

using namespace std;

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

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

bool check(string s){
    int n = s.size();
    rep(i, n / 2) {
        if(s[i] > '7') return false;
        if(s[i] < '7') return true;
    }
    rep(i, n / 2){
        if(s[i+n/2] > '4') return false;
        if(s[i+n/2] < '4') return true;
    }
    return true;
}

int plu[200000];

int main(){
    string number;
    cin >> number;
    int n = number.size();
    if(n % 2 == 1 || !check(number)){
        rep(i, n / 2 + 1) cout << 4;
        rep(i, n / 2 + 1) cout << 7;
    }else{
        memset(plu, 0, sizeof(plu));
        repD(i, n-1){
            if(number[i] > '7' || number[i] == '7' && plu[i]){
                number[i] = '0';
                plu[i-1]++;
            }
        }

        bool f = false;
        int S = n/2, F = n/2;
        rep(i, n){
            if(f){
                if(F > 0) F--, number[i] = '4';
                else S++, number[i] = '7';
            }else if(F > 0 && number[i] + plu[i] <= '4'){
                if(number[i] + plu[i] < '4') f = true;
                number[i] = '4';
                F--;
            }else if(S > 0 && number[i] + plu[i] <= '7'){
                if(number[i] + plu[i] < '7') f = true;
                number[i] ='7';
                S--;
            }else{
                if(S == 0 && number[i] + plu[i] > '4'){
                    i--;
                    while(number[i] != '4'){
                        S++;
                        i--;
                    }
                    number[i] = '7';
                    S--;
                    F++;
                    f = true;
                }
            }
        }
        cout << number;
    }
    cout << endl;
    return 0;
}