luogu#P4988. 重排DL

重排DL

Background

The level order in Dancing Line always feels kind of random.

Problem Description

On this day, Umaru set a new rule for the level order in Dancing Line: suppose a certain level is the nn-th released level, then its position ana_n satisfies an+1=(annk+2)k+n+1a_{n+1}=(\sqrt[k]{a_n-n}+2)^k+n+1, and the first released level is always placed first, i.e., a1=2a_1=2.

But this clearly causes a problem: many positions will be empty levels. So Umaru added another restriction: adjust kk so that the nn-th level satisfies anb(modm)a_n \equiv b\pmod{m}. Now Umaru gives n,m,bn,m,b, and you need to find the smallest integer kk that satisfies the condition.

Input Format

The first line contains three numbers n,m,bn,m,b.

Output Format

Output one number, representing the smallest kk; if the smallest kk does not exist, output INF.

3 3 2
1
3 8 2
INF

Hint

For 30%30\% of the testdata, n100n\le 100, 0b<m100000\le b<m\le 10000.

For 50%50\% of the testdata, n1012n\le 10^{12}, 0b<m100000\le b<m\le 10000.

For 100%100\% of the testdata, n1012n\le 10^{12}, 0b<m10120\le b<m\le 10^{12}.

It is guaranteed that n,m,bn,m,b are all positive integers.

Translated by ChatGPT 5