#P4884. Repunit Congruence Problem

    ID: 18125 Type: Default 1000ms 256MiB

Repunit Congruence Problem

Repunit Congruence Problem

Given an integer \(K\) and a prime number \(m\), find the smallest positive integer \(N\) such that the repunit number formed by \(N\) consecutive ones (i.e. \(\underbrace{11\cdots1}_{N}\)) satisfies

\[ \underbrace{11\cdots1}_{N} \equiv K \pmod{m} \]

If no such \(N\) exists, output -1.

inputFormat

The input consists of two space-separated integers \(K\) and \(m\) on a single line. Here, \(m\) is a prime number.

outputFormat

Output the smallest positive integer \(N\) such that the number formed by \(N\) ones is congruent to \(K\) modulo \(m\). If no such \(N\) exists, output -1.

sample

1 7
1