#include <bits/stdc++.h>
using namespace std;
int dirx[8] = {0, 0, 1, -1, 1, 1, -1, -1};
int diry[8] = {1, -1, 0, 0, 1, -1, 1, -1};
string tostring(long long n) {
  if (!n) return "0";
  string ans = "";
  while (n != 0) {
    ans += (n % 10) + '0';
    n /= 10;
  }
  reverse(ans.begin(), ans.end());
  return ans;
}
const int till = 250;
bool chk(string s) {
  int sum = 0;
  for (int i = 0; i < s.length(); i++) {
    sum *= 10;
    sum += (s[i] - '0');
    sum %= 7;
  }
  return sum % 7 == 0;
}
void solve(string s) {
  int k = 0;
  if (chk(s)) {
    cout << s << endl;
    return;
  }
  while (++k <= till) {
    next_permutation(s.begin(), s.end());
    if (chk(s) && s[0] != '0') {
      cout << s << endl;
      return;
    }
  }
  cout << 0 << endl;
}
int main() {
  string s;
  cin >> s;
  solve(s);
  return 0;
}
