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(); if(a > 0 && b > 0 || a == 0 && b != 0) { System.out.println(-1); return; } 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); } }