#include <bits/stdc++.h>
using namespace std;
const int INF = 2000000000;
const double EPS = 1e-9;
int mods(int a, int b) { return (b + (a % b)) % b; }
bool check(string s) {
  int rem = 0;
  for (int i = 0; i < s.size(); i++) rem = (rem * 10 + (s[i] - '0')) % 7;
  return rem == 0;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string s, tmp, keys = "";
  bool allZero = 1, added[10] = {};
  cin >> s;
  for (int i = 0; i < s.size(); i++) {
    if ((s[i] == '1' || s[i] == '6' || s[i] == '8' || s[i] == '9') &&
        !added[s[i] - '0']) {
      keys += s[i];
      added[s[i] - '0'] = 1;
      s.erase(s.begin() + i);
      i--;
    } else if (s[i] != '0')
      allZero = 0;
  }
  if (!allZero) {
    while (s[0] == '0') {
      s.erase(s.begin());
      s += '0';
    }
  }
  sort(keys.begin(), keys.end());
  do {
    if (allZero)
      tmp = keys + s;
    else
      tmp = s + keys;
    if (check(tmp)) {
      cout << tmp << '\n';
      return 0;
    }
  } while (next_permutation(keys.begin(), keys.end()));
  cout << "0\n";
  return 0;
}
