#include <bits/stdc++.h>
using namespace std;
long long l[20] = {0,
                   74,
                   7744ll,
                   777444ll,
                   77774444ll,
                   7777744444ll,
                   777777444444ll,
                   77777774444444ll,
                   7777777744444444ll};
int main() {
  int t = 1;
  while (t--) {
    long long n;
    scanf("%I64d", &n);
    int bit = 0;
    long long nn = n;
    int num[20];
    while (nn) {
      num[bit++] = nn % 10;
      nn /= 10;
    }
    if (n <= 47) {
      puts("47");
      continue;
    }
    if (bit & 1) {
      for (int i = 0; i <= bit / 2; i++) {
        putchar('4');
      }
      for (int i = 0; i <= bit / 2; i++) {
        putchar('7');
      }
      puts("");
    } else {
      if (n < 1e18 + 1 && n > l[bit / 2]) {
        for (int i = 0; i <= bit / 2; i++) {
          putchar('4');
        }
        for (int i = 0; i <= bit / 2; i++) {
          putchar('7');
        }
        puts("");
        continue;
      }
      bool gt = 0;
      int cnt4 = bit / 2, cnt7 = bit / 2;
      for (int i = bit - 1; i >= 0; i--) {
        if (num[i] < 4) {
          if (gt) {
            if (cnt4) {
              putchar('4');
              cnt4--;
            } else {
              putchar('7');
              cnt7--;
            }
          } else {
            gt = 1;
            if (cnt4) {
              putchar('4');
              cnt4--;
            } else {
              putchar('7');
              cnt7--;
            }
          }
        } else if (num[i] == 4) {
          if (gt) {
            if (cnt4) {
              putchar('4');
              cnt4--;
            } else {
              putchar('7');
              cnt7--;
            }
          } else {
            if (!cnt4) {
              putchar('7');
              cnt7--;
              gt = 1;
            }
            int flag = 0, flag2 = 0;
            int st = i, ct7 = cnt7;
            while (st >= 0 && ct7) {
              ct7--;
              st--;
              if (num[st] > 7) {
                flag2 = 1;
                goto en;
              }
              if (num[st] < 7) {
                flag = 1;
                goto en;
              }
            }
            while (st) {
              st--;
              if (num[st] > 4) {
                flag2 = 1;
              }
            }
          en:;
            if (!flag2) {
              putchar('4');
              cnt4--;
            } else {
              putchar('7');
              cnt7--;
              gt = 1;
            }
          }
        } else if (num[i] < 7) {
          if (gt) {
            if (cnt4) {
              putchar('4');
              cnt4--;
            } else {
              putchar('7');
              cnt7--;
            }
          } else {
            putchar('7');
            cnt7--;
            gt = 1;
          }
        } else if (num[i] == 7) {
          if (gt) {
            if (cnt4) {
              putchar('4');
              cnt4--;
            } else {
              putchar('7');
              cnt7--;
            }
          } else {
            putchar('7');
            cnt7--;
          }
        } else {
          if (cnt4) {
            putchar('4');
            cnt4--;
          } else {
            putchar('7');
            cnt7--;
          }
        }
      }
      puts("");
    }
  }
  return 0;
}
