#include <bits/stdc++.h>
using namespace std;
namespace INPUT {
const int L = 1 << 15;
char _buf[L], *S, *T, c;
char _gc() {
  if (S == T) {
    T = (S = _buf) + fread(_buf, 1, L, stdin);
    if (S == T) return EOF;
  }
  return *S++;
}
void readi(int &X) {
  for (c = _gc(); c < '0' || c > '9'; c = _gc())
    ;
  X = c & 15;
  for (c = _gc(); c >= '0' && c <= '9'; X = X * 10 + (c & 15), c = _gc())
    ;
}
}  // namespace INPUT
const int Maxn = 1005;
const int Maxm = 100005;
int N, M;
struct E {
  int v, nxt;
} adj[Maxm << 1];
int hed[Maxn], TE;
void addedge(int u, int v) {
  ++TE, adj[TE].v = v, adj[TE].nxt = hed[u], hed[u] = TE;
}
int col[Maxn];
int bel[Maxn], n;
void Dfs(int u, int c, int id) {
  col[u] = c;
  bel[u] = id;
  for (int k = hed[u]; k != -1; k = adj[k].nxt)
    if (col[adj[k].v] == -1)
      Dfs(adj[k].v, c ^ 1, id);
    else if (col[adj[k].v] == c) {
      puts("-1");
      exit(0);
    }
}
int mx[Maxn];
int dis[Maxn];
int q[Maxn], ql, qr;
void Bfs(int X) {
  ql = qr = 1;
  memset(dis, -1, sizeof(dis));
  dis[X] = 0, q[qr++] = X;
  while (ql < qr) {
    int u = q[ql++];
    mx[bel[X]] = max(mx[bel[X]], dis[u]);
    for (int k = hed[u]; k != -1; k = adj[k].nxt)
      if (dis[adj[k].v] == -1) dis[adj[k].v] = dis[u] + 1, q[qr++] = adj[k].v;
  }
}
int main() {
  INPUT::readi(N), INPUT::readi(M);
  memset(hed, -1, sizeof(hed));
  for (int u, v, i = 1; i <= M; ++i)
    INPUT::readi(u), INPUT::readi(v), addedge(u, v), addedge(v, u);
  memset(col, -1, sizeof(col));
  for (int i = 1; i <= N; ++i)
    if (col[i] == -1) Dfs(i, 0, ++n);
  for (int i = 1; i <= N; ++i) Bfs(i);
  int Ans = 0;
  for (int i = 1; i <= n; ++i) Ans += mx[i];
  cout << Ans;
  return 0;
}
