public class Euler25 {
    private static class Options {
        int digits = 1000;
        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.digits = parsed;
        return true;
    }

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

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

    private static int solve(int digits) {
        if (digits == 1) {
            return 1;
        }

        double previous = 1.0;
        double current = 1.0;
        int decade = 1;
        int index = 1;

        while (decade < digits) {
            double next = previous + current;
            previous = current;
            current = next;
            if (previous >= 10.0) {
                previous /= 10.0;
                current /= 10.0;
                decade++;
            }
            index++;
        }

        return index;
    }

    private static boolean runCheckpoints() {
        if (solve(2) != 7) {
            System.err.println("Checkpoint failed for digits=2");
            return false;
        }
        if (solve(3) != 12) {
            System.err.println("Checkpoint failed for digits=3");
            return false;
        }
        if (solve(1000) != 4782) {
            System.err.println("Checkpoint failed for digits=1000");
            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.digits));
    }
}
