Function to count the pairs
function CountPairs ( n , m , A , B ) { let cnt = 0 ; let lcm = find_LCM ( A , B ) ; for ( let i = 1 ; i <= n ; i ++ ) cnt += Math . floor ( ( m + ( i % lcm ) ) / lcm ) ; return cnt ; }