House of Cards

Given a lower bound h0, find the smallest height h >= h0 such that the total card count h(3h+1)/2 is divisible by 4.

Medium6MathNumber theoryImplementationBrute forceNo attempts yetTime limit2sMemory limit512 MB

Problem

Susan wants to build a house of cards so big that the whole country would be buried in cards if it toppled over.

The house has a triangular shape. The photo above shows a house of height 6.

A house of height hh is built from hh rows. Counting from the top, row ii consists of ii pairs of cards leaning against each other, so it uses 2i2i cards. Between row ii and row i+1i+1 lie ii flat cards. The total number of cards in a house of height hh is therefore

h(3h+1)2\frac{h(3h+1)}{2}

The figure above shows the structure of a house of height 5, which uses 40 cards.

For the house to look right, the four suits (clubs, diamonds, hearts, spades) must appear equally often. Depending on the height, that is sometimes possible and sometimes not. Given a lower bound h0h_0 on the height, find the smallest height hh0h \ge h_0 for which such a house can be built.

Input

The first line contains an integer h0h_0, the minimum height of the house (1h01010001 \le h_0 \le 10^{1000}).

Output

Print the smallest height hh0h \ge h_0 for which a house can be built using each of the four suits equally often.