#include <bits/stdc++.h>
using namespace std;
#define INF (int)1e9
#define MOD 1000000007
#define PI 3.1415926535897932384626433832795
//
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<string> vstr;
typedef vector<pii> vpii;
typedef vector<vint> vvi;
typedef map<int,int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
#define mk make_pair
#define pb push_back
#define ff first
#define ss second
//
#define por(i, a, b, in) for (int i=a ; i<(b) ; i+=in)
#define rpor(i, a, b, in) for (int i=a-1 ; i>=(b) ; i-=in)
#define rep(i, a, b) por(i, a, b, 1)
#define rrep(i, a, b) rpor(i, a, b, 1)
#define each(it, mp) for (auto it = mp.begin(); it != mp.end(); it++)
#define all(c) c.begin(), c.end()
#define allg(c) all(c), greater<> ()
#define len(v) (int) v.size()
#define notin(c, x) ((c).find(x) == (c).end())
//
#define show(x) cout << x << "\n";
#define showp(x, y) cout << x << " " << y << "\n";
#define showv(v, n) rep(i, 0, n) {cout << v[i] << " ";} cout << "\n";
#define showvp(v, n) rep(i, 0, n) {showp(v[i].ff, v[i].ss);}
#define showvv(vv, n, m) rep(i, 0, n) {showv(vv[i], m);}
#define showset(it, st) each(it, st) {cout << *it << " ";} cout << "\n";
#define showmap(it, mp) each(it, mp) {cout << it->ff << " " << it->ss << "\n";} cout << "\n";
#define precision(d) cout << setprecision(d) << fixed;

void solve () {
	string s;
	cin >> s;
	int n = len(s);
	rrep (i, n, 0) {
		if (s[i] == '6') {
			cout << 9;
		} else if (s[i] == '9') {
			cout << 6;
		} else 
		cout << s[i];
	}
	cout << "\n";
}

int main () 
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	
	//int t; cin >> t; while (t--)
	
	solve();
	
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long int

void get(int n, vector<int>& oct) {
    vector<int> vec1(100);
    int k = 0;
    while (n != 0) {
        vec1[k] = n % 8;
        n = n / 8;
        k++;
    }
    for (int j = k - 1; j >= 0; j--) oct.push_back(vec1[j]);
}

int main() {
    ios::sync_with_stdio(false);

    int n;
    cin >> n;

    int cnt = 0;

    for (int i = 1; i <= n; i++) {
        bool t = true;
        string cur = to_string(i);
        for (int j = 0; j < cur.length(); j++) {
            if (cur[j] - '0' == 7) {
                t = false;
                break;
            }
        }

        if (t) {
            vector<int> vec;
            get(i, vec);

            for (int j = 0; j < vec.size(); j++) {
                //cout << vec[j] << endl;
                if (vec[j] == 7) {
                    t = false;
                    break;
                }
            }
        }

        if (t) cnt ++;

    }
    cout << cnt << endl;

    return 0;


}