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

int ans = 1e9 ;
int mn =  1e9 ;
map < int , int > last ;

main()
{
    int n; cin>>n;
    for ( int i=1 ; i<=n ; i++ ) {
        int a; cin>>a;
        if ( last[ a ] and a <= mn ) ans = min( ans , i-last[a] ) ;
        mn = min( mn , a ) ;
        last[a] = i ;
    }
    return cout << ans << endl , EXIT_SUCCESS ;
}


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

int ans = 1e9 ;
int mn =  1e4+50 ;
map < int , int > last ;

main()
{
    int n; cin>>n;
    for ( int i=1 ; i<=n ; i++ ) {
        int a; cin>>a;
        if ( a <= mn ) ans = 1e4+50 ;
        if ( last[ a ] and a <= mn ) ans = min( ans , i-last[a] ) ;
        mn = min( mn , a ) ;
        last[a] = i ;
    }
    return cout << ans << endl , EXIT_SUCCESS ;
}


#include<bits/stdc++.h>

#define pb push_back

using namespace std ;

typedef long long int ll ;

const int maxn = 1e5;
const int inf = 1e9 + 3 ;

ll a[maxn];
bool mark[maxn];
vector <ll> b;

int main()
{
	int n;
	cin>>n;
	ll mn=inf , MN=inf;
	for(int i= 0 ;i<n ;i++)
	{
		cin>>a[i];
		mn=min(mn , a[i]);
	}
	for(int i=0;i<n;i++)
		if(a[i] == mn)
			b.pb(i);
	for(int i=0 ;i<b.size()-1; i++)
		MN=min(MN,b[i+1]-b[i]);
	cout<<MN<<endl;
}


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

int ans = 1e9 ;
int mn =  1e9 ;
map < int , int > last ;

main()
{
    int n; cin>>n;
    for ( int i=1 ; i<=n ; i++ ) {
        int a; cin>>a;
        if ( last[ a ] and a <= mn ) ans = min( ans , i-last[a] ) ;
        smin( mn , a ) ;
        last[a] = i ;
    }
    return cout << ans << endl , EXIT_SUCCESS ;
}


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

const int maxn = 1e4+100 ;
const int mod = 1e9 + 7 ;
const int inf = 1e9 + 3 ;

int a[maxn];
bool mark[maxn];
vector <int> b;

int main()
{
	int n;
	cin>>n;
	int mn=inf , MN=inf;
	for(int i= 0 ;i<n ;i++) {
		cin>>a[i];
		mn=min(mn , a[i]);
	}
	for(int i=0;i<n;i++)
		if(a[i] == mn)
			b.push_back(i);
	for(int i=0 ;i<b.size()-1; i++)
		MN=min(MN,b[i+1]-b[i]);
	cout<<MN<<endl;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

int main(){
    int a;
    ll mn=1e10;
    //cout<<mn<<endl;
    scanf("%d",&a);
    map<ll,ll> mp1,mp2;
    map<ll,ll>::iterator it;
    for(int i=1;i<=a;i++){
        ll b;
        scanf("%lld",&b);
        if(mp1[b]==0) mp1[b]=i;
        else{
            ll c=abs(mp1[b]-i);
            //cout<<c<<endl;
            if(mp2[b]!=0){
                int x=min(mp2[b],c);
                mp2[b]=x;
            }
            else mp2[b]=c;
            mp1[b]=i;
        }
    }
    for(it=mp2.begin();it!=mp2.end();it++){
        mn=min(mn,it->second);
    }
    printf("%lld",mn);
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

int main(){
    int a;
    int mn=1e9;
    //cout<<mn<<endl;
    scanf("%d",&a);
    map<int,int>mp;
    for(int i=1;i<=a;i++){
        int b;
        scanf("%d",&b);
//        cout<<mp[b]<<endl;
        if(mn==1) continue;
        if(mp[b]==0) mp[b]=i;
        else{
            int x=i-mp[b];
            mn=min(mn,x);
            mp[b]=i;
        }
    }
    printf("%d\n",mn);
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

int main(){
    int a;
    ll mn=1e10;
    scanf("%d",&a);
    map<ll,ll>mp;
//    memset(index,0,sizeof(index));
    for(int i=1;i<=a;i++){
        ll b;
        scanf("%lld",&b);
        if(mp[b]==0) mp[b]=i;
        else{
            mn=min(mn,abs(i-mp[b]));
            mp[b]=i;
        }
    }
    printf("%lld\n",mn);
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

int main(){
    int a;
    int mn=1e6;
    scanf("%d",&a);
    map<ll,ll>mp;
    for(int i=1;i<=a;i++){
        ll b;
        scanf("%lld",&b);
//        cout<<mp[b]<<endl;
        if(mp[b]==0) mp[b]=i;
        else{
            int x=abs(i-mp[b]);
            mn=min(mn,x);
            mp[b]=i;
        }
    }
    printf("%d\n",mn);
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

int main(){
    ll a;
    ll mn=1e9,ans,mn2=0;
    //cout<<mn<<endl;
    scanf("%lld",&a);
    map<ll,ll>mp;
    vector<int>vc;
    for(ll i=1;i<=a;i++){
        ll b;
        scanf("%lld",&b);
//        cout<<mp[b]<<endl;
        for(int j=0;j<vc.size();j++){

            ll x=abs(vc[j]-b);
            if(mn==x&&mn2>=vc[j]){
                ans=abs(i-j)-1;
                mn=min(mn,x);
                mn2=vc[j];
            }
            else if(mn>x){
                ans=abs(i-j)-1;
                mn=min(mn,x);
                mn2=vc[j];
          //  cout<<i<<" "<<j<<" "<<x<<endl;
            }
        }
        vc.push_back(b);
    }
    printf("%lld\n",ans);
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

int main(){
    int a;
    int mn=1e9;
    //cout<<mn<<endl;
    scanf("%d",&a);
    map<int,int>mp;
    for(int i=1;i<=a;i++){
        int b;
        scanf("%d",&b);
//        cout<<mp[b]<<endl;
        if(mp[b]==0) mp[b]=i;
        else{
            int x=abs(i-mp[b]);
            mn=min(mn,x);
            mp[b]=i;
        }
    }
    printf("%d\n",mn);
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

int main(){
    ll a;
    ll mn=1e9;
    //cout<<mn<<endl;
    scanf("%lld",&a);
    map<ll,ll>mp;
    for(ll i=1;i<=a;i++){
        ll b;
        scanf("%lld",&b);
//        cout<<mp[b]<<endl;
        if(mn==1) continue;
        if(mp[b]>0){
            ll x=i-mp[b];
            mn=min(mn,x);
            mp[b]=i;
        }
        else mp[b]=i;
    }
    printf("%lld\n",mn);
    return 0;
}


#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

int main(){
    ll a;
    ll mn=1e9,mn2=1e9;
    //cout<<mn<<endl;
    scanf("%lld",&a);
    map<ll,ll>mp;
    for(ll i=1;i<=a;i++){
        ll b;
        scanf("%lld",&b);
//        cout<<mp[b]<<endl;
        //if(mn==1) continue;
        if(mp[b]>0&&mn2==b){
            ll n=i-mp[b];
            mn=min(n,mn);
            mp[b]=i;
            mn2=min(mn2,b);
        }
        else if(mp[b]>0&&mn2>b){
            mn=i-mp[b];
            mp[b]=i;
            mn2=min(mn2,b);
        }
        else mp[b]=i;
    }
    printf("%lld\n",mn);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long a[200000];
int main() {
  int n,i=0,out=1; scanf("%d",&n); while(n>i)scanf("%lld",&a[i++]);
  sort(a,a+n); for(i=0;i<n;i++)if(a[i]==a[i+1])out++; else break;
  if(out==n) printf("1"); else
  printf("%d",out);
}


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

int a,l,mnDis=1e5+1; long long mnNum=1e9+1,num[100001]; bool bol=0;
int main(){scanf("%d",&a);for(int i=0;i<a;i++){scanf("%lld",num+i);
mnNum=min(mnNum,num[i]);}
for(int i=0;i<a;i++){if(num[i]==mnNum)
{if(bol)mnDis=min(mnDis,i-l);else l=i;bol=1;}}printf("%d",mnDis);}


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

int a,l,r,mnDis=1e5+1; long long mnNum=1e9+1,num[100001]; bool bol=0;
int main(){scanf("%d",&a);for(int i=0;i<a;i++){scanf("%lld",num+i);
mnNum=min(mnNum,num[i]);}
for(int i=0;i<a;i++){if(num[i]==mnNum)
{if(bol){r=i;mnDis=min(mnDis,abs(r-l)); bol=0;}else {l=i;bol=1;}}}printf("%d",min(mnDis,abs(l-r)));}


#include <bits/stdc++.h>
using namespace std;
long long a[200000];
int main() {
  int n,i=0,out=1; scanf("%d",&n); while(n>i)scanf("%lld",&a[i++]);
  sort(a,a+n); for(i=0;i<n;i++)if(a[i]==a[i+1])out++; else break;
  printf("%d",out);
}


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

int a,l,r,mnDis=1e5+1; long long mnNum=1e9+1,num[100001]; bool bol=0;
int main(){scanf("%d",&a);for(int i=0;i<a;i++){scanf("%lld",num+i);
mnNum=min(mnNum,num[i]);}
for(int i=0;i<a;i++){if(num[i]==mnNum)
{if(bol){r=i;mnDis=min(mnDis,r-l); bol=0;}else {l=i;bol=1;}}}printf("%d",min(mnDis,l-r));}


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

int a,l,r,mnDis=1e5+1; long long mnNum=1e9+1,num[100001]; bool bol=0;
int main(){scanf("%d",&a);for(int i=0;i<a;i++){scanf("%lld",num+i);
mnNum=min(mnNum,num[i]);}
for(int i=0;i<a;i++){if(num[i]==mnNum)
{if(bol){r=i;mnDis=min(mnDis,abs(r-l)); bol=0;}else {l=i;bol=1;}}}printf("%d",min(mnDis,l-r));}


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

int a,l,r,mnDis=1e5+1; long long mnNum=1e9+1,num[100001]; bool bol=0;
int main(){scanf("%d",&a);for(int i=0;i<a;i++){scanf("%lld",num+i);
mnNum=min(mnNum,num[i]);}
for(int i=0;i<a;i++){
  if(i!=a-1&&num[i]==num[i+1]){cout<<1<<endl; return 0;}
  if(num[i]==mnNum)
{if(bol){r=i;mnDis=min(mnDis,abs(r-l)); bol=0;}else {l=i;bol=1;}}}printf("%d",min(mnDis,abs(l-r)));}


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

int a,l,r,mnDis=1e5+1; long long mnNum=1e9+1,num[100001]; bool bol=0;
int main(){scanf("%d",&a);for(int i=0;i<a;i++){scanf("%lld",num+i);
mnNum=min(mnNum,num[i]);}
for(int i=0;i<a;i++){
  if(i!=a-1&&num[i]==num[i+1]&&num[i]==mnNum){cout<<1<<endl; return 0;}
  if(num[i]==mnNum)
{if(bol){r=i;mnDis=min(mnDis,abs(r-l)); bol=0;}else {l=i;bol=1;}}}printf("%d",min(mnDis,abs(l-r)));}


/*--.--.--.--.--.--.--.--.--.--.--.--.--*
 *  	By-Rohit Singh					 *
 *    CS , MNNIT Allahabad               *
 *		rohitmnnit1459@gmail.com         *
 *--.--.--.--.--.--.--.--.--.--.--.--.--*/

#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <set>
#include <limits.h>
#include <assert.h>
//#include <unordered_map>
using namespace std;

#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define null NULL
#define ll long long
#define llu unsigned long long
#define MAX 200005
#define mod 1000000007
#define inf 1e16
#define pp pair <int, int>

typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <ldb, ldb> pdd;
#define all(v) (v).begin(),(v).end()

const db PI = 3.141592653589793238;
#define abs(x) ((x)>0?(x):-(x))
//#define pop pop_back()
#define scan(x) scanf("%d",&x)
#define print(x) printf("%d\n",x)
#define scanll(x) scanf("%lld",&x)
#define printll(x) printf("%lld\n",x)
#define lld long double

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define PER(i,a,b) for(int i=b;i>=a;i--)
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
//**********************************************HAPPY CODDING*****************************************************//
int arr[MAX];

int main()  {

    int N, l, r, mi = 1e7;
    scan(N);
    rep(i,N)  {
        scan(arr[i]);
        mi = min(mi, arr[i]);
    }
    rep(i,N)  {
        if (arr[i] == mi)  {
            l = i;
            break;
        }
    }
    PER(i,0,N-1)  {
        if (arr[i] == mi)  {
            r = i;
            break;
        }
    }
    print(r-l);
	return 0;
}


/*--.--.--.--.--.--.--.--.--.--.--.--.--*
 *  	By-Rohit Singh					 *
 *    CS , MNNIT Allahabad               *
 *		rohitmnnit1459@gmail.com         *
 *--.--.--.--.--.--.--.--.--.--.--.--.--*/

#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <set>
#include <limits.h>
#include <assert.h>
//#include <unordered_map>
using namespace std;

#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define null NULL
#define ll long long
#define llu unsigned long long
#define MAX 200005
#define mod 1000000007
#define inf 1e16
#define pp pair <int, int>

typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <ldb, ldb> pdd;
#define all(v) (v).begin(),(v).end()

const db PI = 3.141592653589793238;
#define abs(x) ((x)>0?(x):-(x))
//#define pop pop_back()
#define scan(x) scanf("%d",&x)
#define print(x) printf("%d\n",x)
#define scanll(x) scanf("%lld",&x)
#define printll(x) printf("%lld\n",x)
#define lld long double

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define PER(i,a,b) for(int i=b;i>=a;i--)
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
//**********************************************HAPPY CODDING*****************************************************//
int arr[MAX], st[MAX];

int main()  {

    int N, mi = 1e9+5, ind = 0;
    scan(N);
    rep(i,N)  {
        scan(arr[i]);
        mi = min(mi, arr[i]);
    }
    rep(i,N)  {
        if (arr[i] == mi)  st[ind++] = i;
    }
    mi = 1e9+4;
    REP(i,1,ind-1)  {
        mi = min(st[i]-st[i-1], mi);
    }
    print(mi);
	return 0;
}


/*--.--.--.--.--.--.--.--.--.--.--.--.--*
 *  	By-Rohit Singh					 *
 *    CS , MNNIT Allahabad               *
 *		rohitmnnit1459@gmail.com         *
 *--.--.--.--.--.--.--.--.--.--.--.--.--*/

#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <set>
#include <limits.h>
#include <assert.h>
//#include <unordered_map>
using namespace std;

#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define null NULL
#define ll long long
#define llu unsigned long long
#define MAX 200005
#define mod 1000000007
#define inf 1e16
#define pp pair <int, int>

typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <ldb, ldb> pdd;
#define all(v) (v).begin(),(v).end()

const db PI = 3.141592653589793238;
#define abs(x) ((x)>0?(x):-(x))
//#define pop pop_back()
#define scan(x) scanf("%d",&x)
#define print(x) printf("%d\n",x)
#define scanll(x) scanf("%lld",&x)
#define printll(x) printf("%lld\n",x)
#define lld long double

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define PER(i,a,b) for(int i=b;i>=a;i--)
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
//**********************************************HAPPY CODDING*****************************************************//
int arr[MAX];

int main()  {

    int N, l, r, mi = 0;
    scan(N);
    rep(i,N)  {
        scan(arr[i]);
        mi = min(mi, arr[i]);
    }
    rep(i,N)  {
        if (arr[i] == mi)  {
            l = i;
            break;
        }
    }
    PER(i,0,N-1)  {
        if (arr[i] == mi)  {
            r = i;
            break;
        }
    }
    print(r-l);
	return 0;
}


/*--.--.--.--.--.--.--.--.--.--.--.--.--*
 *  	By-Rohit Singh					 *
 *    CS , MNNIT Allahabad               *
 *		rohitmnnit1459@gmail.com         *
 *--.--.--.--.--.--.--.--.--.--.--.--.--*/

#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <set>
#include <limits.h>
#include <assert.h>
//#include <unordered_map>
using namespace std;

#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define null NULL
#define ll long long
#define llu unsigned long long
#define MAX 200005
#define mod 1000000007
#define inf 1e16
#define pp pair <int, int>

typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <ldb, ldb> pdd;
#define all(v) (v).begin(),(v).end()

const db PI = 3.141592653589793238;
#define abs(x) ((x)>0?(x):-(x))
//#define pop pop_back()
#define scan(x) scanf("%d",&x)
#define print(x) printf("%d\n",x)
#define scanll(x) scanf("%lld",&x)
#define printll(x) printf("%lld\n",x)
#define lld long double

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define PER(i,a,b) for(int i=b;i>=a;i--)
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
//**********************************************HAPPY CODDING*****************************************************//
int arr[MAX];

int main()  {

    int N, l, r, mi = 1e7;
    l = r = -1;
    scan(N);
    rep(i,N)  {
        scan(arr[i]);
        mi = min(mi, arr[i]);
    }
    rep(i,N)  {
        if (l > -1 && arr[i] == mi)  {
            l = i;
        }
        else if (arr[i] == mi)  {
            r = i;
            break;
        }
    }

    print(r-l);
	return 0;
}


/*--.--.--.--.--.--.--.--.--.--.--.--.--*
 *  	By-Rohit Singh					 *
 *    CS , MNNIT Allahabad               *
 *		rohitmnnit1459@gmail.com         *
 *--.--.--.--.--.--.--.--.--.--.--.--.--*/

#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <set>
#include <limits.h>
#include <assert.h>
//#include <unordered_map>
using namespace std;

#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define null NULL
#define ll long long
#define llu unsigned long long
#define MAX 200005
#define mod 1000000007
#define inf 1e16
#define pp pair <int, int>

typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <ldb, ldb> pdd;
#define all(v) (v).begin(),(v).end()

const db PI = 3.141592653589793238;
#define abs(x) ((x)>0?(x):-(x))
//#define pop pop_back()
#define scan(x) scanf("%d",&x)
#define print(x) printf("%d\n",x)
#define scanll(x) scanf("%lld",&x)
#define printll(x) printf("%lld\n",x)
#define lld long double

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define PER(i,a,b) for(int i=b;i>=a;i--)
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
//**********************************************HAPPY CODDING*****************************************************//
int arr[MAX];

int main()  {

    int N, l, r, mi = 0;
    scan(N);
    rep(i,N)  {
        scan(arr[i]);
        mi = min(arr[i]);
    }
    rep(i,N)  {
        if (arr[i] == mi)  {
            l = i;
            break;
        }
    }
    PER(i,0,N-1)  {
        if (arr[i] == mi)  {
            r = i;
            break;
        }
    }
    print(r-l);
	return 0;
}


/*--.--.--.--.--.--.--.--.--.--.--.--.--*
 *  	By-Rohit Singh					 *
 *    CS , MNNIT Allahabad               *
 *		rohitmnnit1459@gmail.com         *
 *--.--.--.--.--.--.--.--.--.--.--.--.--*/

#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <set>
#include <limits.h>
#include <assert.h>
//#include <unordered_map>
using namespace std;

#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define null NULL
#define ll long long
#define llu unsigned long long
#define MAX 200005
#define mod 1000000007
#define inf 1e16
#define pp pair <int, int>

typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <ldb, ldb> pdd;
#define all(v) (v).begin(),(v).end()

const db PI = 3.141592653589793238;
#define abs(x) ((x)>0?(x):-(x))
//#define pop pop_back()
#define scan(x) scanf("%d",&x)
#define print(x) printf("%d\n",x)
#define scanll(x) scanf("%lld",&x)
#define printll(x) printf("%lld\n",x)
#define lld long double

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define PER(i,a,b) for(int i=b;i>=a;i--)
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
//**********************************************HAPPY CODDING*****************************************************//
int arr[MAX];

int main()  {

    int N, l, r, mi = 1e7;
    l = r = -1;
    scan(N);
    rep(i,N)  {
        scan(arr[i]);
        mi = min(mi, arr[i]);
    }
    rep(i,N)  {
        if (l == -1 && arr[i] == mi)  {
            l = i;
        }
        else if (arr[i] == mi)  {
            r = i;
            break;
        }
    }

    print(r-l);
	return 0;
}


/*--.--.--.--.--.--.--.--.--.--.--.--.--*
 *  	By-Rohit Singh					 *
 *    CS , MNNIT Allahabad               *
 *		rohitmnnit1459@gmail.com         *
 *--.--.--.--.--.--.--.--.--.--.--.--.--*/

#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <queue>
#include <stack>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <set>
#include <limits.h>
#include <assert.h>
//#include <unordered_map>
using namespace std;

#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define null NULL
#define ll long long
#define llu unsigned long long
#define MAX 200005
#define mod 1000000007
#define inf 1e16
#define pp pair <int, int>

typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <ldb, ldb> pdd;
#define all(v) (v).begin(),(v).end()

const db PI = 3.141592653589793238;
#define abs(x) ((x)>0?(x):-(x))
//#define pop pop_back()
#define scan(x) scanf("%d",&x)
#define print(x) printf("%d\n",x)
#define scanll(x) scanf("%lld",&x)
#define printll(x) printf("%lld\n",x)
#define lld long double

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define PER(i,a,b) for(int i=b;i>=a;i--)
ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
//**********************************************HAPPY CODDING*****************************************************//
int arr[MAX], st[MAX];

int main()  {

    int N, mi = 1e7, ind = 0;
    scan(N);
    rep(i,N)  {
        scan(arr[i]);
        mi = min(mi, arr[i]);
    }
    rep(i,N)  {
        if (arr[i] == mi)  st[ind++] = i;
    }
    mi = 1e7;
    REP(i,1,ind-1)  {
        mi = min(st[i]-st[i-1], mi);
    }
    print(mi);
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		long long int a[100200],min,d,t,i,s;
		for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
		min=a[0];
		t=0;
		for(i=1;i<n;i++)
		{
			if(a[i]<min)
			{
				min=a[i];
				t=i;
			}
		}
		for(i=t+1;i<n;i++)
		{
			if(a[i]==min)
			{
				d=i-t;
				t=i;
				break;
			}
		}
		for(i=t+1;i<n;i++)
		{
			if(a[i]==min)
			{
				s=i-t;
	    		if(s<d)
	    		d=s;
			}
		}
		printf("%lld\n",d);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		long long int a[100200];
		long long int i,j,z,t=999999,d=999999;
		for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
		for(i=0;i<n;i++)
		{
		for(j=i+1;j<n;j++)
		{
			if(a[i]==a[j])
			t=j-i;
			z=j-i;
			if(t<d)
			{
				d=t;
				break;
			}
			if(z>t)
			break;
		}
	    }
		printf("%lld\n",d);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		long long int a[100200],min,d,t,i,s;
		for(i=0;i<n;i++)
		scanf("%lld",&a[i]);
		min=a[0];
		t=0;
		for(i=1;i<n;i++)
		{
			if(a[i]<min)
			{
				min=a[i];
				t=i;
			}
		}
		for(i=t+1;i<n;i++)
		{
			if(a[i]==min)
			{
				d=i-t;
				t=i;
				break;
			}
		}
		for(i=t+1;i<n;i++)
		{
			if(a[i]==min)
			{
				s=i-t;
				t=i;
	    		if(s<d)
	    		d=s;
			}
		}
		printf("%lld\n",d);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		long long int a[100200];
		long long int i,j,z,t=999999,d=999999;
		for(i=0;i<n;i++)
		scanf("%d",&a[i]);
		for(i=0;i<n;i++)
		{
		for(j=i+1;j<n;j++)
		{
			if(a[i]==a[j])
			t=j-i;
			z=j-i;
			if(t<d)
			{
				d=t;
				break;
			}
			if(z>t)
			break;
		}
	    }
		printf("%lld\n",d);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		int a[100005];
		int i,j,z,t=999999,d=999999;
		for(i=0;i<n;i++)
		scanf("%d",&a[i]);
		for(i=0;i<n;i++)
		{
		for(j=i+1;j<n;j++)
		{
			if(a[i]==a[j])
			t=j-i;
			z=j-i;
			if(t<d)
			{
				d=t;
				break;
			}
			if(z>t)
			break;
		}
	    }
		printf("%d\n",d);
	}
	return 0;
}


#include<stdio.h>
#include<string.h>
int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		int a[100005];
		int i,j,t=999999,d=999999;
		for(i=0;i<n;i++)
		scanf("%d",&a[i]);
		for(i=0;i<n;i++)
		for(j=i+1;j<n;j++)
		{
			if(a[i]==a[j])
			t=j-i;
			if(t<d)
			{
				d=t;
				break;
			}
		}
		printf("%d\n",d);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,a[100005];
    int mi=INFINITY;
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {

        scanf("%d",&a[i]);
        if(a[i]<mi)mi=a[i];
    }
    int mipos1=-1,mipos2=-1,mil=n;
    for(int i=0;i<n;i++)
    {

        if(a[i]==mi)
        {
            if(mipos1==-1&&mipos2==-1)mipos1=i;
            else if(mipos2==-1)mipos2=i;
            else if(mipos2-mipos1<mil)
            {
                mipos1=mipos2;
                mipos2=i;
            }
        }
    }
    printf("%d\n",mipos2-mipos1);
}

/*
int main()
{
    int n;
    scanf("%d",&n);
    {
        long long mi=INFINITY,pos=0,cur,mipos;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&cur);
            if(cur<mi)
            {
                mipos=INFINITY;
                pos=i;
                mi=cur;

            }
            else if(cur==mi)
            {
                if(mipos>i-pos){mipos=i-pos;pos=i;}

            }
        }
        cout<<mipos<<endl;
    }
    return 0;
}
*/


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

int main()
{
    int n;
    while(scanf("%d",&n))
    {
        int mi=1000000009,pos=0,cur,mipos;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&cur);
            if(cur<mi)
            {
                mipos=1000000;
                pos=i;
                mi=cur;

            }
            else if(cur==mi)
            {
                if(mipos>i-pos){mipos=i-pos;pos=i;}
            }
        }
        cout<<mipos<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n,a[100005];
    int mi=INFINITY;
    scanf("%d",&n);
    int coun=0,mipa=-1;
    while(coun<2)
    {
        coun=0;
        for(int i=0;i<n;i++)
        {

        scanf("%d",&a[i]);
        if(a[i]<mi&&a[i]>mipa){mi=a[i];coun=0;}
        if(a[i]==mi)coun++;
        }
        mipa=mi;
    }

    int mipos1=-1,mipos2=-1,mil=n+5;
    int lapos=-1;
    for(int i=0;i<n;i++)
    {

        if(a[i]==mi)
        {
            if(lapos==-1)lapos=i;
            else if(mil>i-lapos){mil=i-lapos;}
            lapos=i;
        }
    }
    printf("%d\n",mil);
}

/*
int main()
{
    int n;
    scanf("%d",&n);
    {
        long long mi=INFINITY,pos=0,cur,mipos;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&cur);
            if(cur<mi)
            {
                mipos=INFINITY;
                pos=i;
                mi=cur;

            }
            else if(cur==mi)
            {
                if(mipos>i-pos){mipos=i-pos;pos=i;}

            }
        }
        cout<<mipos<<endl;
    }
    return 0;
}
*/


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

int main()
{
    int n;
    scanf("%d",&n);
    {
        int mi=1000000009,pos=0,cur,mipos;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&cur);
            if(cur<mi)
            {
                mipos=1000000;
                pos=i;
                mi=cur;

            }
            else if(cur==mi)
            {
                if(mipos>i-pos){mipos=i-pos;pos=i;}
            }
        }
        cout<<mipos<<endl;
    }
    return 0;
}


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

int main()
{
    int n;
    scanf("%d",&n);
    {
        long long mi=INFINITY,pos=0,cur,mipos;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld",&cur);
            if(cur<mi)
            {
                mipos=INFINITY;
                pos=i;
                mi=cur;

            }
            else if(cur==mi)
            {
                if(mipos>i-pos){mipos=i-pos;pos=i;}

            }
        }
        cout<<mipos<<endl;
    }
    return 0;
}


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

int main()
{
    int n;
    scanf("%d",&n);
    {
        int mi=INFINITY,pos=0,cur,mipos;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&cur);
            if(cur<mi)
            {
                mipos=INFINITY;
                pos=i;
                mi=cur;

            }
            else if(cur==mi)
            {
                if(mipos>i-pos){mipos=i-pos;pos=i;}
            }
        }
        cout<<mipos<<endl;
    }
    return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    int m=a[0],l,l2=1000000,li=0;
    for(int i=1;i<n;i++)
    {
        if(a[i]<m)
        {m=a[i];l=0;li=i;}
        else if(a[i]==m)
        {

           // cout<<l<<a[i]<<i<<li;
            l=i-li;
            if(l<l2)
            { l2=l;}
            li=i;
           // l=0;cout<<l2<<endl;
        }
    }

    cout<<l2;
    return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    int m=a[0],l=0,l2=0,li=0;vector<int>b;
    for(int i=1;i<n;i++)
    {
        if(a[i]<m)
        {m=a[i];l=1;l2=0,li=i;}
        else if(a[i]==m)
        {
            l=l+i-li;
            b.push_back(l);
            li=i;l=0;
        }
    }
    sort(b.begin(),b.end());
    cout<<b[0];
    return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    int m=a[0],l=0,l2=1000000,li=0;
    for(int i=1;i<n;i++)
    {
        if(a[i]<m)
        {m=a[i];l=0;l2=0,li=i;}
        else if(a[i]==m)
        {
            l=l+i-li;
            if(l<l2)
           l2=l;
            li=i;l=0;
        }
    }

    cout<<l2;
    return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    int m=a[0],l=0,l2,li=0;
    for(int i=1;i<n;i++)
    {
        if(a[i]<m)
        {m=a[i];l=0;l2=0,li=i;}
        else if(a[i]==m)
        {
            l=l+i-li;
           l2=l;
            li=i;l=0;
        }
    }

    cout<<l2;
    return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    int m=a[0],l,l2=1000000,li=0,la=a[0];
    for(int i=1;i<n;i++)
    {
        if(a[i]<m)
        {m=a[i];l=0;li=i;}
        else if(a[i]==m)
        {

         //  cout<<l<<a[i]<<i<<li;
            l=i-li;
            if(a[i]<la )
            { l2=l;la=a[i];}
            else if(a[i]==la)
            {
                if(l<l2)
                    l2=l;
                la=a[i];
            }
            li=i;
            l=0;//cout<<l2<<endl;
        }
    }

    cout<<l2;
    return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */  int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++)
        cin>>a[i];
    int m=a[0],l=1,l2=0,li=0;vector<int>b;
    for(int i=1;i<n;i++)
    {
        if(a[i]<m)
        {m=a[i];l=1;l2=0,li=i;}
        else if(a[i]==m)
        {
            l=l+i-li;
            b.push_back(l);
            li=i;l=1;
        }
    }
    sort(b.begin(),b.end());
    cout<<b[0]-1;
    return 0;
}


#include <stdio.h>

int main() {
    int n,i=1,nb=0,mindis=100000;
    long long a,min=1000000000;
    scanf("%d",&n);
    while(i<=n){
        scanf("%I64d",&a);
        nb++;
        if(a==min)
            {
            if (nb<mindis)
                mindis=nb;
                nb=0;
             }
        else if(a<min){
            min=a;
            nb=0;
            mindis=100000;
            }
        i++;

    }
    printf("%d",mindis);

    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    scanf("%d",&n);
    int min=100000;
    int longmin=100000;
    int count=1;
    int a;
    for(int i=0;i<n;i++){
        scanf("%d",&a);
        if(a==min){ if(count<longmin) longmin=count; count=1; }
        else if(a<min){ min=a; count=1; longmin=100000;}
        else count++;
    }
    printf("%d",longmin);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    scanf("%d",&n);
    int64_t min=1000000000;
    int longmin=100000;
    int count=1;
    int64_t a;
    for(int i=0;i<n;i++){
        scanf("%I64d",&a);
        if(a==min){ if(count<longmin) longmin=count; count=1; }
        else if(a<min){ min=a; count=1; longmin=1000000000;}
        else count++;
    }
    printf("%d",longmin);
    return 0;
}


#include <stdio.h>

int main() {
    int n,i=1,nb=0,mindis=100000;
    long long a,min=1000000000;
    scanf("%d",&n);
    while(i<=n){
        scanf("%I64d",&a);
        nb++;
        if(a==min)
            {
            if (nb<mindis)
                mindis=nb;
                nb=1;
             }
        else if(a<min){
            min=a;
            nb=0;
            mindis=100000;
            }
        i++;

    }
    printf("%d",mindis);

    return 0;
}


#include <stdio.h>

int main() {
    int n,i=1,nb=0,mindis=100000;
    long long a,min=1000000000;
    scanf("%d",&n);
    while(i<=n){
        scanf("%I64d",&a);
        nb++;
        if(a==min)
            {
            if (nb<mindis)
                mindis=nb;
                nb=0;
             }
        else if(a<min){
            min=a;
            nb=0;
            }
        i++;

    }
    printf("%d",mindis);

    return 0;
}


#include<bits/stdc++.h>
#include<cmath>

#define ll              long long int
#define ull             unsigned long long int
#define pii             pair < int, int>
#define pll             pair < ll, ll>
#define sc              scanf
#define scin(x)         sc("%d",&(x))
#define scln(x)         sc("%lld",&(x))
#define pf              printf
#define ms(a,b)         memset(a,b,sizeof(a))
#define pb(a)           push_back(a)
#define mp              make_pair
#define db              double
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define vi              vector< int >
#define vl              vector< ll >
#define vii             vector<vector< int > >
#define vll             vector<vector< ll > >
#define DBG             pf("HI\n")
#define MOD             1000000007
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define RUN_CASE(t,T)   for(__typeof(t) t=1;t<=T;t++)
#define CASE(t)         printf("Case %d: ",t)
#define CASEL(t)        printf("Case %d:\n",t)
#define intlimit        2147483648
#define longlimit       9223372036854775808
#define infinity        (1<<28)
#define gcd(a, b)       __gcd(a,b)
#define lcm(a, b)       ((a)*(b)/gcd(a,b))
#define mx              1234567899
#define PI              2*acos(0.0)

using namespace std;

///--------------Graph Moves--------------------------------------
///const int fx[] = {+1,-1,+0,+0};
///const int fy[] = {+0,+0,+1,-1};
///const int fx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's move
///const int fy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; ///king's Move
///const int fx[] = {-2,-2,-1,-1,+1,+1,+2,+2}; ///knight's move
///const int fx[] = {-1,+1,-2,+2,-2,+2,-1,+1}; ///knight's move
///---------------------------------------------------------------

int main()
{
    int n;
    scin(n);
    ll ar[n];
    ll p,nim = mx;
    for(int i=0;i<n;i++)
    {
        scln(ar[i]);
        if(ar[i]<nim)
            nim = ar[i];
    }
    ll  nps = mx, p1 = -1, p2 = -1;
    for(int i=0;i<n;i++)
    {
        if(ar[i]==nim)
        {
            if(p1 == -1)
                p1 = i;
            else
            {
                p2 = i;
                if(abs(p2-p1)<nps)
                {
                    nps = abs(p2-p1);
                }
                p1 = p2;
            }
        }
    }
    pf("%lld\n",nps);
    return 0;
}


#include<bits/stdc++.h>
#include<cmath>

#define ll              long long int
#define ull             unsigned long long int
#define pii             pair < int, int>
#define pll             pair < ll, ll>
#define sc              scanf
#define scin(x)         sc("%d",&(x))
#define scln(x)         sc("%lld",&(x))
#define pf              printf
#define ms(a,b)         memset(a,b,sizeof(a))
#define pb(a)           push_back(a)
#define mp              make_pair
#define db              double
#define EPS             10E-10
#define ff              first
#define ss              second
#define sqr(x)          (x)*(x)
#define vi              vector< int >
#define vl              vector< ll >
#define vii             vector<vector< int > >
#define vll             vector<vector< ll > >
#define DBG             pf("HI\n")
#define MOD             1000000007
#define CIN             ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define RUN_CASE(t,T)   for(__typeof(t) t=1;t<=T;t++)
#define CASE(t)         printf("Case %d: ",t)
#define CASEL(t)        printf("Case %d:\n",t)
#define intlimit        2147483648
#define longlimit       9223372036854775808
#define infinity        (1<<28)
#define gcd(a, b)       __gcd(a,b)
#define lcm(a, b)       ((a)*(b)/gcd(a,b))
#define mx              123456789
#define PI              2*acos(0.0)

using namespace std;

///--------------Graph Moves--------------------------------------
///const int fx[] = {+1,-1,+0,+0};
///const int fy[] = {+0,+0,+1,-1};
///const int fx[] = {+0,+0,+1,-1,-1,+1,-1,+1}; ///King's move
///const int fy[] = {-1,+1,+0,+0,+1,+1,-1,-1}; ///king's Move
///const int fx[] = {-2,-2,-1,-1,+1,+1,+2,+2}; ///knight's move
///const int fx[] = {-1,+1,-2,+2,-2,+2,-1,+1}; ///knight's move
///---------------------------------------------------------------

int main()
{
    int n;
    scin(n);
    ll ar[n];
    ll p,nim = longlimit-1;
    for(int i=0;i<n;i++)
    {
        scln(ar[i]);
        if(ar[i]<nim)
            nim = ar[i];
    }
    ll  nps = longlimit-1, p1 = -1, p2 = -1;
    for(int i=0;i<n;i++)
    {
        if(ar[i]==nim)
        {
            if(p1 == -1)
                p1 = i;
            else
            {
                p2 = i;
                if(abs(p2-p1)<nps)
                {
                    nps = abs(p2-p1);
                }
                p1 = p2;
            }
        }
    }
    pf("%lld\n",nps);
    return 0;
}


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

int main()
{
	string a;
	getline(cin, a);

	deque<int> b(a.size());

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

	bool big = false, back = false;
	int pos = 0;
	if(b.size() % 2 == 1)
		goto out;
	int c4 = b.size()/2;
	int c7 = c4;
	while(true)
	{
		back = false;

		if(pos == b.size())
			break;
		if(big == true)
		{
			if(c4 > 0)
				b[pos] = 4, c4--;
			else
				b[pos] = 7, c7--;
		}
		else
		{
			if(b[pos] <= 4 && c4 > 0)
			{
				if(b[pos] < 4) big = true;
				b[pos] = 4;
				c4--;
			}
			else
			{
				if(b[pos] > 7 || c7 == 0)
					back = true;
				else
				{
					if(b[pos] < 7)
						big = true;
					b[pos] = 7, c7--;
				}
			}
		}

		if(back)
		{
			big = false;
			while(!big)
			{
				pos--;
				if(pos < 0)
					goto out;
				if(b[pos] == 4)
					c4++;
				if(b[pos] == 7)
					c7++;

				if(b[pos] < 4 && c4 > 0)
				{
					b[pos] = 4;
					c4--;
					big = true;
				}
				else if(b[pos] < 7 && c7 > 0)
				{
					b[pos] = 7;
					c7--;
					big = true;
				}
			}
		}
		pos++;

	}

	for(int i = 0; i < b.size(); ++i)
		cout << b[i];
out:
	for(int i = 0; i < b.size()/2 + 1; ++i)
		cout << 4;
	for(int i = 0; i < b.size()/2 + 1; ++i)
		cout << 7;
	return 0;

	return 0;
}


#include<iostream>
#include<cstdio>
#include<deque>

using namespace std;

int main()
{
	string a;
	getline(cin, a);

	deque<int> b(a.size());

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

	bool big = false, back = false;
	int pos = 0;
	if(b.size() % 2 == 1)
		b.push_front(0), pos++;
	int c4 = b.size()/2;
	int c7 = c4;
	while(true)
	{
		back = false;

		//cout << pos << ' ' << c4 << ' ' << c7 << endl;
		//for(int i = 0; i < b.size(); ++i)
		//	cout << b[i];
		//cout << endl << endl;
		//cin.get();

		if(pos == b.size())
		{
			if(c4 == 0 && c7 == 0)
				break;
			else
				back = true;
		}
		else if(big == true)
		{
			if(c4 > 0)
				b[pos] = 4, c4--;
			else if(c7 > 0)
				b[pos] = 7, c7--;
		}
		else
		{
			if(b[pos] <= 4 && c4 > 0)
			{
				if(b[pos] < 4) big = true;
				b[pos] = 4;
				c4--;
			}
			else
			{
				if(b[pos] > 7 || (b[pos] <= 7 && c7 == 0))
					back = true;
				else
				{
					if(b[pos] < 7)
						big = true;
					b[pos] = 7, c7--;
				}
			}
		}

		if(back)
		{
			big = false;
			while(!big)
			{
				pos--;
				if(pos < 0)
				{
					pos = 0;
					b.push_front(0);
					b.push_front(0);
					c4++;
					c7++;
				}
				if(b[pos] == 4)
					c4++;
				if(b[pos] == 7)
					c7++;

				if(b[pos] < 4)
				{
					b[pos] = 4;
					c4--;
					big = true;
				}
				else if(b[pos] < 7)
				{
					b[pos] = 7;
					c7--;
					big = true;
				}
			}
		}
		pos++;

	}

	for(int i = 0; i < b.size(); ++i)
		cout << b[i];

	return 0;
}


#include<iostream>
#include<cstdio>
#include<deque>

using namespace std;

int main()
{
	string a;
	getline(cin, a);

	deque<int> b(a.size());

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

	bool big = false, back = false;
	int pos = 0;
	if(b.size() % 2 == 1)
	{
		for(int i = 0; i < a.size()/2 + 1; ++i)
			cout << 4;
		for(int i = 0; i < a.size()/2 + 1; ++i)
			cout << 7;
		return 0;
	}
	int c4 = b.size()/2;
	int c7 = c4;
	while(true)
	{
		back = false;

		//cout << pos << ' ' << c4 << ' ' << c7 << endl;
		//for(int i = 0; i < b.size(); ++i)
		//	cout << b[i];
		//cout << endl << endl;
		//cin.get();

		if(pos == b.size())
		{
			if(c4 == 0 && c7 == 0)
				break;
			else
				back = true;
		}
		else if(big == true)
		{
			if(c4 > 0)
				b[pos] = 4, c4--;
			else if(c7 > 0)
				b[pos] = 7, c7--;
		}
		else
		{
			if(b[pos] <= 4 && c4 > 0)
			{
				if(b[pos] < 4) big = true;
				b[pos] = 4;
				c4--;
			}
			else
			{
				if(b[pos] > 7 || (b[pos] <= 7 && c7 == 0))
					back = true;
				else
				{
					if(b[pos] < 7)
						big = true;
					b[pos] = 7, c7--;
				}
			}
		}

		if(back)
		{
			big = false;
			while(!big)
			{
				pos--;
				if(pos < 0)
				{
					pos = 0;
					b.push_front(0);
					b.push_front(0);
					c4++;
					c7++;
				}
				if(b[pos] == 4)
					c4++;
				if(b[pos] == 7)
					c7++;

				if(b[pos] < 4)
				{
					b[pos] = 4;
					c4--;
					big = true;
				}
				else if(b[pos] < 7)
				{
					b[pos] = 7;
					c7--;
					big = true;
				}
			}
		}
		pos++;

	}

	for(int i = 0; i < b.size(); ++i)
		cout << b[i];

	return 0;
}


//#pragma comment(linker, "/STACK:16777216")
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <sstream>
#include <queue>
#include <stack>
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define DOWN(i,a,b) for(int i = (a); i >= (b); i--)
#define FORV(i,a) for(typeof(a.begin()) i = a.begin(); i != a.end(); i++)
#define FR(i,a) for(int i = 0; i < (a); i++)
#define DR(i,a) for(int i = (a)-1; i >= 0; i--)
#define REP(i,a) for(int i = 0; i < (a); i++)
#define Rep(i,a) for(int i = 0; i < (a); i++)
#define For(i,a,b) for(int i = (a); i <= (b); i++)

#define sqr(x) ((x)*(x))
#define dout debug && cout
#define ll long long
#define sz size()
#define ull unsigned long long
#define pb push_back
#define oo 1000000009
/* DEBUGGING */
bool debug = false;
/* MAIN PROGRAM */

using namespace std;

/*************************TEMPLATE**********************************/
long long convertToNum(string s)
{
    long long val = 0; FR(i,s.size()) val = val * 10 + s[i] - '0';
    return val;
}
string convertToString(long long a) {
    string res = ""; if (!a) return "0";
    while (a) { res = (char)(a % 10 + 48) + res;  a /= 10; }
    return res;
}
long long GCD(long long x,long long y)  {
    if (!x) return y; if (!y) return x;
    if (x == y) return x; if (x < y) return GCD(x,y%x); else return GCD(x%y,y);
}
long long POW(long long x,long long y,long long Base){
    if (!y) return 1; long long u = POW(x,y/2,Base);
    u = (u * u) % Base;
    if (y & 1) return (u * x) % Base; else return u;
}

//newstate = (newstate-1) & oldstate
/**************************CODE HERE*****************************/

char s[100300];
char a[100300];
bool nhohon[100300];
int n;
int main() {
//    freopen("test.in","r",stdin);
//    freopen("test.out","w",stdout);
    gets(s);
    int n = strlen(s);
    if (n % 2 == 1) {
        n++;
        FOR(i,1,n/2) printf("4");
        FOR(i,1,n/2) printf("7");
        return 0;
    }

    FR(i,n/2) a[i] = '7';
    FOR(i,n/2,n-1) a[i] = '4';

    int lower = 0;
    FR(i,n)
    if (a[i] < s[i]) {
        lower = 1;
        break;
    } else if (a[i] > s[i]) {
        lower = 2;
        break;
    }

    if (!lower) {
        FR(i,n) cout << a[i];
        return 0;
    }

    if (lower == 1) {
        n += 2;
        FOR(i,1,n/2) printf("4");
        FOR(i,n/2+1,n) printf("7");
        return 0;
    }

    FR(i,n/2) a[i] = '4';
    FOR(i,n/2,n-1) a[i] = '7';

    int now = n/2;
    FR(i,n)
    if (a[i] > s[i]) {
        FR(j,n) cout << a[j];
        return 0;
    } else if (a[i] < s[i]) {
        swap(a[i],a[now]);
        now++;
        if (a[i] > s[i]) {
            FR(j,n) cout << a[j];
            return 0;
        } else if (a[i] < s[i]) {
            DOWN(j,i-1,0)
            if (a[j] == '4') {
                a[j] = '7';
                int sl4 = n / 2;
                int sl7 = n / 2;
                FOR(t,0,j)
                if (a[t] == '7') sl7--;
                else sl4--;
                FOR(t,0,j) cout << a[t];
                FR(t,sl4) cout << 4;
                FR(t,sl7) cout << 7;
                return 0;
            }
        }
    }
    FR(i,n) cout << a[i];
    return 0;
}


//#pragma comment(linker, "/STACK:16777216")
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <sstream>
#include <queue>
#include <stack>
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define DOWN(i,a,b) for(int i = (a); i >= (b); i--)
#define FORV(i,a) for(typeof(a.begin()) i = a.begin(); i != a.end(); i++)
#define FR(i,a) for(int i = 0; i < (a); i++)
#define DR(i,a) for(int i = (a)-1; i >= 0; i--)
#define REP(i,a) for(int i = 0; i < (a); i++)
#define Rep(i,a) for(int i = 0; i < (a); i++)
#define For(i,a,b) for(int i = (a); i <= (b); i++)

#define sqr(x) ((x)*(x))
#define dout debug && cout
#define ll long long
#define sz size()
#define ull unsigned long long
#define pb push_back
#define oo 1000000009
/* DEBUGGING */
bool debug = false;
/* MAIN PROGRAM */

using namespace std;

/*************************TEMPLATE**********************************/
long long convertToNum(string s)
{
    long long val = 0; FR(i,s.size()) val = val * 10 + s[i] - '0';
    return val;
}
string convertToString(long long a) {
    string res = ""; if (!a) return "0";
    while (a) { res = (char)(a % 10 + 48) + res;  a /= 10; }
    return res;
}
long long GCD(long long x,long long y)  {
    if (!x) return y; if (!y) return x;
    if (x == y) return x; if (x < y) return GCD(x,y%x); else return GCD(x%y,y);
}
long long POW(long long x,long long y,long long Base){
    if (!y) return 1; long long u = POW(x,y/2,Base);
    u = (u * u) % Base;
    if (y & 1) return (u * x) % Base; else return u;
}

//newstate = (newstate-1) & oldstate
/**************************CODE HERE*****************************/

char s[100300];
char a[100300];
bool nhohon[100300];
int n;
int main() {
//    freopen("test.in","r",stdin);
//    freopen("test.out","w",stdout);
    gets(s);
    n = strlen(s);
    if (n % 2 == 1) {
        n++;
        FOR(i,1,n/2) printf("4");
        FOR(i,1,n/2) printf("7");
        return 0;
    }

    FR(i,n/2) a[i] = '7';
    FOR(i,n/2,n-1) a[i] = '4';

    int lower = 0;
    FR(i,n)
    if (a[i] < s[i]) {
        lower = 1;
        break;
    } else if (a[i] > s[i]) {
        lower = 2;
        break;
    }

    if (!lower) {
        FR(i,n) cout << a[i];
        return 0;
    }

    if (lower == 1) {
        n += 2;
        FOR(i,1,n/2) printf("4");
        FOR(i,n/2+1,n) printf("7");
        return 0;
    }

    FR(i,n/2) a[i] = '4';
    FOR(i,n/2,n-1) a[i] = '7';

    int now = n/2;
    FR(i,n)
    if (a[i] > s[i]) {
        FR(j,n) cout << a[j];
        return 0;
    } else if (a[i] < s[i]) {
        if (now < n) {
            swap(a[i],a[now]);
            now++;
        }
        if (a[i] > s[i]) {
            FR(j,n) cout << a[j];
            return 0;
        } else if (a[i] < s[i]) {
            DOWN(j,i,0)
            if (a[j] == '4') {
                a[j] = '7';
                int sl4 = n / 2;
                int sl7 = n / 2;
                FOR(t,0,j)
                if (a[t] == '7') sl7--;
                else sl4--;
                FOR(t,0,j) cout << a[t];
                FR(t,sl4) cout << 4;
                FR(t,sl7) cout << 7;
                return 0;
            }
        }
    }
    FR(i,n) cout << a[i];
    return 0;
}


//#pragma comment(linker, "/STACK:16777216")
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <sstream>
#include <queue>
#include <stack>
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define DOWN(i,a,b) for(int i = (a); i >= (b); i--)
#define FORV(i,a) for(typeof(a.begin()) i = a.begin(); i != a.end(); i++)
#define FR(i,a) for(int i = 0; i < (a); i++)
#define DR(i,a) for(int i = (a)-1; i >= 0; i--)
#define REP(i,a) for(int i = 0; i < (a); i++)
#define Rep(i,a) for(int i = 0; i < (a); i++)
#define For(i,a,b) for(int i = (a); i <= (b); i++)

#define sqr(x) ((x)*(x))
#define dout debug && cout
#define ll long long
#define sz size()
#define ull unsigned long long
#define pb push_back
#define oo 1000000009
/* DEBUGGING */
bool debug = false;
/* MAIN PROGRAM */

using namespace std;

/*************************TEMPLATE**********************************/
long long convertToNum(string s)
{
    long long val = 0; FR(i,s.size()) val = val * 10 + s[i] - '0';
    return val;
}
string convertToString(long long a) {
    string res = ""; if (!a) return "0";
    while (a) { res = (char)(a % 10 + 48) + res;  a /= 10; }
    return res;
}
long long GCD(long long x,long long y)  {
    if (!x) return y; if (!y) return x;
    if (x == y) return x; if (x < y) return GCD(x,y%x); else return GCD(x%y,y);
}
long long POW(long long x,long long y,long long Base){
    if (!y) return 1; long long u = POW(x,y/2,Base);
    u = (u * u) % Base;
    if (y & 1) return (u * x) % Base; else return u;
}

//newstate = (newstate-1) & oldstate
/**************************CODE HERE*****************************/

char s[100300];
char a[100300];
int n;
int main() {
//    freopen("test.in","r",stdin);
//    freopen("test.out","w",stdout);
    gets(s);
    int n = strlen(s);
    if (n % 2 == 1) {
        n++;
        FOR(i,1,n/2) printf("4");
        FOR(i,1,n/2) printf("7");
        return 0;
    }

    FR(i,n/2) a[i] = '7';
    FOR(i,n/2,n-1) a[i] = '4';

    int lower = 0;
    FR(i,n)
    if (a[i] < s[i]) {
        lower = 1;
        break;
    } else if (a[i] > s[i]) {
        lower = 2;
        break;
    }

    if (!lower) {
        FR(i,n) cout << a[i];
        return 0;
    }

    if (lower == 1) {
        n += 2;
        FOR(i,1,n/2) printf("4");
        FOR(i,n/2+1,n) printf("7");
        return 0;
    }

    FR(i,n/2) a[i] = '4';
    FOR(i,n/2,n-1) a[i] = '7';

    int now = n/2;
    FR(i,n)
    if (a[i] > s[i]) {
        FR(j,n) cout << a[j];
        return 0;
    } else if (a[i] < s[i]) {
        swap(a[i],a[now]);
        now++;
        if (a[i] > s[i]) {
            FR(j,n) cout << a[j];
            return 0;
        }
    }
    FR(i,n) cout << a[i];
    return 0;
}


//#pragma comment(linker, "/STACK:16777216")
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <sstream>
#include <queue>
#include <stack>
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define DOWN(i,a,b) for(int i = (a); i >= (b); i--)
#define FORV(i,a) for(typeof(a.begin()) i = a.begin(); i != a.end(); i++)
#define FR(i,a) for(int i = 0; i < (a); i++)
#define DR(i,a) for(int i = (a)-1; i >= 0; i--)
#define REP(i,a) for(int i = 0; i < (a); i++)
#define Rep(i,a) for(int i = 0; i < (a); i++)
#define For(i,a,b) for(int i = (a); i <= (b); i++)

#define sqr(x) ((x)*(x))
#define dout debug && cout
#define ll long long
#define sz size()
#define ull unsigned long long
#define pb push_back
#define oo 1000000009
/* DEBUGGING */
bool debug = false;
/* MAIN PROGRAM */

using namespace std;

/*************************TEMPLATE**********************************/
long long convertToNum(string s)
{
    long long val = 0; FR(i,s.size()) val = val * 10 + s[i] - '0';
    return val;
}
string convertToString(long long a) {
    string res = ""; if (!a) return "0";
    while (a) { res = (char)(a % 10 + 48) + res;  a /= 10; }
    return res;
}
long long GCD(long long x,long long y)  {
    if (!x) return y; if (!y) return x;
    if (x == y) return x; if (x < y) return GCD(x,y%x); else return GCD(x%y,y);
}
long long POW(long long x,long long y,long long Base){
    if (!y) return 1; long long u = POW(x,y/2,Base);
    u = (u * u) % Base;
    if (y & 1) return (u * x) % Base; else return u;
}

//newstate = (newstate-1) & oldstate
/**************************CODE HERE*****************************/

char s[100300];
char a[100300];
bool nhohon[100300];
int n;
int main() {
//    freopen("test.in","r",stdin);
//    freopen("test.out","w",stdout);
    gets(s);
    n = strlen(s);
    if (n % 2 == 1) {
        n++;
        FOR(i,1,n/2) printf("4");
        FOR(i,1,n/2) printf("7");
        return 0;
    }

    FR(i,n/2) a[i] = '7';
    FOR(i,n/2,n-1) a[i] = '4';

    int lower = 0;
    FR(i,n)
    if (a[i] < s[i]) {
        lower = 1;
        break;
    } else if (a[i] > s[i]) {
        lower = 2;
        break;
    }

    if (!lower) {
        FR(i,n) cout << a[i];
        return 0;
    }

    if (lower == 1) {
        n += 2;
        FOR(i,1,n/2) printf("4");
        FOR(i,n/2+1,n) printf("7");
        return 0;
    }

    FR(i,n/2) a[i] = '4';
    FOR(i,n/2,n-1) a[i] = '7';

    int now = n/2;
    int n4 = n / 2, n7 = n / 2;
    FR(i,n) {
        if (a[i] > s[i]) {
            FR(j,n) cout << a[j];
            return 0;
        } else if (a[i] < s[i]) {
            if (now < n) {
                swap(a[i],a[now]);
                now++;
            }
            if (a[i] > s[i]) {
                FR(j,n) cout << a[j];
                return 0;
            } else if (a[i] < s[i]) {
                DOWN(j,i,0) {
                    if (a[j] == '4' && n7 > 0) {
                        a[j] = '7';
                        int sl4 = n / 2;
                        int sl7 = n / 2;
                        FOR(t,0,j)
                        if (a[t] == '7') sl7--;
                        else sl4--;
                        FOR(t,0,j) cout << a[t];
                        FR(t,sl4) cout << 4;
                        FR(t,sl7) cout << 7;
                        return 0;
                    }
                    if (a[j] == '4') n4++;
                    else n7++;
                }
            }
        }
        if (a[i] == '4') n4--;
        else n7--;
    }
    FR(i,n) cout << a[i];
    return 0;
}


//#pragma comment(linker, "/STACK:16777216")
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <sstream>
#include <queue>
#include <stack>
#define FOR(i,a,b) for(int i = (a); i <= (b); i++)
#define DOWN(i,a,b) for(int i = (a); i >= (b); i--)
#define FORV(i,a) for(typeof(a.begin()) i = a.begin(); i != a.end(); i++)
#define FR(i,a) for(int i = 0; i < (a); i++)
#define DR(i,a) for(int i = (a)-1; i >= 0; i--)
#define REP(i,a) for(int i = 0; i < (a); i++)
#define Rep(i,a) for(int i = 0; i < (a); i++)
#define For(i,a,b) for(int i = (a); i <= (b); i++)

#define sqr(x) ((x)*(x))
#define dout debug && cout
#define ll long long
#define sz size()
#define ull unsigned long long
#define pb push_back
#define oo 1000000009
/* DEBUGGING */
bool debug = false;
/* MAIN PROGRAM */

using namespace std;

/*************************TEMPLATE**********************************/
long long convertToNum(string s)
{
    long long val = 0; FR(i,s.size()) val = val * 10 + s[i] - '0';
    return val;
}
string convertToString(long long a) {
    string res = ""; if (!a) return "0";
    while (a) { res = (char)(a % 10 + 48) + res;  a /= 10; }
    return res;
}
long long GCD(long long x,long long y)  {
    if (!x) return y; if (!y) return x;
    if (x == y) return x; if (x < y) return GCD(x,y%x); else return GCD(x%y,y);
}
long long POW(long long x,long long y,long long Base){
    if (!y) return 1; long long u = POW(x,y/2,Base);
    u = (u * u) % Base;
    if (y & 1) return (u * x) % Base; else return u;
}

//newstate = (newstate-1) & oldstate
/**************************CODE HERE*****************************/

char s[100300];
char a[100300];
bool nhohon[100300];
int n;
int main() {
//    freopen("test.in","r",stdin);
//    freopen("test.out","w",stdout);
    gets(s);
    int n = strlen(s);
    if (n % 2 == 1) {
        n++;
        FOR(i,1,n/2) printf("4");
        FOR(i,1,n/2) printf("7");
        return 0;
    }

    FR(i,n/2) a[i] = '7';
    FOR(i,n/2,n-1) a[i] = '4';

    int lower = 0;
    FR(i,n)
    if (a[i] < s[i]) {
        lower = 1;
        break;
    } else if (a[i] > s[i]) {
        lower = 2;
        break;
    }

    if (!lower) {
        FR(i,n) cout << a[i];
        return 0;
    }

    if (lower == 1) {
        n += 2;
        FOR(i,1,n/2) printf("4");
        FOR(i,n/2+1,n) printf("7");
        return 0;
    }

    FR(i,n/2) a[i] = '4';
    FOR(i,n/2,n-1) a[i] = '7';

    int now = n/2;
    FR(i,n)
    if (a[i] > s[i]) {
        FR(j,n) cout << a[j];
        return 0;
    } else if (a[i] < s[i]) {
        if (now < n) {
            swap(a[i],a[now]);
            now++;
        }
        if (a[i] > s[i]) {
            FR(j,n) cout << a[j];
            return 0;
        } else if (a[i] < s[i]) {
            DOWN(j,i-1,0)
            if (a[j] == '4') {
                a[j] = '7';
                int sl4 = n / 2;
                int sl7 = n / 2;
                FOR(t,0,j)
                if (a[t] == '7') sl7--;
                else sl4--;
                FOR(t,0,j) cout << a[t];
                FR(t,sl4) cout << 4;
                FR(t,sl7) cout << 7;
                return 0;
            }
        }
    }
    FR(i,n) cout << a[i];
    return 0;
}


/*
 * in the name of god
 *
 *
 *
 *
 *
 *
 *
 *
 */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <cmath>

using namespace std;

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

int len;
string s;

int main(){

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

	if (len%2==1){
		len++;
		for (int i=1; i<=len/2; i++)
			cout << '4';
		for (int i=1; i<=len/2; i++)
			cout << '7';
		cout << endl;
		return 0;
	}

	bool lucky = true , cnt40=0, cnt70=0;

	for (int i=0;i<(int)s.size(); i++){

		if (s[i]!='4' && s[i]!='7'){
			lucky = false;
			break;
		}

		else if (s[i]=='4')
			cnt40++;
		else
			cnt70++;
	}

	if (lucky && cnt40==cnt70){
		cout << s << endl;
		return 0;
	}

	bool flag = false , bad = false;

	for (int i=1; i<=len/2; i++){

		if (s[i-1]>'7') { flag = true; break; }

		else if (s[i-1] < '7') { bad = true; break; }
	}

	if (!flag && !bad){

		for (int i=len/2; i<(int)s.size(); i++){

			if (s[i] > '4') { flag=true; break; }

			else if (s[i] < '4') { bad=true; break; }
		}
	}

	if (flag){

		len+=2;

		for (int i=1; i<=len/2; i++)
			cout << '4';
		for (int i=1; i<=len/2; i++)
			cout << '7';
		cout << endl;
		return 0;
	}

	bool equal = true;
	int  cnt4  = len/2  , cnt7 = len/2;

	int poi=-1;
	for (int i=0; i<(int)s.size(); i++) if (s[i]>'7') { poi = i; }

	string res=  "";

	if (poi!=-1){

		for (int i=0; i<(int)poi; i++){

			if (equal){

				if (s[i]<='4' && cnt4) { res+= '4'; cnt4--; equal = (s[i]=='4'); }

				else { res+= '7'; cnt7--; equal = (s[i]=='7'); }
			}

			else{

				if (cnt4) { res+= '4'; cnt4--; }

				else { res+= '7'; cnt7--; }
			}
		}

		if (equal){

			int tmp=-1;
			for (int i=poi-1; i>0; i--) if (s[i]=='7' && s[i-1]=='4'){
				tmp = i-1;
				break;
			}

			if (tmp==-1){
				res[poi-1]='7';
				cnt4++;
				cnt7--;
			}

			else{
				swap (res[tmp] , res[tmp+1]);
				for (int i=tmp+1; i<poi; i++)
					if (res[i]=='4')
						cnt4++;
					else
						cnt7++;
				poi = tmp+1;
			}
		}

		equal = false;
	}

	else
		poi++;

	for (int i=poi; i<(int)s.size(); i++){

		if (equal){

			if (s[i]<='4' && cnt4) { res+= '4'; cnt4--; equal = (s[i]=='4'); }

			else { res+= '7'; cnt7--; equal = (s[i]=='7'); }
		}

		else{

			if (cnt4) { res+= '4'; cnt4--; }

			else { res+= '7'; cnt7--; }
		}
	}

	cout << res << endl;

	return 0;
}


/*
 * in the name of god
 *
 *
 *
 *
 *
 *
 *
 *
 */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <cmath>

using namespace std;

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

int len;
string s;

int main(){

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

	if (len%2==1) { cout << string(len/2+1,'4') << string(len/2+1,'7') << endl; return 0; }

	bool lucky = true;
	int cnt40=0, cnt70=0;

	for (int i=0; i<(int)s.size(); i++){

		if (s[i]!='4' && s[i]!='7'){
			lucky = false;
			break;
		}

		else if (s[i]=='4')
			cnt40++;
		else
			cnt70++;
	}

	if (lucky && cnt40==cnt70) { cout << s << endl; return 0; }

	bool flag = false , bad = false;

	for (int i=1; i<=len/2; i++){

		if (s[i-1]>'7') { flag = true; break; }

		else if (s[i-1] < '7') { bad = true; break; }
	}

	if (!flag && !bad){

		for (int i=len/2; i<(int)s.size(); i++){

			if (s[i] > '4') { flag=true; break; }

			else if (s[i] < '4') { bad=true; break; }
		}
	}

	if (flag) { cout << string(len/2 + 1,'4') << string(len/2 + 1,'7') << endl; return 0; }

	int eq=0 , no=-1;
	int eq4=len/2 , eq7=len/2;

	for (int i=0; i<(int)s.size(); i++){

		if (eq==-1)
			break;

		if (s[i]<'4'){
			no=i;
			eq=-1;
		}

		else if (s[i]=='4'){

			if (eq7>0) { no = i; }

			if (eq4>0) { eq4--; }

			else { eq=-1; }
		}

		else if (s[i]<'7'){

			if (eq7>0) { no = i; }

			eq=-1;
		}

		else if (s[i]=='7'){

			if (eq7>0) { eq7--; }

			else { eq=-1; }
		}

		else if (s[i]>'7') { eq=-1; }
	}

	int res4=len/2 , res7=len/2;

	for (int i=0; i<(int)s.size(); i++){

		if (i<no) { cout << s[i]; (s[i]=='4') ? res4-- : res7--; }

		else if (i==no){

			if (s[i]<'4' && res4) { cout << '4'; res4--; }

			else { res7--; cout << '7'; }
		}

		else{
			if (res4) { cout << '4'; res4--; }
			else { cout << '7'; res7--; }
		}
	}

	cout << endl;

	return 0;
}


/*
 * in the name of god
 *
 *
 *
 *
 *
 *
 *
 *
 */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <cmath>

using namespace std;

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

int len;
string s;

int main(){

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

        if (len%2==1){
                len++;
                for (int i=1; i<=len/2; i++)
                        cout << '4';
                for (int i=1; i<=len/2; i++)
                        cout << '7';
                cout << endl;
                return 0;
        }

        bool lucky = true;
        int  cnt40=0, cnt70=0;

        for (int i=0;i<(int)s.size(); i++){

                if (s[i]!='4' && s[i]!='7'){
                        lucky = false;
                        break;
                }

                else if (s[i]=='4')
                        cnt40++;
                else
                        cnt70++;
        }

        if (lucky && cnt40==cnt70){
                cout << s << endl;
                return 0;
        }

        bool flag = false , bad = false;

        for (int i=1; i<=len/2; i++){

                if (s[i-1]>'7') { flag = true; break; }

                else if (s[i-1] < '7') { bad = true; break; }
        }

        if (!flag && !bad){

                for (int i=len/2; i<(int)s.size(); i++){

                        if (s[i] > '4') { flag=true; break; }

                        else if (s[i] < '4') { bad=true; break; }
                }
        }

        if (flag){

                len+=2;

                for (int i=1; i<=len/2; i++)
                        cout << '4';
                for (int i=1; i<=len/2; i++)
                        cout << '7';
                cout << endl;
                return 0;
        }

        bool equal = true;
        int  cnt4  = len/2  , cnt7 = len/2;

        int poi=-1;
        for (int i=0; i<(int)s.size(); i++) if (s[i]>'7') { poi = i; break;}

        string res=  "";

        if (poi!=-1){

                for (int i=0; i<(int)poi; i++){

                        if (equal){

                                if (s[i]<='4' && cnt4) { res+= '4'; cnt4--; equal = (s[i]=='4'); }

                                else { res+= '7'; cnt7--; equal = (s[i]=='7'); }
                        }

                        else{

                                if (cnt4) { res+= '4'; cnt4--; }

                                else { res+= '7'; cnt7--; }
                        }
                }

                if (equal){

                        int tmp=-1;
                        for (int i=poi-1; i>0; i--) if (s[i]=='7' && s[i-1]=='4'){
                                tmp = i-1;
                                break;
                        }

                        if (tmp==-1){
                                res[poi-1]='7';
                                cnt4++;
                                cnt7--;
                        }

                        else{
                                swap (res[tmp] , res[tmp+1]);
                                for (int i=tmp+1; i<poi; i++)
                                        if (res[i]=='4')
                                                cnt4++;
                                        else
                                                cnt7++;
                                poi = tmp+1;
                        }
                }

                equal = false;
        }

        else
                poi++;

        for (int i=poi; i<(int)s.size(); i++){

                if (equal){

                        if (s[i]<='4' && cnt4) { res+= '4'; cnt4--; equal = (s[i]=='4'); }

                        else { res+= '7'; cnt7--; equal = (s[i]=='7'); }
                }

                else{

                        if (cnt4) { res+= '4'; cnt4--; }

                        else { res+= '7'; cnt7--; }
                }
        }

        cout << res << endl;

        return 0;
}


/*
 * in the name of god
 *
 *
 *
 *
 *
 *
 *
 *
 */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <cmath>

using namespace std;

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

int len;
string s;

int main(){

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

	if (len%2==1){
		len++;
		for (int i=1; i<=len/2; i++)
			cout << '4';
		for (int i=1; i<=len/2; i++)
			cout << '7';
		cout << endl;
		return 0;
	}

	bool lucky = true , cnt40=0, cnt70=0;

	for (int i=0;i<(int)s.size(); i++){

		if (s[i]!='4' && s[i]!='7'){
			lucky = false;
			break;
		}

		else if (s[i]=='4')
			cnt40++;
		else
			cnt70++;
	}

	if (lucky && cnt40==cnt70){
		cout << s << endl;
		return 0;
	}

	bool flag = false , bad = false;

	for (int i=1; i<=len/2; i++){

		if (s[i-1]>'7') { flag = true; break; }

		else if (s[i-1] < '7') { bad = true; break; }
	}

	if (!flag && !bad){

		for (int i=len/2; i<(int)s.size(); i++){

			if (s[i] > '4') { flag=true; break; }

			else if (s[i] < '4') { bad=true; break; }
		}
	}

	if (flag){

		len+=2;

		for (int i=1; i<=len/2; i++)
			cout << '4';
		for (int i=1; i<=len/2; i++)
			cout << '7';
		cout << endl;
		return 0;
	}

	bool equal = true;
	int  cnt4  = len/2  , cnt7 = len/2;

	int poi=-1;
	for (int i=0; i<(int)s.size(); i++) if (s[i]>'7') { poi = i; break;}

	string res=  "";

	if (poi!=-1){

		for (int i=0; i<(int)poi; i++){

			if (equal){

				if (s[i]<='4' && cnt4) { res+= '4'; cnt4--; equal = (s[i]=='4'); }

				else { res+= '7'; cnt7--; equal = (s[i]=='7'); }
			}

			else{

				if (cnt4) { res+= '4'; cnt4--; }

				else { res+= '7'; cnt7--; }
			}
		}

		if (equal){

			int tmp=-1;
			for (int i=poi-1; i>0; i--) if (s[i]=='7' && s[i-1]=='4'){
				tmp = i-1;
				break;
			}

			if (tmp==-1){
				res[poi-1]='7';
				cnt4++;
				cnt7--;
			}

			else{
				swap (res[tmp] , res[tmp+1]);
				for (int i=tmp+1; i<poi; i++)
					if (res[i]=='4')
						cnt4++;
					else
						cnt7++;
				poi = tmp+1;
			}
		}

		equal = false;
	}

	else
		poi++;

	for (int i=poi; i<(int)s.size(); i++){

		if (equal){

			if (s[i]<='4' && cnt4) { res+= '4'; cnt4--; equal = (s[i]=='4'); }

			else { res+= '7'; cnt7--; equal = (s[i]=='7'); }
		}

		else{

			if (cnt4) { res+= '4'; cnt4--; }

			else { res+= '7'; cnt7--; }
		}
	}

	cout << res << endl;

	return 0;
}


/*
 * in the name of god
 *
 *
 *
 *
 *
 *
 *
 *
 */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <cmath>

using namespace std;

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

int len;
string s;

int main(){

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

	if (len%2==1) { cout << string(len/2+1,'4') << string(len/2+1,'7') << endl; return 0; }

	bool lucky = true , cnt40=0, cnt70=0;

	for (int i=0;i<(int)s.size(); i++){

		if (s[i]!='4' && s[i]!='7'){
			lucky = false;
			break;
		}

		else if (s[i]=='4')
			cnt40++;
		else
			cnt70++;
	}

	if (lucky && cnt40==cnt70) { cout << s << endl; return 0; }

	bool flag = false , bad = false;

	for (int i=1; i<=len/2; i++){

		if (s[i-1]>'7') { flag = true; break; }

		else if (s[i-1] < '7') { bad = true; break; }
	}

	if (!flag && !bad){

		for (int i=len/2; i<(int)s.size(); i++){

			if (s[i] > '4') { flag=true; break; }

			else if (s[i] < '4') { bad=true; break; }
		}
	}

	if (flag) { cout << string(len/2 + 1,'4') << string(len/2 + 1,'7') << endl; return 0; }

	int eq=0 , no=-1;
	int eq4=len/2 , eq7=len/2;

	for (int i=0; i<(int)s.size(); i++){

		if (eq==-1)
			break;

		if (s[i]<'4'){
			no=i;
			eq=-1;
		}

		else if (s[i]=='4'){

			if (eq7>0) { no = i; }

			if (eq4>0) { eq4--; }

			else { eq=-1; }
		}

		else if (s[i]<'7'){

			if (eq7>0) { no = i; }

			eq=-1;
		}

		else if (s[i]=='7'){

			if (eq7>0) { eq7--; }

			else { eq=-1; }
		}

		else if (s[i]>'7') { eq=-1; }
	}

	int res4=len/2 , res7=len/2;

	for (int i=0; i<(int)s.size(); i++){

		if (i<no) { cout << s[i]; (s[i]=='4') ? res4-- : res7--; }

		else if (i==no){

			if (s[i]<'4' && res4) { cout << '4'; res4--; }

			else { res7--; cout << '7'; }
		}

		else{
			if (res4) { cout << '4'; res4--; }
			else { cout << '7'; res7--; }
		}
	}

	cout << endl;

	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;
                    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;
}
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') {
                            --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) {
                        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;
                    sc++;
                    printf("%s",ans);
                    while(sc++<len/2) {
                        putchar('7');
                    }puts("");
                    out=true;
                    break;
                }
                if(sc>len/2) {
                    cnt--;
                    sc--;
                    while(ans[cnt-1]=='7') {
                        --cnt;
                        sc--;
                        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) ;
    for (int i = 0; i < len/ 2; i++) {
        if (str[i] != '7')
            return false;
    }
    for(int i=len/2;i<len;i++){
        if(str[i]>'4') return true;
    }
    return false;
}
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-1]=='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-1]=='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) {
                    //printf("len:%d,cnt:%d,sc:%d,fc:%d\n",len,cnt,sc,fc);
                    //puts(str+cnt);
                    while(ans[cnt-1]=='7'||sc>=len/2) {
                        //printf("cnt:%d,sc:%d,len:%d\n",cnt,sc,len);
                        if(ans[cnt-1]=='7') {
                            sc--;
                        } else fc--;

                        --cnt;
                        if(cnt==0) break;
                    }
                    //puts(str+cnt);
                    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;
}
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-1]=='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-1]=='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) {
                    //printf("len:%d,cnt:%d,sc:%d,fc:%d\n",len,cnt,sc,fc);
                    //puts(str+cnt);
                    while(ans[cnt-1]=='7'||sc>=len/2) {
                        //printf("cnt:%d,sc:%d,len:%d\n",cnt,sc,len);
                        if(ans[cnt-1]=='7') {
                            sc--;
                        } else fc--;

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