#include <bits/stdc++.h>
const int N = 1e3 + 5, M = 1e5 + 5;
int n, m, Enum, H[N], nxt[M << 1], to[M << 1], vis[N], cnt, bel[N], ans_b[N],
    dis[N], q[N];
inline int read() {
  int now = 0;
  register char c = getchar();
  for (; !isdigit(c); c = getchar())
    ;
  for (; isdigit(c); now = now * 10 + c - '0', c = getchar())
    ;
  return now;
}
inline void AddEdge(int u, int v) {
  to[++Enum] = v, nxt[Enum] = H[u], H[u] = Enum;
  to[++Enum] = u, nxt[Enum] = H[v], H[v] = Enum;
}
void DFS(int x, int f) {
  bel[x] = cnt;
  for (int v, i = H[x]; i; i = nxt[i])
    if ((v = to[i]) != f && !vis[v])
      vis[v] = vis[x] ^ 1, DFS(v, x);
    else if (vis[v] == vis[x]) {
      printf("-1");
      exit(0);
    }
}
int BFS(int now) {
  memset(dis, 0, sizeof dis);
  int h = 0, t = 1, res = 0;
  q[0] = now, dis[now] = 1;
  while (h < t) {
    int x = q[h++];
    for (int i = H[x]; i; i = nxt[i])
      if (!dis[to[i]])
        res = std::max(res, dis[to[i]] = dis[x] + 1), q[t++] = to[i];
  }
  return res - 1;
}
int main() {
  n = read(), m = read();
  for (int u, v, i = 1; i <= m; ++i) u = read(), v = read(), AddEdge(u, v);
  for (int i = 1; i <= n; ++i)
    if (!vis[i]) ++cnt, vis[i] = 2, DFS(i, i);
  for (int i = 1; i <= n; ++i) ans_b[bel[i]] = std::max(ans_b[bel[i]], BFS(i));
  int res = 0;
  for (int i = 1; i <= cnt; ++i) res += ans_b[i];
  printf("%d", res);
  return 0;
}
