public static void main(String[] args) {
		BigInteger max = BigInteger.ZERO;
		int maxD = 0;

		for (int D = 2; D <= 1000; D++) {
			double root = Math.sqrt(D);
			if (root != (int) root) {
				List<BigInteger> continued = getContinuedFraction(D);
				BigInteger x = BigInteger.ZERO, y = BigInteger.ZERO;
				BigInteger[] result = null;

				int limit = 1;
				while ((result = getFraction(continued.get(0),
						continued.subList(1, continued.size()), 0, limit)) != null
						&& (x = result[1]).compareTo(BigInteger.ZERO) > 0
						&& (y = result[0]).compareTo(BigInteger.ZERO) > 0
						&& !isSolution(x, y, D))
					limit++;

				if (x.compareTo(max) > 0) {
					max = x;
					maxD = D;
				}
			}
		}

		System.out.println(maxD);
	}