import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

public class Euler66 {

    private static class Options {
        int limit = 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;
        }
        for (int i = 0; i < tail.length(); i++) {
            if (!Character.isDigit(tail.charAt(i))) {
                return false;
            }
        }
        options.limit = Integer.parseInt(tail);
        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 BigInteger minimalXForPell(int D) {
        int a0 = (int) Math.sqrt(D);
        if (a0 * a0 == D) {
            return BigInteger.ZERO;
        }

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

        BigInteger numPrev = BigInteger.ONE;
        BigInteger num = BigInteger.valueOf(a);
        BigInteger denPrev = BigInteger.ZERO;
        BigInteger den = BigInteger.ONE;

        while (!num.multiply(num).subtract(BigInteger.valueOf(D).multiply(den).multiply(den)).equals(BigInteger.ONE)) {
            m = d * a - m;
            d = (D - m * m) / d;
            a = (a0 + m) / d;

            BigInteger nextNum = BigInteger.valueOf(a).multiply(num).add(numPrev);
            BigInteger nextDen = BigInteger.valueOf(a).multiply(den).add(denPrev);
            numPrev = num;
            denPrev = den;
            num = nextNum;
            den = nextDen;
        }

        return num;
    }

    private static int solve(int limit) {
        int bestD = 0;
        BigInteger bestX = BigInteger.ZERO;

        for (int D = 2; D <= limit; D++) {
            BigInteger x = minimalXForPell(D);
            if (x.compareTo(bestX) > 0) {
                bestX = x;
                bestD = D;
            }
        }

        return bestD;
    }

    private static boolean runCheckpoints() {
        if (solve(7) != 5) {
            System.err.println("Checkpoint failed for limit=7");
            return false;
        }
        if (solve(13) != 13) {
            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));
    }
}
