import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); long n = sc.nextLong(); long m = sc.nextLong(); long g = gcd(n, m); n /= g; m /= g; long x = m; while(m % 10 == 0) { m /= 10; } int base = 1; while (x % 2 == 0) { x /= 2; base = 2; } while (x % 5 == 0) { x /= 5; base = 5; } if (x != 1) { System.out.println(-1); return; } if (m == 1) { while (n % 10 == 0) { n /= 10; } System.out.println(n % 10); return; } ArrayDeque deq = new ArrayDeque<>(); ArrayDeque next = new ArrayDeque<>(); long y = n; while(y > 0) { deq.addFirst((int)(y % 10)); y /= 10; } while (m % base == 0) { int last = 0; boolean isFirst = true; while (deq.size() > 0 || last != 0) { int z = last * 10; if (deq.size() > 0) { z += deq.poll(); } if (!isFirst || z / base != 0) { next.add(z / base); } last = z % base; isFirst = false; } ArrayDeque tmp = next; next = deq; deq = tmp; m /= base; } while (deq.getLast() == 0) { deq.pollLast(); } System.out.println(deq.getLast()); } static long gcd(long x, long y) { if (x % y == 0) { return y; } else { return gcd(y, x % y); } } }