Problem:
Score : 300 points

Problem Statement

You are given an integer N. Among the integers between 1 and N (inclusive), how many Shichi-Go-San numbers (literally "Seven-Five-Three numbers") are there?

Here, a Shichi-Go-San number is a positive integer that satisfies the following condition:

When the number is written in base ten, each of the digits 7, 5 and 3 appears at least once, and the other digits never appear.

Constraints

1 \leq N < 10^9

N is an integer.

Input

Input is given from Standard Input in the following format:

N

Output

Print the number of the Shichi-Go-San numbers between 1 and N (inclusive).

Sample Input 1

575

Sample Output 1

4

There are four Shichi-Go-San numbers not greater than 575: 357, 375, 537 and 573.

Sample Input 2

3600

Sample Output 2

13

There are 13 Shichi-Go-San numbers not greater than 3600: the above four numbers, 735, 753, 3357, 3375, 3537, 3557, 3573, 3575 and 3577.

Sample Input 3

999999999

Sample Output 3

26484

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Accepted)

import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author __shangu__
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        C755 solver = new C755();
        solver.solve(1, in, out);
        out.close();
    }

    static class C755 {
        private int[] dig;
        private int[] enu;
        private int n;
        private int len;

        public void solve(int testNumber, InputReader in, PrintWriter out) {
            dig = new int[20];
            enu = new int[]{3, 5, 7};
            n = in.nextInt();
            while (n > 0) {
                dig[len++] = n % 10;
                n /= 10;
            }
            out.println(calc(len - 1, new boolean[3], false, false));
        }

        private int calc(int curi, boolean[] has, boolean issmaller, boolean hasstarted) {
            if (curi == -1) return hasstarted && has[0] && has[1] && has[2] ? 1 : 0;
            int ans = 0;
            if (issmaller) {
                for (int i = 0; i < 3; ++i) {
                    boolean tmp[] = has.clone();
                    tmp[i] = true;
                    ans += calc(curi - 1, tmp, true, true);
                }
                if (!hasstarted) ans += calc(curi - 1, has, true, false);
            } else {
                for (int i = 0; i < 3; ++i) {
                    boolean tmp[] = has.clone();
                    tmp[i] = true;
                    if (enu[i] < dig[curi]) ans += calc(curi - 1, tmp, true, true);
                    else if (enu[i] == dig[curi]) ans += calc(curi - 1, tmp, false, true);
                }
                if (!hasstarted) ans += calc(curi - 1, has, true, false);
            }
            return ans;
        }

    }

    static class InputReader {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public InputReader(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

    }
}