#include #include #include #include #include #define maxn 1000007 #define modp 1000000007LL typedef long long LL; typedef unsigned long long ULL; int n,m,T,tot; using namespace std; LL qpow(int x, int y) { if (y<=0) return 1; LL res=qpow(x,y>>1); res*=res; if (y&1) res*=x; return res; } LL exp_mod(LL a, LL b, LL p) { LL tmp = a % p, ans =1; while(b) { if(b & 1) ans = ans * tmp % p; tmp = tmp*tmp % p; b >>=1; } return ans; } LL CC(LL n,LL m) { LL res=1; if (n-mn-m;--i) { res*=i; res/=n-i+1; } return res%modp; } int check(LL x) { int s=0,t=0; while (x) { int u=x%10; x/=10; s+=u==4; t+=u==7; } return s==t; } LL dfs(int x, int y, LL t,ULL b, LL s, int f) { if (x==0&&y==0) { if (s>=t) return s; else return 0; } LL res, tmp; if (x>0) { if (f) res=dfs(x-1,y,t,b/10,s+b*4,f); else if (t/b%10==4) res=dfs(x-1,y,t,b/10,s+b*4,0); else if (t/b%10<4) res=dfs(x-1,y,t,b/10,s+b*4,1); else res=0; } else res=0; if (y>0) { if (f) { if (x==0) tmp=dfs(x,y-1,t,b/10,s+b*7,f); else tmp=0; } else if (t/b%10==7) tmp=dfs(x,y-1,t,b/10,s+b*7,0); else if (t/b%10<7) tmp=dfs(x,y-1,t,b/10,s+b*7,1); else tmp=0; } else tmp=0; if (res==0||tmp&&tmp777777777444444444LL) { printf("44444444447777777777\n"); continue; } k=n; tot=0; b=1; while (k){ k/=10; ++tot; b*=10; } if (tot&1) ans=dfs((tot+1)/2,(tot+1)/2,n,b,0,1); else { ans=dfs(tot/2,tot/2,n,b/10,0,0); if (ans==0) ans=dfs((tot+2)/2,(tot+2)/2,n,b*10,0,1); } printf("%I64d\n",ans); } return 0; }