#include <bits/stdc++.h>
using namespace std;
struct edg {
  int to, nxt;
} e[200005];
int cnt, head[1005], n, m, col[1005], dis[1005], tot, bel[1005], ans;
bool vis[1005], fl, vis1[1005];
void add(int u, int v) {
  cnt++;
  e[cnt].to = v;
  e[cnt].nxt = head[u];
  head[u] = cnt;
}
void dfs(int u, int c) {
  bel[++tot] = u;
  vis[u] = 1;
  col[u] = c;
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (!vis[v])
      dfs(v, c ^ 1);
    else
      fl |= (col[u] == col[v]);
  }
}
int bfs(int s) {
  memset(dis, 0, sizeof(dis));
  memset(vis1, 0, sizeof(vis1));
  queue<int> q;
  q.push(s);
  vis1[s] = 1;
  while (q.size()) {
    int u = q.front();
    q.pop();
    for (int i = head[u]; i; i = e[i].nxt) {
      int v = e[i].to;
      if (!vis1[v]) {
        vis1[v] = 1;
        dis[v] = dis[u] + 1;
        q.push(v);
      }
    }
  }
  int mx = 0;
  for (int i = 1; i <= n; i++) mx = max(mx, dis[i]);
  return mx;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    add(u, v);
    add(v, u);
  }
  for (int i = 1; i <= n; i++) {
    if (vis[i]) continue;
    tot = 0;
    int mx = 0;
    dfs(i, 0);
    if (fl) {
      printf("-1");
      return 0;
    }
    for (int i = 1; i <= tot; i++) mx = max(mx, bfs(bel[i]));
    ans += mx;
  }
  printf("%d", ans);
  return 0;
}
