import java.util.*; public class Main { public static void main(String args[]) throws Exception { Scanner sc = new Scanner(System.in); int a = sc.nextInt(); int b = sc.nextInt(); int n = 0; Set seen = new HashSet<>(); while(!seen.contains(n)) { seen.add(n); n = a * n + b; } System.out.println(n == 0 ? seen.size() : -1); } }