#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    long long k;
    cin >> k;

    vector<int> digits;
    while (k > 0) {
        digits.push_back(k % 10);
        k /= 10;
    }

    vector<int> reflected_digits;
    for (int digit : digits) {
        if (digit == 3 || digit == 4 || digit == 7) {
            cout << -1 << endl;
            return 0;
        }
        reflected_digits.push_back((digit == 2) ? 5 : ((digit == 5) ? 2 : ((digit == 6) ? 9 : ((digit == 9) ? 6 : digit))));
    }

    reverse(reflected_digits.begin(), reflected_digits.end());

    for (int digit : reflected_digits) {
        cout << digit;
    }
    cout << endl;

    return 0;
}
