package main
import (
"bufio"
"fmt"
"os"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
scanner.Split(bufio.ScanWords)
scanInt := func() int {
scanner.Scan()
res := 0
for _, b := range scanner.Bytes() {
res = res*10 + int(b-'0')
}
return res
}
if !scanner.Scan() {
return
}
n := 0
for _, b := range scanner.Bytes() {
n = n*10 + int(b-'0')
}
m := scanInt()
adj := make([][]int, n+1)
for i := 0; i < m; i++ {
u := scanInt()
v := scanInt()
adj[u] = append(adj[u], v)
adj[v] = append(adj[v], u)
}
color := make([]int, n+1)
ans := 0
for i := 1; i <= n; i++ {
if color[i] == 0 {
comp := []int{}
q := []int{i}
color[i] = 1
possible := true
for head := 0; head < len(q); head++ {
u := q[head]
comp = append(comp, u)
for _, v := range adj[u] {
if color[v] == 0 {
color[v] = 3 - color[u]
q = append(q, v)
} else if color[v] == color[u] {
possible = false
}
}
}
if !possible {
fmt.Println("-1")
return
}
maxD := 0
dist := make([]int, n+1)
for _, start := range comp {
for _, v := range comp {
dist[v] = -1
}
dist[start] = 0
qBfs := []int{start}
currentMax := 0
for head := 0; head < len(qBfs); head++ {
u := qBfs[head]
currentMax = dist[u]
for _, v := range adj[u] {
if dist[v] == -1 {
dist[v] = dist[u] + 1
qBfs = append(qBfs, v)
}
}
}
if currentMax > maxD {
maxD = currentMax
}
}
ans += maxD
}
}
fmt.Println(ans)
}