Portal
Time limit2sMemory limit1024 MB
Given N portal cells on an infinite grid, find the maximum number of colours that keeps the friend from noticing the teleports, or -1 if the count is unlimited.
- Level
Medium7 of 10
- Topics
- Number theory, Math
- Solved
- No attempts yet
Portal
You want to prank your best friend by placing them on cell of an infinite grid of coloured cells. The friend then moves one step at a time to one of the four adjacent cells, forever.
cells of the grid contain a portal. When the friend steps on a portal, they instantly teleport to a random portal, which may be the same one they just stepped on. If there is a portal at , the friend is teleported at the start as well.
You want the friend to never notice the portals. The friend only sees the colour of the cell they are on, so the colours seen must never change from the friend's point of view. If the friend thinks they have entered a cell again (for example by moving left and then right), they must see the same colour as the first time.
When the friend steps on a portal, they see the colour of the cell they step on and the colour of the cell they are teleported to. So all portal cells must have the same colour.
Colour the grid with as many colours as possible. Colouring every cell the same colour works, but you want more colours than that.
For example, with portals at , and , the friend moves up, right, down, left. The friend ends up back at in their own view, but may actually be at or . These three cells must share a colour, while and may differ.
Print the maximum number of colours that can be used without the friend noticing the portals, or if the number of colours is unlimited.
Input
The first line contains the integer , the number of portals.
The next lines each contain two integers and , meaning there is a portal at cell .
Output
Print a single integer, the maximum number of colours that can be used, or if the number of colours is unlimited.
Constraints
for all
No two portals share the same coordinates.