class Euler64 {

    private static class Options {
        int limit = 10000;
        boolean runCheckpoints = true;
    }

    private static boolean parseIntAfterPrefix(String arg, String prefix, Options options) {
        if (!arg.startsWith(prefix)) {
            return false;
        }
        String tail = arg.substring(prefix.length());
        if (tail.isEmpty()) {
            return false;
        }

        int parsed = 0;
        for (int i = 0; i < tail.length(); i++) {
            char c = tail.charAt(i);
            if (c < '0' || c > '9') {
                return false;
            }
            parsed = parsed * 10 + (c - '0');
        }
        options.limit = parsed;
        return true;
    }

    private static boolean parseArguments(String[] args, Options options) {
        for (String arg : args) {
            if ("--skip-checkpoints".equals(arg)) {
                options.runCheckpoints = false;
                continue;
            }
            if (parseIntAfterPrefix(arg, "--limit=", options)) {
                continue;
            }

            System.err.println("Unknown argument: " + arg);
            return false;
        }
        return options.limit >= 2;
    }

    private static int periodLength(int n) {
        int a0 = (int) Math.sqrt(n);
        if (a0 * a0 == n) {
            return 0;
        }

        int m = 0;
        int d = 1;
        int a = a0;
        int period = 0;

        do {
            m = d * a - m;
            d = (n - m * m) / d;
            a = (a0 + m) / d;
            period++;
        } while (a != 2 * a0);

        return period;
    }

    private static int solve(int limit) {
        int count = 0;
        for (int n = 2; n <= limit; n++) {
            if ((periodLength(n) & 1) == 1) {
                count++;
            }
        }
        return count;
    }

    private static boolean runCheckpoints() {
        if (solve(13) != 4) {
            System.err.println("Checkpoint failed for limit=13");
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        Options options = new Options();
        if (!parseArguments(args, options)) {
            System.exit(1);
        }
        if (options.runCheckpoints && !runCheckpoints()) {
            System.exit(2);
        }

        System.out.println(solve(options.limit));
    }
}
