#P4616. Pictionary Road Construction
Pictionary Road Construction
Pictionary Road Construction
There is a planet in a yet undiscovered part of the universe where a country inhabited solely by mathematicians exists. In this country, there are N mathematicians living in cities labeled from 1 to N, and initially, no two cities are connected by a road. One day, a mishap with a smartphone auto-correction in an academic paper introduces the term "Pictionary". Soon, every mathematician wants to meet and play Pictionary, so road construction between the cities begins.
The construction lasts for M days. On the ith day, roads are built between every pair of cities a and b if and only if
\( \gcd(a, b) = M - i + 1 \)
Your task is to determine the minimal number of days required before a given pair of mathematicians (cities) can meet and play Pictionary, i.e. when there exists a path connecting the two cities through the constructed roads.
inputFormat
The input consists of a single line with four space-separated integers: N, M, A, and B. Here, N is the number of mathematicians (and cities), M is the number of days over which road construction is scheduled, and A and B are the labels of the two cities for which you need to determine the earliest day that they become connected.
outputFormat
Output a single integer representing the minimal day by which cities A and B are connected (i.e. there is a path connecting them through the constructed roads).
sample
6 3 2 5
3