Problem D
Colouring Book

The Ministry of Trigonometric Artistry (or MTA for short) is
producing a colouring book to get children interested in
trigonometry at a young age. The colouring book consists of
many pages. On each page is a set of numbered dots that are to
be connected with straight line segments according to some
instructions given on the page. Once these line segments are
drawn, the entire page is to be coloured with as many colours
as the young trigonometric artist desires, provided that the
following rule is obeyed: in order to move continuously from a
point anywhere on the page that has been coloured with colour
MTA’s executives would like to determine how fun the colouring book is going to be. So for each picture that the Ministry’s designers have created, the executives want to know the maximum number of colours that can be used to colour that page. They also want to know if the set of instructions that they are giving to join the dots is valid. An instruction set is said to be invalid if (a) there exist two line segments that intersect at a point that is not an endpoint of both of the line segments, (b) there is a dot that lies on a line segment but is not one of its endpoints, or (c) there is a pair of dots that are not connected by some sequence of line segments.
![\includegraphics[width=0.3\textwidth ]{sample1}](/problems/colouringbook/file/statement/en/img-0002.jpg)
Input
The input consists of a single test case. The first line
contains two space-separated integers
Output
If the instruction set is invalid, output
Sample Input 1 | Sample Output 1 |
---|---|
4 4 0 0 4 0 4 3 6 5 0 1 1 2 0 2 3 2 |
2 |
Sample Input 2 | Sample Output 2 |
---|---|
4 3 -1 0 1 0 0 1 0 -1 0 1 2 3 0 2 |
-1 |
Sample Input 3 | Sample Output 3 |
---|---|
4 2 0 0 1 0 2 0 3 0 0 1 2 3 |
-1 |