Under construction

Still being built. Things may change

Winter Festival

Time limit5sMemory limit512 MB

Summary
Assign costs 0, 1, or 2 to each road so that no area has adjacent roads whose costs sum to 1 mod 3 and every cycle has an odd sum; find the minimum total.
Level

Hard8 of 10

Topics
Graph, DFS, Union-find
Solved
No attempts yet

Problem

Peter's favorite season is winter. Caught up in the festivities, Peter would like to decorate his city.

The city has many areas for things like ice skating, skiing, and snowball fights. There are roads connecting some pairs of areas. Peter would like to put a special decoration along each of these roads. There are three types of decorations of costs 0, 1, and 2, respectively.

Peter would like his decoration to meet some properties:

  • For any pair of distinct roads adjacent to an area, let aa and bb be the costs of decorations along those roads. It must hold that (a+b) mod 3≠1(a + b) \bmod 3 \neq 1.
  • The sum of costs along any cycle must be an odd number.

A cycle is a sequence of areas connected by roads that form a loop. Each area may appear exactly once in the loop.

Peter would like to know: What is the cheapest amount he can pay to decorate his city the way he wants?

Input

Each input will consist of a single test case. Your program may be run multiple times on different inputs.

Each test case begins with a line containing two integers nn and mm (1≤n,m≤1051 \le n, m \le 10^5), where nn is the number of areas and mm is the number of roads. The areas are numbered 1 to nn.

Each of the next mm lines contains two integers aa and bb (1≤a<b≤n1 \le a < b \le n), indicating that there is a road directly connecting areas aa and bb. No two roads connect the same two areas. It may or may not be possible to get from every area to every other area along the roads.

Output

Output a single integer, the minimum cost of decorating the city, or −1-1 if it is not possible to decorate the city according to Peter's properties.

Examples3

  1. Example 1

    Input
    5 8
    1 4
    4 5
    1 5
    1 2
    1 3
    2 3
    3 5
    2 5
    
    Expected output
    -1
    
  2. Example 2

    Input
    6 5
    2 4
    3 5
    1 5
    3 6
    1 6
    
    Expected output
    5
    
  3. Example 3

    Input
    10 10
    5 8
    2 6
    3 9
    1 4
    9 10
    4 6
    5 9
    7 8
    7 10
    2 3
    
    Expected output
    5