int ax = 0; int ay = 0; // alice's x and y coordinates
int bx = r; int by = s; // bob's x and y coordinates
int t = 0; // keep track of the current time
while(ax < bx && ay < by){
// every second, update alice's and bob's coordinates and the time
ax += m; ay += n;
bx -= p; by -= q;
t++;
}
if(ax == bx && ay == by){ // if they are in the same location
pw.println(t); // they meet at time t
} else {
pw.println(-1); // they never meet
}
pw.close(); // flush the output