https://www.acmicpc.net/problem/2526
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner s=new Scanner(System.in);
int n=s.nextInt(), p=s.nextInt();
int arr[]=new int[p];
arr[0]=n;
int i=0;
int j=0;
int c=0;
for(i=1;i<p;i++){
arr[i]=(arr[i-1]*n)%p;
for(j=0;j<i;j++){
if(arr[i]==arr[j]){
c=1;
break;
}
}
if(c==1) break;
}
System.out.format("%d",p==i?j:i-j);
}
}