#include <bits/stdc++.h>
using namespace std;
vector<int> next_p(vector<int> s) {
  vector<int> ss = s;
  int I = -1, J = -1;
  for (int i = s.size() - 1; i >= 0; i--) {
    if (s[i] == 7) {
      I = i;
      break;
    }
  }
  for (int j = I - 1; j >= 0; j--) {
    if (s[j] == 4) {
      J = j;
      break;
    }
  }
  if (I == -1 || J == -1)
    return s;
  else {
    int tmp = ss[I];
    ss[I] = ss[J];
    ss[J] = tmp;
    sort(ss.begin() + J + 1, ss.end());
  }
  return ss;
}
int grt(string s, string t) {
  if (s.size() > t.size())
    return 1;
  else if (s.size() < t.size())
    return 2;
  for (int i = (int)0; i < (int)s.size(); ++i) {
    if (s[i] - '0' > t[i] - '0')
      return 1;
    else if (s[i] - '0' < t[i] - '0')
      return 2;
  }
  return 2;
}
string smallest_given(int n, int fours, int sevens) {
  string s("");
  for (int i = (int)0; i < (int)(n / 2) - fours; ++i) s += '4';
  for (int i = (int)0; i < (int)(n / 2) - sevens; ++i) s += '7';
  return s;
}
string smallest(int n) {
  string s("");
  for (int i = (int)0; i < (int)n / 2; ++i) s += '4';
  for (int i = (int)0; i < (int)n / 2; ++i) s += '7';
  return s;
}
string next(string s) {
  int fr = 0;
  int sv = 0;
  vector<int> vv, nn;
  for (int i = (int)0; i < (int)s.size(); ++i) {
    if (s[i] == '4') {
      fr++;
      vv.push_back(4);
    } else if (s[i] == '7') {
      sv++;
      vv.push_back(7);
    }
  }
  if (fr == sv)
    return s;
  else {
    nn = next_p(vv);
    int fg = 0;
    if (vv == nn) {
      if (vv[0] == 4) {
        return smallest(vv.size());
      } else {
        return smallest(vv.size() + 2);
      }
    } else {
      if (fr > sv) {
        int j = 0;
        for (int i = vv.size() - 1; i >= 0; i--) {
          if (vv[i] == 4) {
            vv[i] = 7;
            fr--;
            sv++;
          }
          if (fr == sv) break;
        }
      } else {
        for (int i = vv.size() - 1; i >= 0; i--) {
          if (vv[i] == 7) {
            vv[i] = 4;
            fr++;
            sv--;
          }
          if (fr == sv) break;
        }
        vv = next_p(vv);
      }
      string t;
      for (int i = (int)0; i < (int)vv.size(); ++i) {
        if (vv[i] == 4)
          t += '4';
        else if (vv[i] == 7)
          t += '7';
      }
      return t;
    }
  }
}
int main() {
  string s;
  cin >> s;
  vector<int> v, v_next;
  if (s.size() % 2 == 0) {
    string sofar("");
    int fours = 0;
    int sevens = 0;
    if (s[0] - '0' > 7) {
      sofar += smallest(s.size() + 2);
    } else {
      for (int i = (int)0; i < (int)s.size(); ++i) {
        if ((s[i] - '0') < 4) {
          sofar += smallest_given(s.size(), fours, sevens);
          break;
        } else if (s[i] == '4') {
          sofar += '4';
          v.push_back(4);
          fours++;
        } else if (s[i] == '5' || s[i] == '6') {
          sofar += '7';
          sevens++;
          sofar += smallest_given(s.size(), fours, sevens);
          break;
        } else if (s[i] == '7') {
          sofar += '7';
          sevens++;
          v.push_back(7);
        } else if (s[i] - '0' > 7) {
          v_next = next_p(v);
          int fg = 0;
          if (v == v_next) {
            if (v[0] == 4) {
              v[v.size() - 1] = 7;
              fours--;
              sevens++;
            } else {
              fg = 1;
              v.clear();
              for (int i = (int)0; i < (int)v_next.size() + 1; ++i)
                v.push_back(4);
              fours = v_next.size() + 1;
              sevens = 0;
            }
          } else {
          }
          sofar = "";
          for (int i = (int)0; i < (int)v.size(); ++i) {
            if (v[i] == 4)
              sofar += '4';
            else
              sofar += '7';
          }
          if (fg == 1) {
            sofar = smallest_given(s.size() + 2, 0, 0);
          } else {
            sofar += smallest_given(s.size(), fours, sevens);
            if (grt(sofar, s) == 2) {
              v.clear();
              for (int i = (int)0; i < (int)sofar.size(); ++i) {
                v.push_back(sofar[i] - '0');
              }
              v = next_p(v);
              sofar = "";
              for (int i = (int)0; i < (int)v.size(); ++i) {
                if (v[i] == 4)
                  sofar += '4';
                else
                  sofar += '7';
              }
            }
          }
          break;
        }
      }
    }
    cout << next(sofar) << endl;
  } else {
    cout << smallest(s.size() + 1) << endl;
  }
  return 0;
}
